2
\$\begingroup\$

I'm getting into electronics (so my terminology may be a little wonky in places), and I'm trying to create a simple setup using a Raspberry Pi 4B, a GPIO extension board, a HC-SR04 sonar sensor, and an LED.

To wire up the sonar, the kit I'm using supplies this diagram:

enter image description here

Most of this makes sense, except for the need for the resistors, and the connection of Echo to GND.

Despite the sensor running using 5V, I'm guessing R2 is to protect GPIO24, since it takes 3v3?

My main question is about R1 and R3, and why Echo is connected to ground at all. My first thought was that this would cause the signal sent from Echo to be lost to ground, but then I realized that the total resistance going to ground is higher than that of the direct path to GPIO24, so most of the electricity will travel along the lower resistance path to the GPIO pin, and some will go to ground.

Is this simply "bleeding off" extra voltage into ground?

\$\endgroup\$
7
  • 5
    \$\begingroup\$ Are you familiar with a voltage divider? \$\endgroup\$ – Hearth Dec 27 '20 at 19:08
  • 2
    \$\begingroup\$ What's the maximum allowed I/O input voltage of your raspberry pi? \$\endgroup\$ – Chris Stratton Dec 27 '20 at 19:09
  • 1
    \$\begingroup\$ @Hearth Nope, but I'll look into that. \$\endgroup\$ – Carcigenicate Dec 27 '20 at 19:11
  • 3
    \$\begingroup\$ Ah: "A voltage divider is a simple circuit which turns a large voltage into a smaller one. Using just two series resistors and an input voltage, we can create an output voltage that is a fraction of the input.". So, it's reducing the voltage given to the pin by bleeding off some into the ground. \$\endgroup\$ – Carcigenicate Dec 27 '20 at 19:12
  • 2
    \$\begingroup\$ @Carcigenicate If you learn what a voltage divider is, the purpose of these resistors will be obvious. \$\endgroup\$ – Hearth Dec 27 '20 at 19:13
14
\$\begingroup\$

R1, R2 and R3 form a voltage divider.

schematic

simulate this circuit – Schematic created using CircuitLab

When a 5V signal is applied to the top of the divider (the echo signal), ohm's law states that: $$\frac{5V}{3000\Omega} = 1.67 mA$$

Will flow trough the resistors to ground. This means there will be a voltage drop of: $$1.67mA * 1000\Omega = 1.67V $$ Across each resistor.

Since R1 and R3 are in series, their voltage drops add up. So there will be approximately 3.34V on the output to the raspberry pi.

The raspberry pi input has a very high input resistance so that it's effect on loading the divider can safely be ignored in this case.

\$\endgroup\$
2
  • \$\begingroup\$ The OP specified that they're using a raspberry pi, not an arduino. Where did you get the arduino bit? \$\endgroup\$ – Connor Wolf Dec 28 '20 at 5:36
  • \$\begingroup\$ @ConnorWolf My mistake, the concept is the same tough. Fixed. \$\endgroup\$ – Unimportant Dec 28 '20 at 20:11
3
\$\begingroup\$

That resistor circuit is know as a "voltage divider" as described here: https://en.wikipedia.org/wiki/Voltage_divider

