cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Ultrasonic sensor and voltage division

Mon Feb 11, 2019 6:57 pm

I have a question which probably is stupid, but I just don't understand this.

I intend to connect a ultrasonic sensor (HC-SR04) to a robot. There are many guides, I could do it, but I don't understand one thing regarding the resistances.

So to my understanding Ground (-) and Echo (+) basically form an own circuit. Physically electricity moves from - to +. And two resistors are needed to bring 5V to a level the GPIO pin can handle. Below is a diagramm showing this.

R2 with 470 Ohm reduces the 5V voltage to ~ 2,9 V (I is 0,00625 A) and right after that you connect PIN 18 that can handle max 3.3 V, so that's okay.

1) But why do you need R1 then? If physically the electricity (electrons) moves from from Ground to +, then it wouldn't be necessary? :?:
2) But then somehow Echo sends a signal with 5V voltage to Pin 18, then you would of course also need a resistor (R1 reduces 5 V to rougly 2 V). But how can the electrical signal move backwards then from basically + to - ? :?:

This is probably a stupid question, but I would like to understand the idea behind this before I proceed. I was never good at physics (teacher sucked as much as me) and now I'm very interested in the topic and reading theory, but this practical example here I don't really understand.

I really would appreciate if someone could explain this for me.

Image

tempest766
Posts: 105
Joined: Sun Jan 07, 2018 3:44 pm

Re: Ultrasonic sensor and voltage division

Mon Feb 11, 2019 7:18 pm

you've got it backwards, which isn't surprising. Since electrons are negatively charged, people assume that (-) is the supply of electrons...but it's actually the other way around.

"echo" is a supply of electrons that want to move to ground (-), being pushed at some voltage potential (5v).

Your diagram is a simple voltage divider where the voltage at pin18 is the proportional voltage drop between the source "echo" and ground, as defined by the values of the resisters(kirchoff voltage divider law). the resisters are also necessary to limit the amount of current coming out of echo, lest you fry the component.

Brandon92
Posts: 704
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: Ultrasonic sensor and voltage division

Mon Feb 11, 2019 9:02 pm

Well, if you look at the electrons. They indeed move from the (-) to the (+). However, by convention we decide that the current flows from (+) to (-). More info

So, in your case, (if we make the input of the Rpi infinity), the current will go from "power supply" -> Echo -> R1 -> R2 -> ground -> "power supply". And this is indeed one of the kirchoff laws.

