4

In my Raspberry Pi project, I need to measure a distance. To do that, I have an ultrasound sensor called "AJ-SR04M" (it is basically a waterproof HC-SR04 I believe).

Characteristics of the AJ-SR04M:

  • Supply voltage: 5 V
  • Working current: 30 mA
  • Max. detection distance: 4.5 m
  • Resolution: 0.5 cm
  • Blind distance: 25 cm

As I already have other sensors (including a camera module) connected to the Pi, I fear the Pi might not be able to source the current required by the ultrasound sensor. That's why I purchased an external power supply board, the "Power MB v2".

Here is how I want to wire everything: wiring sketch

Picture of the AJ-SR04M sensor:

Picture of the AJ-SR04M sensor

Description of the wiring:

  • Sensor's VCC is connected to the power supply board 5V output
  • Sensor's TRIG is connected to the Pi GPIO 26 (output mode)
  • Sensor's ECHO is connected to a voltage divider and then to the Pi GPIO 19 (input mode)
  • Sensor's GND is wired with one of the Pi GND PINs and the power supply board GND

I have two questions:

  1. Is it okay to wire together the GND of all the devices as I did above? Will it work as expected?
  2. Since the Raspberry Pi GPIO PINs work with 3.3V, I had to build a voltage divider to convert the 5V output of the sensor to 3.3V. Assuming the left-most resistor on the breadboard is 2K ohm and the right-most resistor is 1K ohm, did I do right?
2
  • 3
    Welcome to Raspberry Pi SE. I've been prompted by the system to review your question, and suggest changes for improvement. You've done a good job articulating your question - it's well-written and clear. My only suggestion is that you consider using the schematic tool instead of wiring graphics to show your wiring. It's actually not essential in this case, as your wiring is fairly clear, but please know that the tool is available, and that it will have distinct advantages in some cases.
    – Seamus
    Sep 15, 2018 at 13:47
  • @Seamus Thank you for the review! I didn't notice the schematic tool, I will use it next time.
    – GuiTeK
    Sep 15, 2018 at 14:21

3 Answers 3

4

Everything looks fine.

Yes, you need to connect the Pi ground to the ground of any sensors you connect to the GPIO. It doesn't matter if they are powered by the Pi or by an external power source. You need a common voltage reference so both ends know what's high and what's low.

The resistor divider is fine. There are many examples on-line. This is probably as good as any.

For a 5V source it might be more convenient to use a pair of resistors of the same value. That will drop the voltage to 2.5V which will still be seen as high by the Pi. There is also the advantage that you can't put the resistors the wrong way around.

4
  • Thank you for your answer. Good to know for the resistors! I will use a pair of resistors instead then. Can I pick any pair of resistors with the same value (1k, 2K...)?
    – GuiTeK
    Sep 15, 2018 at 14:23
  • I don’t think the divider to 2.5V is a good idea. It’s true that the Raspberry considers everything above 2V as a logic high. However, if the 5V source is actually outputting 4.8V and the sensor is dropping another 200mV you are down to 4.6V/2= 2.3V. Add some long wires with resistive losses and interference and you could have issues. Totally okay for a quick test on a breadboard, wouldn’t recommend for a permanent solution.
    – Michael
    Sep 15, 2018 at 16:53
  • 1
    @Michael I agree. For a permanent solution I would use a 1 to 2 ratio.
    – joan
    Sep 15, 2018 at 17:04
  • 1
    @GuiTeK. Any pair of 1k or 2k etc. should work. I'm not sure how high you could ago. A pair of 10k would be fine, a pair of 100k perhaps not. As Michael says use the proper ratio if possible. If you only have one value of resistors (e.g. a pack of 5k) you could get the 1 to 2 ratio using 3 resistors (1 singly and 2 in series).
    – joan
    Sep 15, 2018 at 17:08
5
  1. Yes, (in general) in this case you should wire the ground terminal of all devices in your circuit together. This provides the necessary common ground reference voltage for them to interoperate.

  2. Your voltage divider needs to go from 5 vdc to 3.3 vdc. There are two considerations in setting up a voltage divider:

a. it provides the proper voltage, and

b. its impedance doesn't interfere with proper circuit function of the interface

Here's a schematic representation of your voltage divider:

schematic

simulate this circuit – Schematic created using CircuitLab

First, we see that the output of your voltage divider is 3.33 volts, and that is "close enough" to the GPIO rating of 3.3 volts.

Second, we see that the impedance of your voltage divider will not interfere with proper circuit function. This due to the fact that the effective resistance of the RPi's GPIO input is far greater than the impedance of your voltage divider, and therefore will not place a significant additional current load on the circuit.

And so, the answer to your second question is, "Yes, you did it right."

EDIT - AN ALTERNATIVE TO THE VOLTAGE DIVIDER

I'm not changing anything in the answer above, but I wanted to add another solution - an alternative to a voltage divider. Simply replace the shunt resistor (R1 in the schematic above) with a 3.3V Zener Diode. as shown in the schematic below. The motivation for using a Zener instead of a voltage divider is that it will provide a more stable 3.3V input voltage to the RPi. Specifically:

  • In situations where the external voltage is unknown, or fluctuates over a wide range, the Zener diode will hold the input voltage to the GPIO pin at a steady 3.3V. Here's an example: Assume for a moment that something went wrong, and the external voltage was 9V instead of 5V. In this case, the voltage divider would put 6V at the RPi GPIO pin, possibly damaging the RPi. However, the same 9V external voltage applied to the Zener circuit below would continue to present 3.3V to the RPi GPIO pin.

  • While the GPIO pins on the RPi have a high input impedance, this is not true of all inputs. Zeners offer an advantage in interfacing with such low impedance circuitry because their output voltage is unaffected (within reasonable limits) by current flowing through the series resistor (R2 above).

  • A bit obscure, but some sensors behave more as a current source than a voltage source. Some circuit examples of current sources. The Zener circuit below will keep the voltage at the GPIO pin of the RPi at 3.3V over a wide range of current.

And so, Zener diodes offer advantages over a voltage divider. Zener diodes are also inexpensive and plentiful; here's a list of 3.3 V Zeners from a vendor I use. (Disclaimer: I have no affiliation w/ Mouser)

Here's a schematic showing use of a Zener diode instead of the voltage divider:

schematic

simulate this circuit

The 1N5226 Zener is rated at 0.5 watts. If we allow it to dissipate 0.1 watts, this circuit will provide 3.3V at the input to the RPi's GPIO pin for Vin up to 90V.

Note however that while a 90V input won't over-stress the Zener in this circuit, it will create a voltage drop of 86.7V across R2, and R2 will need to be sized to dissipate approx 1.6W.

1

did I do right? - NO

The values suggested (1kΩ/2kΩ) may be mathematically correct (assuming you could ACTUALLY get 2kΩ resistors) BUT ignore the reality of circuit design which needs to consider the tolerance of the external voltage, the resistor values and the input characteristics of the Pi.

An Engineer would design a circuit to provide ≥ 1.8V < 3.3V given the worst case tolerances - as suggested equal resistors would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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