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.

Arduino sensor wiring being checked for ultrasonic distance measurement
Test the sensor alone with a flat target before adding robot code, servos, displays, or motor drivers.

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.

Do not ignore this:

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.

From Prototype to Product: Moving Beyond the Workbench

The HC-SR04 outputs a 5V echo pulse. Directly connecting this to a 3.3V microcontroller like the ESP32 can degrade the GPIO pin or crash the processor. To turn this prototype into a robust product, you need to integrate proper level translation, connector anchoring, and software filtering on your custom PCB.

Prototype-to-Product Journey

The Productization Path for HC-SR04 Sensors

1 Workbench Fix: Level-shift the 5V Echo pin using breadboard resistors, verify VCC/GND wires, and test with minimal code.
2 Logic-Level Translation: Place a resistor divider (1kΩ / 2kΩ) or an active level shifter (e.g. TXS0102) directly on the board to translate the 5V Echo logic down to safe 3.3V logic.
3 Connector Security: Replace jumper wires with a keyed 4-pin JST-PH or JST-XH connector to prevent reversing VCC and GND.
4 Noise Filtering: Implement a software median filter (e.g. 5-point moving median) in firmware to discard random reflection spikes and out-of-bounds readings.
5 Project Release: Save, version, and lock your KiCad design and BOM configurations inside PCBVault Software to ensure your fabricator builds the correct board revision.

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.

Ready to productize your design?

Get the free Electronics Project Rescue Pack. It includes power, wiring, module, sensor, and breadboard-to-PCB checklists to help you move from a messy prototype to a release-ready custom PCB.

Download the rescue pack