And to give you a simple example of your setup:
The current that will flow out of the echo is: I=U/(R1+R2)=5/(330/470)=6.25mA. So, there is indeed 2.9V across R2 and the voltage across R1 is 2.1V. And the voltage across R1 and R2 are together 5V (Kirchhoff's voltage law). And if you remove the R1, the current can not make a full loop at where he started as showed in this picture:
Image
*https://learn.sparkfun.com/tutorials/vo ... viders/all

And the next question you need to ask yourself, can the HC-SR04 deliver the 6.25mA?

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Mon Feb 11, 2019 10:56 pm

Thanks for the replies. Much appreciated.
Brandon92 wrote:
Mon Feb 11, 2019 9:02 pm
Well, if you look at the electrons. They indeed move from the (-) to the (+). However, by convention we decide that the current flows from (+) to (-). More info

So, in your case, (if we make the input of the Rpi infinity), the current will go from "power supply" -> Echo -> R1 -> R2 -> ground -> "power supply". And this is indeed one of the kirchoff laws.

And to give you a simple example of your setup:
The current that will flow out of the echo is: I=U/(R1+R2)=5/(330/470)=6.25mA. So, there is indeed 2.9V across R2 and the voltage across R1 is 2.1V. And the voltage across R1 and R2 are together 5V (Kirchhoff's voltage law). And if you remove the R1, the current can not make a full loop at where he started as showed in this picture:
Image
*https://learn.sparkfun.com/tutorials/vo ... viders/all

And the next question you need to ask yourself, can the HC-SR04 deliver the 6.25mA?
This convention to assume that the current goes from - to + when it physically the other way around is something I don't really understand. :?

To me it now seems that the current physically goes on my diagram from Ground through R2 towards pin 18 and at the same the current also goes from Echo throught R1 towards pin 18. Assuming that the current physically only goes from Ground (-) towards Echo via Pin 18 and R2, why do I need R1 then? Couldn't I just replace R1 with a simple wire? Or does the flow of the current change here, because - and + change with each other?

I think my my main problem is to grasp, why two resistors are required when the current only flows in one direction (- to +). :?
Maybe these diagrams are noe explaining enough for me?

Very frustrating, because it seems to be a simple standard thing... :oops: :?

Brandon92
Posts: 704
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 6:38 am

Yes, physical the current flow from - to +. But we dicided to make it goes from + to -. To make it easier for or self. (Easier to calculate for example, you don't need to thing much about the - sing.)

Anyway, this explanation we use the convention way.

So, let's assume that a F1 car is the current and that if goes around a oval race track. Where the start and finish is the echo. And R1 is one big turn of the circuit and R2 is the other one.
When the echo is enabled the F1 cars are beginning to race and everything is happy. Because they can make all the laps they want. So, now we cut the whole R1 turn. This means that the F1 cars can not complete the circuit any more. And they can not move to the finish any more.
So, if they are not moving through the R1 turn. There is also no current through the resistor R2, and no current means that there is also no voltage across that resistor.

So the current want to make always a full circuit. And if you make the R1 resistor a wire, what has a very low resistance. The total resistance has changed and the 6.25mA is not true any more. Try to calculate this :)

User avatar
Burngate
Posts: 5780
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 12:09 pm

cloudapplepi wrote:
Mon Feb 11, 2019 10:56 pm
This convention to assume that the current goes from - to + when it physically the other way around is something I don't really understand. :?
Blame the 18th & 19th century physicists for this.

They could see strange things happening when they connected differing metals, with chemicals between them, or when they moved magnets inside coils.
There appeared to be a flow of something, but they hadn't a clue what that something could be, or which direction it was going.
Nor did there appear to be any way to find out.

So they made a random choice - which turned out to be wrong.
A bit like trying to convert the whole world to driving on the sensible side of roads, assigning the charge on the electron a positive value would be possible, but is unlikely to happen.

So, indeed, in your diagram, electrons set off from the ground pin, go through your two resistors, and into the Echo pin - from where they're sent through the 5v pin to the power supply which pushes them to the ground pin, again.

How many electrons go round that loop per second is determined by the resistance of the whole loop - lower resistance means more electrons/second.
Since the charge on the electron is rather small, and so the number of electrons would be astronomical, we tend to talk about the total charge rather than the number of electrons, calling them Coulombs (after the bloke of that name, 1736 - 1806, another of those pesky early physicists) and then promptly ignore him, in favour of the Amp (Andre-Marie Ampere, 1775 - 1836) which is just Coulombs/second.

So, to make the voltage (Alessandro Giuseppe Antonio Anastasio Volta, 1745 - 1827) across the 470Ω resistor R2 about 3v, we need 6¼ mA to flow through it.
For that to happen, the total resistance in the loop has to be about 800Ω. Hence R1.

And just to fill out the dates, Georg Simon Ohm 1789 – 1854. Whoever decided to use the Greek letter Omega hadn't encountered the standard QWERTY keyboard - another irritation.

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 12:40 pm

Brandon92 wrote:
Tue Feb 12, 2019 6:38 am
Yes, physical the current flow from - to +. But we dicided to make it goes from + to -. To make it easier for or self. (Easier to calculate for example, you don't need to thing much about the - sing.)

Anyway, this explanation we use the convention way.

So, let's assume that a F1 car is the current and that if goes around a oval race track. Where the start and finish is the echo. And R1 is one big turn of the circuit and R2 is the other one.
When the echo is enabled the F1 cars are beginning to race and everything is happy. Because they can make all the laps they want. So, now we cut the whole R1 turn. This means that the F1 cars can not complete the circuit any more. And they can not move to the finish any more.
So, if they are not moving through the R1 turn. There is also no current through the resistor R2, and no current means that there is also no voltage across that resistor.

So the current want to make always a full circuit. And if you make the R1 resistor a wire, what has a very low resistance. The total resistance has changed and the 6.25mA is not true any more. Try to calculate this :)
Thanks for the reply. Yeah, it would be ~ 0,011 A with only R2 (470 Ohm) and accordint to the Kirchhoff law the resistor R1 woudln't even reduce the voltage, or what? U would be I * R, so ~0,11 A * 47 Ohm, thus ~5 V.

So in other words, if you don't use two resistors, the voltage won't change at all and that's because of the Kirchhoff law?!

I try to explain electricity to myself with the water flow example. So in that specific case, this example would not be analog to electricity?

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 12:46 pm

Burngate wrote:
Tue Feb 12, 2019 12:09 pm
cloudapplepi wrote:
Mon Feb 11, 2019 10:56 pm
This convention to assume that the current goes from - to + when it physically the other way around is something I don't really understand. :?
Blame the 18th & 19th century physicists for this.

They could see strange things happening when they connected differing metals, with chemicals between them, or when they moved magnets inside coils.
There appeared to be a flow of something, but they hadn't a clue what that something could be, or which direction it was going.
Nor did there appear to be any way to find out.

So they made a random choice - which turned out to be wrong.
A bit like trying to convert the whole world to driving on the sensible side of roads, assigning the charge on the electron a positive value would be possible, but is unlikely to happen.

So, indeed, in your diagram, electrons set off from the ground pin, go through your two resistors, and into the Echo pin - from where they're sent through the 5v pin to the power supply which pushes them to the ground pin, again.

How many electrons go round that loop per second is determined by the resistance of the whole loop - lower resistance means more electrons/second.
Since the charge on the electron is rather small, and so the number of electrons would be astronomical, we tend to talk about the total charge rather than the number of electrons, calling them Coulombs (after the bloke of that name, 1736 - 1806, another of those pesky early physicists) and then promptly ignore him, in favour of the Amp (Andre-Marie Ampere, 1775 - 1836) which is just Coulombs/second.

So, to make the voltage (Alessandro Giuseppe Antonio Anastasio Volta, 1745 - 1827) across the 470Ω resistor R2 about 3v, we need 6¼ mA to flow through it.
For that to happen, the total resistance in the loop has to be about 800Ω. Hence R1.

And just to fill out the dates, Georg Simon Ohm 1789 – 1854. Whoever decided to use the Greek letter Omega hadn't encountered the standard QWERTY keyboard - another irritation.
Thanks for your reply. Getting into physics/electricity it seems to me very illogical to not adapt the schemes to physical reality. Physics as subject has the reputation to be logical, but then it is so illogical in this, only because it would be too much effort to adapt. I don't bother so much about the Greek letters, but this logical issue is difficult to understand from the point of view of someone who has an arts degree. Anyway, that's another topic.

What I still don't grasp is why there can't be just one resistance (R2) unless it so as I mentioned in my post above that the Kirchhoff Law just says that one resistance is basically as good as having no resistance regarding the voltage!?

procount
Posts: 1535
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 1:00 pm

@cloudapplepi - I'll have a go at trying to explain, because I think Brandon mis-interpreted your "removal of R1" suggestion as to have no connection (open-circuit), whereas I think you were intending to replace it with a straight wire (short-circuit) ....?
So I'll assume this was your thought process.

I like to use a plumbing analogy to explain electricity (it breaks down in some places but it helps to get the point across),

Try to image the electrical circuit as a central heating system, where the battery is the boiler, the wires are pipes and the resistors are radiators.
In your diagram the ultrasonic sensor represents the boiler, with the echo pin providing hot water, and the cold water from the radiators is returned to the ground pin.

When the sensor detects something, the boiler turns on to produce the hot water which is pushed out of the echo pin and through the R1 "radiator". When it gets to the pin 18 connector, it has a choice - does it go through radiator R2 and back to the ground pin to complete the circuit, or does it go into pin 18?
This choice is decided by the resistance of each pathway. If each path was of equal resistance, you would find half the water goes through radiator R2 and half would go through pin 18.
Suppose you started to close down the valve on the radiator. This restricts the flow of water by increasing the "resistance" of the radiator. The water would find it harder to go that route, so more water would go though pin 18 instead.Conversely, if you open the radiator valve, its resistance is less and more water would flow through it and less through pin 18.
So what is the resistance of Pin 18? Brandon already gave you the answer that it is very high (infinite maybe), like a very, very narrow pipe. So how much water will flow though it? Hardly any. So you can assume all of the water is flowing through Radiator R2.

Suppose we close the valve completely on Radiator R2. It now has infinite resistance and no water will flow. No water can flow through pin18 because it is also infinite resistance. So no water can flow through Radiator R1 either, because it has no where to flow to. So what happens?
Well, the boiler is still on and trying to push water out of the echo pin and pressure is building up in your pipework.

How much pressure is pin 18 feeling? All of the boiler pressure! So you are about to kill it.

Let's try and bring this analogy back to electricity.
The flow of water is the same as the electrical current. The pressure of the water is like the voltage.
When the resistance R2 is very high, there is no current flowing in the circuit, but there is a lot of voltage (pressure) at pin 18, in fact it is the full 5v, more than the 3V3 that Pin18 can handle.

In your first post you assumed the resistor could reduce the voltage on its own. But it can't. Current needs to flow through the resistor for the voltage to drop.
It's all to do with Ohm's law that states V=IR : The voltage(V) developed across a resistor(R) is proportional to the current (I) flowing though it.

So if there is no current flowing though R1, it produces no voltage across it and the voltage at pin 18 is the voltage at the echo pin minus the voltage across R1. 5V -0V = 5V

So you need current to flow through R1 and you do that by having R2 in the circuit to allow the "water" to flow back to the "boiler"

How much current is flowing?
Well, we know from Ohm's law that V=IR. The voltage (V) at the echo pin is 5V. The resistance is (R1+R2), so the current I=V/R = 5/(R1+R2)
In your example I = 5V(330+470) = 5V/800 = 6.25mA

Now we can calculate the voltage developed across R2 alone.
So using V=IR again, V= 6.25mA * R2 = 6.25mA * 470 ohms = 2.9V

So back to your question about removing R1.
If R1 is removed and no wire replaces it, then the water from the boiler will spill onto the floor and ruin your kitchen! :) In this case you don't have a complete electrical circuit and nothing will work (like Brandon's answer).

