Buchempfehlung:
Roboter-Autos mit dem Raspberry Pi

Roboter Autos mit dem Raspberry Pi

I build several robot cars and now I want to equip them not only with a camera module but also with a distance sensor. For this the ideal sensor is an ultrasonic sensor because it is very cheap and easy to install. For my cardboard car project I have chosen the HC-SR04 sensor. The HC-SR04 sensor is small in its dimensions and is available for a few bucks in every robot online shop. With the sensor it is possible to measure distances between 2cm and 4m with a theoretical resolution of 3mm. Depending on the echo signal coming back from the object the resolution of the sensor may vary. Soft obstacles like a curtain are not easy to recognize because they do not really reflect the ultrasonic sound. If you drive in an angel in the direction of a wall the echo signal will be reflected away from the sensor and not back to the sensor for a correct measurement.

The sensor is easily connected to an Arduino. If you use a SBC like a Raspberry Pi for your robot car you may need a Level-Shifter depending on the voltage of your GPIO’s and sensor. The ultrasonic sensor HC-SR04 needs a 5V power supply. The current which is used by the sensor is low as 2mA. This makes the sensor ideal for small robotic projects.

The picture below shows the HC-SR04 sensor.

HC-SR04 Ultrasonic distance sensor

HC-SR04 Ultrasonic distance sensor

The picture shows the HC-SR04 sensor and you can see the four pins of the sensor. The first pin is for the Vcc the 5V power supply of the module. The second pin with the label Trig is the trigger pin to activate the sensor. Over this pin you trigger the ultrasonic sensor to send the ultrasonic sound for a short time. The third pin with the label Echo send back a HIGH signal on the Echo pin when the echo of the ultrasonic sound was received by the sensor. The fourth pin with the label Gnd is the Ground pin.

Function of the HC-SR04 sensor

Adopt IO trigger through supplying at least 10us sequence of high level signal on the trigger pin. The module automatically send eight 40khz square wave and automatically detect whether receive
the returning pulse signal. If there is a signal returning the Echo pin is set to HIGH. The time between the trigger signal was send and the receiving of the sound, the Echo pin is set to High, is the  basis for the calculation of the distance in my demo program.

((Start Time (s) – Recieving Time (s)) * 340 m/s)/2

It is possible to do 50 measurements per second with the HC-SR04 module. The inteval between each measurement is 20ms. This time inteval is enough for a small robot car to control the distance in front of the car to the next object.

Video of the HC-SR04 sensor in action

I programed a small program to control two DC motors independently with two ultrasonic sensors. The DC motors are spinning with a basic speed until an object is more than 50cm away. If you go for example with your hands in front of the sensor and you reduce the distance less than 50cm the DC motors will slow down until they stop.

HC-SR04 demo program

You can download the small program here. It consists out of the ultrasonic demo program and the motor driver basic module for controlling the motors. You do not need a motor driver and a motor for executing the program. The program will also display the distance without a motor in the terminal. At the beginning of the program it is possible to set the GPIO pins for the ultrasonic sensor and the motor driver.

Download: Ultrasonic sensor program HC-SR04

(Visited 297 times, 1 visits today)