Ultrasonic Sensor
Find distance to the nearest obstacle
In the previous program, we saw how a combination of Infrared Transmitter and a Receiver LED is used to check if a line is black or white. In this section, we will see how to read values from an Ultrasonic sensor.
To use an ultrasonic sensor, first connect a HC-SR04 Ultrasonic Sensor provided in the kit. Align and insert into the four pin female header which is soldered in front of the board. HC-SR04 is the most popular sensor for measuring distance and making obstacle avoiding robots.
HC-SR04 is an easy to use distance measuring sensor which has a range from 3cm to 400cm. The sensor is composed of two ultrasonic transducers. Transmitter outputs ultrasonic sound pulses and the receiver listens to reflected waves. It’s basically a mini SONAR which is used in submarines for detecting underwater objects.
The sensor has 4 pins: VCC, GND, Trigger and Echo. The Trigger pin sends the ultrasound wave from the transmitter, and Echo pin listens to the reflected waves.
Ultrasonic Sensor emits an ultrasound at 40 000 Hz (40 kHz) which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound we can calculate the distance.
In order to generate ultrasound we need to set the Trig pin on a High State for 10 µs. That will send out an 8 cycle ultrasonic burst which will travel at the speed of sound. The Echo pins goes high right after that 8 cycle ultrasonic burst is sent, and it starts listening to that wave to be reflected from an object.
If there is no object or reflected pulse, the Echo pin will time-out after 38ms and get back to low state. If there is an obstacle, the Echo pin will go high sooner than 38ms and depending on the amount of time it was high, we can calculate the distance from the nearest obstacle.
Lets implement this in a program and check how it works:
Last updated