But suppose you replace it with a wire of zero resistance.
Let's go through the calculation again. How much current is flowing through the circuit now?
Using V=IR, The 5V at the echo pin = current x the resistance R2
5V= I x 470, so I=10.63mA. So more current is flowing now.

What is the voltage at pin 18?
It's the same as the voltage across R2.
So V=IR = 10.63mA x 470 ohms = 5V.
Looking at it another way, there is no resistance between the echo pin and pin 18 to lower the voltage from 5V.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 2:32 pm

procount wrote:
Tue Feb 12, 2019 1:00 pm
@cloudapplepi - I'll have a go at trying to explain, because I think Brandon mis-interpreted your "removal of R1" suggestion as to have no connection (open-circuit), whereas I think you were intending to replace it with a straight wire (short-circuit) ....?
So I'll assume this was your thought process.

I like to use a plumbing analogy to explain electricity (it breaks down in some places but it helps to get the point across),

Try to image the electrical circuit as a central heating system, where the battery is the boiler, the wires are pipes and the resistors are radiators.
In your diagram the ultrasonic sensor represents the boiler, with the echo pin providing hot water, and the cold water from the radiators is returned to the ground pin.

When the sensor detects something, the boiler turns on to produce the hot water which is pushed out of the echo pin and through the R1 "radiator". When it gets to the pin 18 connector, it has a choice - does it go through radiator R2 and back to the ground pin to complete the circuit, or does it go into pin 18?
This choice is decided by the resistance of each pathway. If each path was of equal resistance, you would find half the water goes through radiator R2 and half would go through pin 18.
Suppose you started to close down the valve on the radiator. This restricts the flow of water by increasing the "resistance" of the radiator. The water would find it harder to go that route, so more water would go though pin 18 instead.Conversely, if you open the radiator valve, its resistance is less and more water would flow through it and less through pin 18.
So what is the resistance of Pin 18? Brandon already gave you the answer that it is very high (infinite maybe), like a very, very narrow pipe. So how much water will flow though it? Hardly any. So you can assume all of the water is flowing through Radiator R2.

