Interfacing HC04 Ultrasonic Sensor with Arduino Uno
The ultrasonic sensor is one of the most common and used sensors in DIY and electronics applications , due to its simplicity and accuracy for measuring distance and detecting objects it operates in a range from 3 cm to 400 cm .
The sensor is composed of two ultrasonic transducers , one is the transmitter which sends ultrasonic sound pulses and the other is the receiver which catch the reflected waves.
and it have also power terminals (VCC,Gnd) the HC-SR04 Ultrasonic sensor pinout figure is shown below:
Working Principal:
the working of the ultrasonic sensor is quite easy , by the beginning the the two Pins (trigger, echo) should be turned off (0) ,then the trigger pin is turned on (1) for a period of ….. micro second in order to send ultrasonic pulses , after that we turn off the trigger pin and we wait the echo pin to receive .
In order to generate the 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 away after that 8 cycle ultrasonic burst is sent, and it starts listening or waiting for 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 we receive a reflected pulse, the Echo pin will go down sooner than those 38ms. According to the amount of time the Echo pin was HIGH, we can determine the distance the sound wave traveled, thus the distance from the sensor to the object.
For that purpose we are using the following basic formula for calculating distance
Distance= Speed * Time the Speed is also known (sound speed on the air) 340 m/s
the time is evaluated by the period of the reception of the pulses by the echo Pin
There’s one additional step we need to do, and that’s divide the end result by 2. and that’s because we are measuring the duration the sound wave needs to travel to the object and bounce back.
so : Distance= (Speed * Time )/2
Interfacing with Arduino Uno:
for the interfacing with arduino the configuration is very simple we need to connect 5V and Gnd Pins to 5V and Gnd respectively on the arduino board then we connect the trigger pin and echo pin to two digital pin like is shown in the figure below:
the Circuit and the arduino code is given below :