Ultrasonic distance sensors are designed to measure distance between the source and target using ultrasonic waves. We use ultrasonic waves because they are relatively accurate across short distances and don’t cause disturbances as they are inaudible to human ear.
HC-SR04 is a commonly used module for non contact distance measurement for distances from 2cm to 400cm. It uses sonar (like bats and dolphins) to measure distance with high accuracy and stable readings. It consist of an ultrasonic transmitter, receiver and control circuit. The transmitter transmits short bursts which gets reflected by target and are picked up by the receiver. The time difference between transmission and reception of ultrasonic signals is calculated. Using the speed of sound and ‘Speed = Distance/Time‘ equation, the distance between the source and target can be easily calculated.
HC-SR04 ultrasonic distance sensor module has four pins :
- VCC – 5V, input power
- TRIG – Trigger Input
- ECHO – Echo Output
- GND – Ground
Working of HC-SR04
- Provide trigger signal to TRIG input, it requires a HIGH signal of atleast 10μS duration.
- This enables the module to transmit eight 40KHz ultrasonic burst.
- If there is an obstacle in-front of the module, it will reflect those ultrasonic waves
- If the signal comes back, the ECHO output of the module will be HIGH for a duration of time taken for sending and receiving ultrasonic signals. The pulse width ranges from 150μS to 25mS depending upon the distance of the obstacle from the sensor and it will be about 38ms if there is no obstacle.
The ECHO output is of 5v. The input pin of Raspberry Pi GPIO is rated at 3.3v. So 5v cannot be directly given to the unprotected 3.3v input pin. Therefore we use a voltage divider circuit using appropriate resistors to bring down the voltage to 3.3V.
For more detail: Interfacing HC-SR04 Ultrasonic Sensor with Raspberry Pi