Suppose we close the valve completely on Radiator R2. It now has infinite resistance and no water will flow. No water can flow through pin18 because it is also infinite resistance. So no water can flow through Radiator R1 either, because it has no where to flow to. So what happens?
Well, the boiler is still on and trying to push water out of the echo pin and pressure is building up in your pipework.

How much pressure is pin 18 feeling? All of the boiler pressure! So you are about to kill it.

Let's try and bring this analogy back to electricity.
The flow of water is the same as the electrical current. The pressure of the water is like the voltage.
When the resistance R2 is very high, there is no current flowing in the circuit, but there is a lot of voltage (pressure) at pin 18, in fact it is the full 5v, more than the 3V3 that Pin18 can handle.

In your first post you assumed the resistor could reduce the voltage on its own. But it can't. Current needs to flow through the resistor for the voltage to drop.
It's all to do with Ohm's law that states V=IR : The voltage(V) developed across a resistor(R) is proportional to the current (I) flowing though it.

So if there is no current flowing though R1, it produces no voltage across it and the voltage at pin 18 is the voltage at the echo pin minus the voltage across R1. 5V -0V = 5V

So you need current to flow through R1 and you do that by having R2 in the circuit to allow the "water" to flow back to the "boiler"

How much current is flowing?
Well, we know from Ohm's law that V=IR. The voltage (V) at the echo pin is 5V. The resistance is (R1+R2), so the current I=V/R = 5/(R1+R2)
In your example I = 5V(330+470) = 5V/800 = 6.25mA