The two resistors going to ground (R1 and R3) form a simple series circuit equivalent to a single 2Kohm resistor (with twice the power handling capacity of the 1K parts though that's probably not why they did it, likely it was reduce the number of different values needed).

R2 in series with the output will have a voltage drop across it induced by the current that is flowing into R1+R3. Ignoring any current flowing into the GPIO pin and any resistance internal to the sensor output (which are likely insignificant in this application), using Ohms law the voltage Vin at the GPIO pin can be expressed as:

Vin = Vecho * (R1 + R3) / (R1 + R2 + R3)

where Vin is the voltage at the GPIO input, and Vecho is the voltage at the sensor output.

Plugging in values of R1, R2 and R3, the voltage at the GPIO pin will be 2/3 of the voltage of the sensor output. The effective output impedance as seen from the GPIO pin will be R2 in parallel with (R1+R3) or:

Rout = 1 / (1/R2 + 1/(R1+R3)) or approximately 666.7 ohms.

So the "equivalent" circuit would be the same as if the sensor output was being driven from a (5V * 2/3) or 3.333V power rail, through a 666.7ohm resistor. It is essential the equivalent resistance here be be much less than the input resistance of the the GPIO pin to avoid the unwanted loading effects that we ignored earlier. Since the typical CMOS input resistance around a Megaohm that is satisfied.

Also, the loading on the ECHO output must be considered. Some logic families can't source much current, some as little at 2mA. The loading on the ECHO pin is the series sum of R1+R2+R3 or 3Kohm, so the current drain is at most 5V/3K = 1.667mA, likely within the spec of the output.

Modern IC's that are pushing the size of the transistors ever smaller can't handle as much voltage (and don't need as much voltage to operate). Although the Raspberry PI board is powered from 5V, the power rails used to power the processor IC are down-converted to lower voltages. The power rail for the GPIO of that IC cannot be more than 3.3V nominal, and I/O is not tolerant of 5V logic. Thus this voltage divider is a simple and cheap way to convert from 5V logic outputs to 3.3V logic inputs.

Tim

\$\endgroup\$
4
  • 4
    \$\begingroup\$ Re, "...twice the power handling capacity..." But that probably is not why they put two resistors in series. 1K is a very common value that the kit company probably buys in large quantities and uses in many different kits. 2K might be something that they only need for this one kit. Because of the premium that you have to pay for ordering small quantities of parts like that, they might have to pay a lot more for a 2K resistor than they would pay for two 1K resistors. \$\endgroup\$ – Solomon Slow Dec 27 '20 at 23:28
  • \$\begingroup\$ Two other possible reasons to add to Solomon Slow's comment: The kit supplier may be using the same circuit board design for several different kits. A different kit may need all three resistors, and something would be needed to bridge the unused space in this kit. Also, if these kits are marketed to inexperienced builders, supplying all the resistors with the same value means they can't be assembled in the wrong places and cause problems! \$\endgroup\$ – alephzero Dec 28 '20 at 18:38
  • \$\begingroup\$ hence my statement "likely it was reduce the number of different values needed". \$\endgroup\$ – Tim T Dec 29 '20 at 0:22
  • \$\begingroup\$ Would be helpful if the kit supplier explained the choice of 2 x 1K resistors in series (as an alternative to a 2K) in the diagram to avoid confusing inexperienced folks who may may be trying to interpret what "magic" is done by that configuration. \$\endgroup\$ – mhaselup Dec 29 '20 at 7:49
-1
\$\begingroup\$

it's an external pull down resistor to keep the signal from floating. it's possible to program a internal pull down resistor, as part of the program, but they're not that reliable. it's always advisable to physically wire in an external pull down resistor.

I've had experience with internal pull down resistors failing. I guess it depends on how much faith you put in a $20 computer.

please check out this tutorial from raspberry pi,

https://tutorials-raspberrypi.com/raspberry-pi-ultrasonic-sensor-hc-sr04/

\$\endgroup\$
-2
\$\begingroup\$

it's an external pull down resistor to keep the signal from floating. it's possible to program an internal pull down resistor, as part of the program, but they're not that reliable. it's always advisable to physically wire in an external pull down resistor.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is incorrect; it's being used as a voltage divider. And there's nothing unreliable about internal pull resistors, unless you're using microcontrollers from some shady source. \$\endgroup\$ – Hearth Dec 28 '20 at 23:56
  • \$\begingroup\$ I'm not aware of any microprocessors that have programmable internal pulldowns, they are always pullups in my experience. In addition, the internal resistors are not of precise tolerance and vary widely over temperature. Trying to use for a voltage divider would yield poor accuracy. Further, if it were not on from power-up or ever turned off, it could lead to damage. \$\endgroup\$ – Tim T Dec 29 '20 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.