raspberry pi

Ultrasonic Sensor with Raspberry pi, interfacing and Python Code

Ultrasonic sensor with Raspberry Pi, Overview:

Ultrasonic sensors can measure distances using sound waves. The type of Ultrasonic Sensor I will be using with the Raspberry Pi is the HCSR04 which is the most popular one. This one is most frequently use with Arduino boards for measuring the distance, is used in obstacle avoidance robots, Water level monitoring systems, and so on, you can check my category on Arduino Projects. I have been using this type of Ultrasonic sensor in different types of Arduino based projects. This time I am going to use this Ultrasonic Sensor with Raspberry Pi.

Amazon Purchase Links:

HC-SR04 Ultrasonic Sensor:

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other components and tools:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

A good keyword when looking for other sources of supply is HCSR04 (see Figure 1). There are two pots on this module the ultrasonic transmitter and the receiver. The working principle of these sensors is simple: the transmitter sends an ultrasonic pulse. The recipient is waiting that the impulse is reflected from an object and comes back.

Ultrasonic Sensor with Raspberry Pi
Figure 1: The ultrasonic module HCSR04

The control of the sensor and the processing of the signals are also very good simply solved. The modules have four connections:

  • VCC: supply voltage 5 V
  • Trigger: trigger
  • Echo: feedback from the sensor
  • GND: ground

If a high pulse is detected on the trigger pin, the sensor sends an ultrasonic signal in the nearby areas. Almost at the same time, a high signal can be read back from Echo will. The pin echo now delivers a high signal until the reflected sound was received. At this moment the echo output changes back to low. In Your software now only needs to change the time difference between the increasing and the falling edge of the echo pin.

Before we get to the software, however, the sensor should be connected to the Raspberry Pi get connected. In the connection description you can see that VCC is 5 V. should. This is the optimal operating voltage of the sensor with which to measure the distance of up to 450 cm should be possible. Now comes the highlight of the matter. If you apply VCC to 5 V, the echo pin also delivers 5 V signals. As you should already know, voltages greater than 3.3V are present on the It is essential to avoid GPIO pins. You now have two options:

  • They supply the sensor with 3.3 V and so lose a lot of range. The maximum range is now only approx. 50 cm.
  • You supply the sensor with 5 V and use a simple one on the echo pin Voltage divider. So you get the full range of the sensor, but you need it two external resistors (see Figure2). Pins 11 and 13 can be used against any free GPIO pin can be exchanged.
Ultrasonic Sensor with Raspberry Pi
Figure 2: Connection of the ultrasonic sensor to 5 V with a Voltage divider for the echo signal

Ultrasonic Sensor Python Programming:

The following Python program takes a distance measurement twice per second carried out. The actual determination of the values ​​happens in the while-Loop. The trigger is activated for 10 μs. This is the signal for the sensor to emit the sound pulse. Between the trigger signal and the positive one There is a short delay on the edge of the echo signal. This delay is only a fraction of a second, but is made possible by the following while loop considered: As soon as the echo signal changes to high, the start time is determined.

The negative edge of the echo signal is recognized in the last while loop, and the stop time is then determined. Subtract the start point from the end point, so you have the time it takes for the sound wave to go there and back needed. Now multiply this time by the speed of sound, the in air is 343 m/s. So that you can output the values ​​in cm, there is another Multiplication by 100 necessary. This results in 34,300 cm/s. Last but not least divide everything by 2 to get only one distance.

Problems in practice:

Our experiments showed that the event or wait_for_edge functions in Python not called fast enough. Especially with short distances Edge not recognized. The manufacturer of the sensor gives an accuracy of 0.3 cm. In the practical test but the measured distance is approx. 1 cm from the real distance. An emergency Solution offers an offset value here. Determine the deviation from the real distance, and add this to the variable distance.


Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button