Now we can calculate the voltage developed across R2 alone.
So using V=IR again, V= 6.25mA * R2 = 6.25mA * 470 ohms = 2.9V

So back to your question about removing R1.
If R1 is removed and no wire replaces it, then the water from the boiler will spill onto the floor and ruin your kitchen! :) In this case you don't have a complete electrical circuit and nothing will work (like Brandon's answer).

But suppose you replace it with a wire of zero resistance.
Let's go through the calculation again. How much current is flowing through the circuit now?
Using V=IR, The 5V at the echo pin = current x the resistance R2
5V= I x 470, so I=10.63mA. So more current is flowing now.

What is the voltage at pin 18?
It's the same as the voltage across R2.
So V=IR = 10.63mA x 470 ohms = 5V.
Looking at it another way, there is no resistance between the echo pin and pin 18 to lower the voltage from 5V.

Thanks for the long and good explanation with the radiator system. Yes, I meant to replace R1 by just a wire.

So basically I would need two resistors (R1 and R2 in this example), because only having one resistor (R2 or R1) is not changing the voltage, it would stay at 5V in the whole circuit. And this is due to Ohm's laws (I accidently wrote Kirchhoffsche Laws in my former post)?! :?:

So I basically just would need to "accept" that the Ohm's law apply, as one resistance alone doesn't change the 5V at all? Somehow this is "funny" or rather difficult to grasp that one resistance doesn't change anything. I once put a resistance "in front" of a LED so that it doesn't break, but I guess the LED is considered as resistance, too!? :?:

