The HC-SR04 Ultrasonic Distance Meter project teaches you one of the most useful applications of sensors in embedded systems: precise distance measurement.
In this project, an HC-SR04 ultrasonic sensor measures the distance to objects by sending sound waves and calculating the time it takes for the echo to return. The Arduino reads the sensor data and displays the distance on the Serial Monitor in both centimeters and inches.
When an object is placed in front of the sensor, the Arduino instantly measures the distance. As you move the object closer or farther away, the distance readings update in real-time, creating an interactive distance measurement system.
This project teaches Ultrasonic Sensor Basics, Pulse Measurement Timing, Distance Calculation Formulas, Sensor Calibration, and Real-Time Data Display.
By finishing this project, you’ll know how ultrasonic sensors work in robotics, parking systems, obstacle detection, and smart automation systems.
To build this circuit, collect these specific items from your kit:
Arduino Uno × 1, Breadboard × 1, HC-SR04 Ultrasonic Sensor × 1, Jumper Wires × 4, USB Cable × 1
Connect the Arduino GND pin to the negative rail of the breadboard. This creates a shared ground connection for your entire circuit.
Place the HC-SR04 ultrasonic sensor near the edge of your breadboard. The sensor has four pins that need careful connections.
Connect the VCC (power) pin of the HC-SR04 to Arduino 5V. This provides power to the ultrasonic sensor module.
Connect the GND (ground) pin of the HC-SR04 to the breadboard negative rail. This completes the power circuit.
Connect the TRIG (trigger) pin of the HC-SR04 to Arduino Pin 9. This pin sends the signal to activate the sensor.
Connect the ECHO (echo) pin of the HC-SR04 to Arduino Pin 10. This pin receives the reflected sound wave information.
Double-check all four sensor pins before uploading your code. Ensure VCC and GND are correct and verify TRIG and ECHO pins match your circuit.
Important: The HC-SR04 sensor measures distance using ultrasonic sound waves. The TRIG pin sends the pulse and the ECHO pin receives it back, allowing Arduino to calculate distance accurately.
In setup(), Arduino sets Pin 9 as OUTPUT for the trigger signal. Pin 10 is set as INPUT to receive the echo signal. This allows Arduino to communicate with the HC-SR04 sensor properly.
The Arduino sends a 10-microsecond pulse to the TRIG pin. This pulse tells the ultrasonic sensor to send out sound waves and listen for the echo.
The tone() function generates Arduino uses the pulseIn() function to measure how long the ECHO pin stays HIGH. The duration represents the time it takes for the sound wave to travel to an object and return to the sensor.
The calculated distance is printed to the Serial Monitor every 500 milliseconds. The program continuously repeats this process inside the loop() function, allowing real-time distance measurement.
After uploading your code, here is what happens.
Open the Serial Monitor at 9600 baud rate to see the distance readings. When no object is in front of the sensor, the distance shows as very large or maximum range. Place your hand in front of the sensor and the distance displays the actual distance in centimeters. Move your hand closer and farther away and watch the distance values update in real-time. The sensor continuously measures and displays distance without any user interaction.
No Distance Readings Appear :- Verify that all four sensor pins connect correctly to Arduino. Check that VCC and GND have proper power connections and confirm the Serial Monitor is open at 9600 band.
Distance Shows Zero or Maximum :- Check the TRIG and ECHO pin connections. Make sure Pin 9 is TRIG and Pin 10 is ECHO. Verify the sensor is not blocked by debris or obstacles.
Readings Are Inconsistent :- Move the sensor away from reflective surfaces like walls or metal objects. Add a small delay between measurements in your code if readings jump around too much.
Ultrasonic Sensor Not Responding :- Verify the VCC voltage is at 5V and check that the GND connection is solid. Try uploading a simple test code to confirm the sensor is receiving power.
Distance Values Are Inaccurate :- Check the distance calculation formula in your code. Adjust the divisor values if readings seem off. The speed of sound changes with temperature, so calibration may be needed.
By finishing this project, you now know :-
How ultrasonic sensors measure distance using sound waves, how to use the pulseIn() function to measure pulse duration, how to calculate distance from time measurements, how to work with multiple sensor inputs simultaneously, and how to display real-time sensor data on the Serial Monitor.
In Day 10, you’ll learn how to build an Obstacle Avoidance Robot using the HC-SR04 sensor with motor control to create a fully autonomous robot that detects and avoids obstacles automatically.