The HC-SR04 ultrasonic sensor is widely used in beginner electronics and robotics to estimate the distance to a nearby object. It sends a short ultrasonic burst and measures how long the echo takes to return. With an Arduino, this makes a useful first project for obstacle detection, parking aids and simple interactive devices.
How the measurement works
The sensor uses a trigger pulse to start a measurement and an echo pulse whose duration corresponds to the sound’s round trip. A common approximation at room temperature is:
Distance in centimetres ≈ echo duration in microseconds ÷ 58.
This is an estimate, not a precision measurement. Temperature, target shape, angle, surface material and electrical noise can affect the result. Soft fabric and angled surfaces may reflect sound away from the receiver.
Wiring to an Arduino Uno
| HC-SR04 pin | Example Arduino connection |
|---|---|
| VCC | 5 V, if supported by your sensor board |
| GND | GND |
| TRIG | Digital pin 9 |
| ECHO | Digital pin 10 |
Use a shared ground and keep wires short during initial tests. Important: the classic HC-SR04 Echo signal is usually 5 V. Do not connect it directly to an ESP32 GPIO; use a suitable resistor divider or level shifter so the input stays within the ESP32’s 3.3 V limit.
Example Arduino sketch
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Timeout prevents the sketch waiting indefinitely.
unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000UL);
if (duration == 0) {
Serial.println("No echo");
} else {
float distanceCm = duration / 58.0;
Serial.print("Distance: ");
Serial.print(distanceCm, 1);
Serial.println(" cm");
}
delay(100);
}
Upload the sketch, open Serial Monitor at 9600 baud and place a flat object in front of the sensor. Keep the object within the sensor’s practical operating range and perpendicular to the two transducers for the most repeatable results.
Why readings jump around
- Target is angled: rotate the sensor or target so the sound reflects back.
- Readings are zero: check VCC, GND, trigger/echo pin assignments and the echo timeout.
- Readings spike near motors: improve power wiring, keep sensor leads away from motor wires and add appropriate supply decoupling.
- Very close object: many ultrasonic modules have a minimum range; use another sensor type if close-range detection is essential.
- Multiple sensors interfere: trigger them sequentially rather than at exactly the same time.
Using it on a robot
For an obstacle-avoiding rover, take several readings and reject obvious timeouts rather than trusting one sample. Add hysteresis to the stop threshold—for example, stop below one distance and resume only after the measured distance rises above a slightly larger one. This prevents rapid stop/start behaviour near the threshold.
Do not treat the sensor as a safety-rated device. A hobby robot can miss soft, narrow or angled objects, so test slowly and keep a manual stop available.
Need the rest of the build? Start with the beginner board and parts checklist, and browse Pandey Electronics for microcontrollers, sensors and prototyping accessories. Check the exact product datasheet before wiring.
Summary
The HC-SR04 is an affordable way to learn timing and sensor integration. Accurate-enough results depend on correct wiring, a suitable target angle, timeouts, clean power and sensible filtering. Always protect 3.3 V microcontroller inputs from 5 V Echo signals.