When thinking about actually building this circuit and connecting the ultra sound device to the Raspberry Pi, can I connect (solder) it as shown in the picture below (sorry, just a simple paint drawing)? :?:

Regarding the Ohm values of the resistances: I guess I can use also other resistors with other values, as long as I don't get more than 3.3V (or play safe with 2.9 - 3.0 V) on the Pin 18? Is there a minimum voltage that I need to have at pin 18? :?:

And thanks again to you and everyone else for taking the time to explain this to me. I appreciate it very much.

Here the picture:
Image

pcmanbob
Posts: 5494
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 2:59 pm

With the 3.3v gpio any voltage above 1.8 - 2.0V should be seen as a high input, when assembling a potential divider using resistors I normally aim for 2.9V on the gpio pin , this allows a little leeway for the resistor tolerances, because no resistor is ever its stated value.

If you want to see how changing your resistor values will change the output voltage have a look at these sites, handy if you have some existing resistor and want to see if you have some suitable values.

https://www.electronics2000.co.uk/calc/ ... ulator.php

https://www.random-science-tools.com/el ... ivider.htm
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

procount
Posts: 1535
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Ultrasonic sensor and voltage division

Tue Feb 12, 2019 3:43 pm

Well, I hope my ramblings clarified a few points for you.
cloudapplepi wrote:
Tue Feb 12, 2019 2:32 pm
as one resistance alone doesn't change the 5V at all?
Be careful how you interpret what I said in relation to your simple circuit.

The one resistor R1 connected between Echo and PIn 18 wouldn't change the voltage ONLY BECAUSE there is no current flowing in it because pin18 is such a high resistance. As soon as current flows through the resistor, a voltage will develop across it and the voltage at pin 18 will be the 5v at the echo pin minus the voltage developed across the resistor (IR). Use the webpage that pcmanbob linked to to see how varying R2 will affect the voltage at pin 18.

The simple equation for this voltage divider is: V_pin18 = Vecho X R2 / (R1+R2)

Note that resistors come in specific standard values (like 330 and 470), so you can't always pick the exact value you need. You might have to manufacture a value by putting resistors in series or parallel to get what you actually need. But the combined value of parallel resistors is another story.

There is also a balancing act to be done between the voltage you want and the current you want.
With the values of 330 & 470 Ohms you have chosen, Vpin18 will be 2.9V and the current flowing is 6.25mA.
You could also have chosen 3.3K and 4.7K to give the same voltage, but the current though the voltage divider would be 0.625mA (ie. 10x smaller)

