Using a Raspberry Pi distance sensor (ultrasonic sensor HC-SR04)

For many (outdoor) projects a distance measurement is necessary or advantageous. These small modules are available starting at 1-2 bucks and can measure the distance up to 4-5 meters by ultrasound and are suprisingly accurate. This tutorial shows the connection and control.

 

Hardware

  • HC-SR04 Module (US / UK)
  • Resistors: 330Ω and 470Ω (US / UK)
  • Jumper wire (US / UK)

Wiring

There are four pins on the ultrasound module that are connected to the Raspberry:

  • VCC to Pin 2 (VCC)
  • GND to Pin 6 (GND)
  • TRIG to Pin 12 (GPIO18)
  • connect the 330Ω resistor to ECHO.  On its end you connect it to Pin 18 (GPIO24) and through a 470Ω resistor you connect it also to Pin6 (GND).

We do this because the GPIO pins only tolerate maximal 3.3V. The connection to GND is to have a obvious signal on GPIO24. If no pulse is sent, the signal is 0 (through the connection with GND), else it is 1. If there would be no connection to GND, the input would be undefined if no signal is sent (randomly 0 or 1), so ambiguous.

Here is the structure as a circuit diagram:

ultraschall_Steckplatine
 

Script for controlling

First of all, the Python GPIO library should be installed

To use the module, we create a new script

sudo nano ultrasonic_distance.py

with the following content:

After that we run:

sudo python ultrasonic_distance.py

So every second, the distance will be measured until the script is cancelled by pressing CTRL + C.
That‘s it. You can use it many fields, but who still want to measure larger distances would have to rely on laser measuring devices, which, however, are much more expensive.

45 Responses

  1. Thank you, I need this for a Magic Mirror. I want it to activate when someone is nearby, in front.
    I have experimented with PIR, I always seem to find them a bit random but this should be good.
    I found other examples of “how to do this”. This one, yours, worked immediately.

    OK, so it is not my solution and I need to do much more to make my exact application work but “Thank You” so much for this proof of concept.

    Reply
  2. Thank you for making it so easy to get playing with this sensor.

    I only had 1k and 2.2k resistors available. Would that be the reason my measurements are off by about 1.7cm on the distances (0.5 – 2 meters) I tested?

    My understanding is that a higher resistor will merely pull the voltage even lower than the target 3.3V needed to make the echo response safe for the pi’s GPIO pins.

    Thanks again

    Reply
    • Might be too late, but for future readers… it actually doesn’t make a difference. As long as the pi recognizes the signal as a high, it works. The only two possible outcomes are: 1. the pi doesn’t receive the high signal and your program either handles this, crashes, or runs forever because it’s waiting to receive a high or 2. the pi recognizes the signal as a high and therefore no error due to the pi/wiring/sensor. You could create a calibration curve for your sensor if you want the distances to be perfect. Also don’t forget that you’re doing the computation for the distance, so the speed of sound through air at the ambient temperature must be set properly for your ambient temperature.

      Reply
      • Hey, I have problem where when I read the distance measurements on the sensor in loop, it reads 4-5 measurements and then it freezes on the terminal for a few seconds and then it starts reading in measurements again. I’m not sure what the problem do this issue is and how to fix this, I need the sensor to continuously measure the distance without pausing

  3. I used this code and wired the circuit correctly with the 330 and 470 Ohm resistors but am getting values of over 100,000 cm even though my sensor is only 1 meter from a wall. Any ideas?

    Reply
    • (also, the values are fluctuating quite a bit even with the sensor stationary; it’s not just off by some factor of 10…)

      Reply

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!