The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.
An ultrasonic sensor generates the high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.
Ultrasonic Working Principle
By measuring the time required for the echo to reach to the receiver, we can calculate the distance. This is the basic working principle of Ultrasonic module to measure distance.
HC-SR-04 Ultrasonic Module
HC-SR-04 has an ultrasonic transmitter, receiver and control circuit.
In ultrasonic module HCSR04, we have to give trigger pulse, so that it will generate ultrasound of frequency 40 kHz. After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high. Echo pin remains high until it does not get the echo sound back. So the width of echo pin will be the time for sound to travel to the object and return back. Once we get the time we can calculate distance, as we know the speed of sound.
HC-SR04 can measure up to range from 2 cm – 400 cm.
HC-SR04 Pin Description
VCC – +5 V supply
TRIG – Trigger input of sensor. Microcontroller applies 10 us trigger pulse to the HC-SR04 ultrasonic module.
ECHO–Echo output of sensor. Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.
GND – Ground
HC-SR04 Working Principle
HC-SR04 Ultrasonic Module Timing Diagram
- We need to transmit trigger pulse of at least 10 us to the HC-SR04 Trig Pin.
- Then the HC-SR04 automatically sends Eight 40 kHz sound wave and wait for rising edge output at Echo pin.
- When the rising edge capture occurs at Echo pin, start the Timer and wait for falling edge on Echo pin.
- As soon as the falling edge is captured at the Echo pin, read the count of the Timer. This time count is the time required by the sensor to detect an object and return back from an object.
Now how to calculate distance?
We know that,
The speed of sound waves is 343 m/s.
Total distance is divided by 2 because signal travels from HC-SR04 to object and returns to the module HC-SR-04.