This circuit demonstrates how to use an HC-SR04 Ultrasonic Range finder to measure the distance to an object.
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. Power the module using Vcc, ground it using GND, and use an Arduino digital pin to send an input signal to TRIG, which triggers the sensor to transmit an ultrasonic burst of 8 x 40KHz pulses. The sound waves bounce off any nearby objects and some are reflected back to the sensor.
The ECHO pin will be “LOW” (0V) until the outgoing signal has stopped, at which point it goes HIGH (5v). Once a return pulse has been detected, ECHO is set “LOW” again. So the time for which the ECHO pin is HIGH equals the time taken for the sound wave to travel to the nearest obstacle and back.
Your code needs to send a signal to the TRIG pin and then time how long the pulse on ECHO lasts. The Arduino language makes this easy thanks to the pulseIn() function which reads a pulse on a pin and returns the length of the pulse.
Because speed = distance divided by time, and you know how fast sound travels in air (343 m/s), you can calculate the distance to the obstacle.
Rearranging the formula you see that distance = speed x time which you can caclulate in your code (and remembering to convert to cm). You also need to divide your answer by 2 - can you explain why?
Use the Serial monitor to read the results from the sensor.
Can you add some LEDs to indicate how close an object is? Or add a buzzer to warn if something gets too close to the sensor?