I have built several remote controlled cars and robots with a Raspberry Pi computer and now I want to equip them not only with a camera but also with a HC-SR04 ultrasonic distance sensor. An ultrasonic distance sensor is ideal for this purpose as it is cheap and easy to install. For my robot car project I decided to use the HC-SR04 ultrasonic distance sensor. It is small in size and available in many robot online shops for a few euros. With this sensor it is possible to measure distances between 2cm and 4m with a theoretical resolution of 3mm. Depending on the echo thrown back, the resolution may vary. Soft obstacles such as a curtain are not easy to detect because they do not reflect the ultrasonic signal well. If you drive with your robot in a flat angle towards a wall, the ultrasonic signal is reflected away from the receiver and not towards the sensor and so a correct distance measurement is difficult.
The sensor can easily be connected to an Arduino. If an SBC like a Raspberry Pi comes to the Einstaz, a level shifter is needed depending on the voltage of the GPIO’s and the sensor. The HC-SR04 ultrasonic distance sensor requires 5V as supply voltage. The current amplifiers required by the sensor are at low 2mA. This makes the sensor ideal for small robot projects.
The following picture shows the HC-SR04 ultrasonic distance sensor.
The picture shows the HC-SR04 sensor and the four contacts are clearly visible. The first pin is the Vcc for the 5V power supply. The second pin labeled Trig is the trigger pin to activate the sensor. This pin tells the sensor to send the ultrasonic signal for a short time. The third pin labeled Echo sends a HIGH signal back when it has received the ultrasonic sound again. The fourth pin Gnd is the connection for grounding.
Function of the HC-SR04 Ultrasonic Sensor
For the sensor to send an active ultrasonic signal, a HIGH signal of 10us seconds must be applied to the Trig Pin. The sensor then automatically sends out a 40khz square wave signal and receives the return echo. When a signal comes back the Echo Pin is set to HIGH. The time that elapses between sending the signal, receiving the echo and immediately setting the echo pin to HIGH is the basis for the subsequent calculation of the distance.
((Start Time (s) – Recieving Time (s)) * 340 m/s)/2
With the HC-SR04 sensor it is possible to take 50 measurements per second. The interval for a measurement is 20us. The time interval is sufficient for a small robot car to steer it and detect obstacles in front of the robot.
Video of the HC-SR04 sensor in action
I have written a small program to control two DC motors independently of each other via the HC-SR04 ultrasonic distance sensor. The electric motors rotate at a pre-set basic speed as long as an obstacle is 50cm away. If, as shown in the video, you move your hand in front of the sensor and approach it, the speed of the motors will decrease until they come to a standstill.
HC-SR04 demo program
The small program can be downloaded here. It consists of the distance measurement and the program to control the motors. No motor driver l298n and motor is needed to run the program. The program shows the distance even without a motor in the terminal window. At the beginning of the program it is possible to set the GPIO pins for the HC-SR04 ultrasonic distance sensor and the motor driver.
Download: Ultrasonic sensor program HC-SR04
With the appropriate technology and understanding of the Raspberry Pi, it is possible to realize great projects with an ultrasonic sensor. But it is important to point out the different tensions. The Raspberry Pi computer operates at 3.3V and the HC-SR04 sensor at 5.0V. Therefore a level shifter must be installed between the Raspberry Pi and the ultrasonic sensor to avoid damaging the Raspberry Pi computer.