In your LED example, you are not using the resistor as a voltage divider, but just as an essential current limiter.
If you connect the resistor between the positive of an (imaginary) 5V battery and the LED and leave the other side of the LED disconnected, then, (obviously) no current will flow in the resistor because the LED looks like an infinite resistance (like your pin18). The voltage at the LED will be the same as the battery. But when you connect the other side of the LED to the negative side of the battery, current will flow through the LED and in the resistor and cause a voltage drop across the resistor. LEDs are not like resistors because they are non-linear. Consider that they have no resistance, but have a fixed voltage drop across them. (Let's say 0.2V for simplicity).

So you can calculate the current flowing through the resistor using V=IR again.
Assuming some random figures of battery = 5V, LED voltage drop = 0.2V, resistor = 470 Ohms,
then V=IR
(5V-0.2V) = I x 470 ohms
I = 4.8V/470 ohms
I = 10mA which is probably enough to let the LED glow safely but dimly. (LEDs will consume as much current as they can, but if they exceed their current rating they tend to die, hence the need for the current limiting resistor).

In answer to your other questions, yes you can solder it as you have painted.

Every logic pin has a minimum "turn-on" and a maximum "turn-off" voltage value and this will be specified in the data sheet. Pcmanbob indicates for 3.3V systems, the minimum turn-on is typically around 1.8-2.0V, so resistors that give you a value between that and 3V should be ok.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 8:05 am

pcmanbob wrote:
Tue Feb 12, 2019 2:59 pm
With the 3.3v gpio any voltage above 1.8 - 2.0V should be seen as a high input, when assembling a potential divider using resistors I normally aim for 2.9V on the gpio pin , this allows a little leeway for the resistor tolerances, because no resistor is ever its stated value.

If you want to see how changing your resistor values will change the output voltage have a look at these sites, handy if you have some existing resistor and want to see if you have some suitable values.

https://www.electronics2000.co.uk/calc/ ... ulator.php

https://www.random-science-tools.com/el ... ivider.htm
Thanks. Very useful links, got to bookmark them.

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 8:12 am

procount wrote:
Tue Feb 12, 2019 3:43 pm
Well, I hope my ramblings clarified a few points for you.
cloudapplepi wrote:
Tue Feb 12, 2019 2:32 pm
as one resistance alone doesn't change the 5V at all?
Be careful how you interpret what I said in relation to your simple circuit.

The one resistor R1 connected between Echo and PIn 18 wouldn't change the voltage ONLY BECAUSE there is no current flowing in it because pin18 is such a high resistance. As soon as current flows through the resistor, a voltage will develop across it and the voltage at pin 18 will be the 5v at the echo pin minus the voltage developed across the resistor (IR). Use the webpage that pcmanbob linked to to see how varying R2 will affect the voltage at pin 18.

The simple equation for this voltage divider is: V_pin18 = Vecho X R2 / (R1+R2)

Note that resistors come in specific standard values (like 330 and 470), so you can't always pick the exact value you need. You might have to manufacture a value by putting resistors in series or parallel to get what you actually need. But the combined value of parallel resistors is another story.

There is also a balancing act to be done between the voltage you want and the current you want.
With the values of 330 & 470 Ohms you have chosen, Vpin18 will be 2.9V and the current flowing is 6.25mA.
You could also have chosen 3.3K and 4.7K to give the same voltage, but the current though the voltage divider would be 0.625mA (ie. 10x smaller)

In your LED example, you are not using the resistor as a voltage divider, but just as an essential current limiter.
If you connect the resistor between the positive of an (imaginary) 5V battery and the LED and leave the other side of the LED disconnected, then, (obviously) no current will flow in the resistor because the LED looks like an infinite resistance (like your pin18). The voltage at the LED will be the same as the battery. But when you connect the other side of the LED to the negative side of the battery, current will flow through the LED and in the resistor and cause a voltage drop across the resistor. LEDs are not like resistors because they are non-linear. Consider that they have no resistance, but have a fixed voltage drop across them. (Let's say 0.2V for simplicity).

So you can calculate the current flowing through the resistor using V=IR again.
Assuming some random figures of battery = 5V, LED voltage drop = 0.2V, resistor = 470 Ohms,
then V=IR
(5V-0.2V) = I x 470 ohms
I = 4.8V/470 ohms
I = 10mA which is probably enough to let the LED glow safely but dimly. (LEDs will consume as much current as they can, but if they exceed their current rating they tend to die, hence the need for the current limiting resistor).

In answer to your other questions, yes you can solder it as you have painted.

Every logic pin has a minimum "turn-on" and a maximum "turn-off" voltage value and this will be specified in the data sheet. Pcmanbob indicates for 3.3V systems, the minimum turn-on is typically around 1.8-2.0V, so resistors that give you a value between that and 3V should be ok.
Thanks again for the reply.

Yes, I forgot about pin 18.

The links from pcmanbob are very useful.

I'm not sure if I really understood the physical logic behind this (why to use 2 resistors or why one resistor, R2, doesn't change the voltage), but I guess it is, because of the Ohm's Law. When you calculate it, it turns out that there is 5 V at pin 18 when I use just one resistor, R2. I should probably build this circuit and measure it, to understand more how it physically works.

It's a bit dissatisfying to not understand the physical logic here, but I guess I can live with that and continue. :-)

Thanks again for all replies.

User avatar
rpdom
Posts: 13753
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 8:39 am

I don't know if this will help the understanding, but I'll have a go.

Imagine a longish metal rod. Put one end in a flame (5V) and the other end in a bucket of ice (0V). About 2/3 of the way along that rod from the cold end it will be quite warm (3.3V). That's where you connect to it.

The rod is like the two resistors put together. The join between the resistors is like that point 2/3 the way along the rod. By changing the resistor values you are changing where you feel the heat (which voltage you will get).

If you only have one resistor you will only have no middle point to connect to. You only have the hot end or cold end.
Signature is on holiday.

cloudapplepi
Posts: 17
Joined: Wed Dec 05, 2018 6:35 pm

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 9:06 am

rpdom wrote:
Wed Feb 13, 2019 8:39 am
I don't know if this will help the understanding, but I'll have a go.

Imagine a longish metal rod. Put one end in a flame (5V) and the other end in a bucket of ice (0V). About 2/3 of the way along that rod from the cold end it will be quite warm (3.3V). That's where you connect to it.

The rod is like the two resistors put together. The join between the resistors is like that point 2/3 the way along the rod. By changing the resistor values you are changing where you feel the heat (which voltage you will get).

If you only have one resistor you will only have no middle point to connect to. You only have the hot end or cold end.
Thanks. That's a good explanation. :-)

I think I need to think about this more and maybe re-read some things at the weekend or so. But I think I mostly got it now.

procount
Posts: 1535
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 9:33 am

@rpdom - good one! I was about to go for a ladder analogy, but I think yours was better (and shorter!)

@Cloudapplepi - I hope you get it. Have a good re-read of this thread over the weekend and let it all sink in. It's all there once you get the hang of it.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

User avatar
Burngate
Posts: 5780
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 10:23 am

All this about R2 on its own not changing the voltage - but no-one has said it's because the 5v supply is a source of constant voltage
It's designed to keep its terminals 5v apart whatever you do (within reason) and the Echo pin just gets connected to it when necessary

You can get constant current sources, but they're less common.
You could also design a supply of constant power.

But almost every power supply you can buy at least purports to be constant voltage.
Good ones keep the voltage constant over a greater range of output current; bad ones don't

User avatar
rpdom
Posts: 13753
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Ultrasonic sensor and voltage division

Wed Feb 13, 2019 11:15 am

procount wrote:
Wed Feb 13, 2019 9:33 am
@rpdom - good one! I was about to go for a ladder analogy, but I think yours was better (and shorter!)
A ladder analogy would have worked well. After all, you do get resistor ladders made from several resistors in series giving many different points where you can tap off different voltages.
Signature is on holiday.

JohnsUPS
Posts: 76
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Ultrasonic sensor and voltage division

Thu Feb 21, 2019 12:21 pm

Another way to look at the situation of voltage division is to consider a potentiometer.
The construction is that of a resistive element with a sliding tap. Using a 10k pot for an example, set the pot halfway, and you have, in effect, a circuit with two 5k resistors in series with the tap at your halfway point.
Taking this one step further, using the 10k pot again, you could apply 5V across the pot, and measuring from ground to the tap, adjust it for 3.3V at the tap/slider. Remove the power, and measure the resistances, and you will see that the resistance ratio falls very close to what has been calculated in previous posts. Also, if you want 3.3V exactly and not have to deal with resistance tolerances, the pot could be left in place. Just don't adjust it after the fact :( or you may wind up with a dead GPIO pin.

Return to “Automation, sensing and robotics”