Sensor Troubleshooting
HC-SR04 ultrasonic sensor not working: Arduino and ESP32 fix guide
HC-SR04 failures usually come from swapped trigger/echo pins, no common ground, weak 5V power, wrong timing code, measuring soft/angled surfaces, or feeding a 5V echo signal directly into an ESP32 input.
Direct answer
If your HC-SR04 is not working, connect VCC to 5V, GND to common ground, TRIG to the trigger pin in your code, ECHO to the echo pin, use a voltage divider on ECHO for ESP32, test with a flat object 10-50 cm away, and run a minimal distance sketch before adding motors or displays.
Symptoms and likely causes
| Symptom | Likely cause | First fix |
|---|---|---|
| Always reads 0 cm | Echo not detected, wrong pins, bad wiring | Check TRIG/ECHO pins and common ground |
| Always reads maximum or random values | No echo, bad target angle, noisy wiring, timing issue | Use a flat object close to the sensor |
| Works on Arduino but not ESP32 | 5V ECHO signal unsafe or wrong ESP32 pins | Use a voltage divider on ECHO |
| Robot behaves randomly | Sensor works but motor noise or code logic breaks readings | Test sensor alone, then add motors later |
Correct HC-SR04 wiring
The basic wiring is simple: VCC to 5V, GND to ground, TRIG to a digital output pin, and ECHO to a digital input pin. The code pin numbers must match the real wiring. A common mistake is copying code that uses pins 9 and 10 while the sensor is wired to different pins.
ESP32 warning: level shift the ECHO pin
The HC-SR04 ECHO pin can output 5V. ESP32 GPIO pins are 3.3V inputs. Use a voltage divider or level shifter between ECHO and the ESP32 input. Arduino Uno can usually read the 5V echo directly, but ESP32 should not.
A sensor may appear to work for a while even with unsafe voltage, but repeated 5V input can damage ESP32 GPIO pins.
Minimal HC-SR04 test code
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
float distanceCm = duration * 0.0343 / 2;
Serial.println(distanceCm);
delay(300);
}
If `duration` is 0, the echo was not received within the timeout. Check wiring, target distance, sensor orientation, and power.
Accuracy checks
Ultrasonic sensors do not measure every surface well. Soft cloth, angled objects, very small targets, moving objects, and noisy environments can produce bad readings. Start with a flat wall or cardboard sheet 10-50 cm away.
FAQ
Why does HC-SR04 read zero?
The echo pulse may not be arriving. Check trigger/echo pins, common ground, power, code pin numbers, and whether the target is in range.
Can HC-SR04 run on 3.3V?
Some modules may respond at 3.3V, but standard HC-SR04 modules are normally used at 5V. With ESP32, power may be 5V but ECHO should be level shifted.
Sensor works alone but fails in the project?
Score the full project to find whether the real issue is power, wiring, firmware, or module integration.
Score your project