HC-SR04 Ultrasonic Sensor with ESP8266

Introduction
The HC-SR04 Ultrasonic Sensor is widely used to measure distance by emitting ultrasonic waves and measuring the time it takes for the echo to return. It works like sonar:
- The TRIG pin sends a short ultrasonic pulse.
- The ECHO pin receives the reflected signal from an obstacle.
- Using the time taken for the pulse to return, we calculate the distance.
The ESP8266 reads this timing and converts it into centimeters (cm) or inches (in). Ultrasonic sensors are commonly used in robotics, automation, parking sensors, and water level monitoring.
Components Required
- ESP8266 (NodeMCU / Wemos D1 Mini)
- HC-SR04 Ultrasonic Sensor
- Jumper Wires
- Breadboard
- USB Cable
Circuit Diagram
β οΈ Important: The HC-SR04 ECHO pin outputs 5V, but ESP8266 works at 3.3V. Use a voltage divider (2 resistors, e.g., 1k + 2k) or a logic level shifter to protect the ESP8266.
- HC-SR04 VCC β 5V
- HC-SR04 GND β GND
- HC-SR04 TRIG β D2
- HC-SR04 ECHO β D3

Arduino Code
// HC-SR04 Ultrasonic Sensor with ESP8266
#define TRIG_PIN D2 // GPIO5
#define ECHO_PIN D3 // GPIO4
long duration;
int distance;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Step 1: Clear TRIG pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Step 2: Send 10us pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Step 3: Measure echo time
duration = pulseIn(ECHO_PIN, HIGH);
// Step 4: Convert to distance
distance = duration * 0.034 / 2; // Speed of sound: 343 m/s
// Step 5: Print distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
Step-by-Step Code Explanation
Define TRIG and ECHO pins
#define TRIG_PIN D2 #define ECHO_PIN D3
TRIG sends the pulse, ECHO receives it.
Send 10Β΅s Trigger Pulse
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW);
This starts the ultrasonic wave.
Measure Echo Duration
duration = pulseIn(ECHO_PIN, HIGH);
Returns the time in microseconds that the echo pin stayed HIGH.
Convert into Distance
distance = duration * 0.034 / 2;
- Speed of sound = 0.034 cm/Β΅s
- Divide by 2 (round trip).
Output
Place an object 10 cm away β Serial Monitor prints:
Distance: 10 cm
Move object further β Distance increases.
Real-Life Applications
- Obstacle Avoidance Robots β Detects distance to objects.
- Parking Assistance β Measures distance to walls/objects.
- Smart Trash Bin β Opens lid automatically when hand is near.
- Water Level Monitoring β Measures tank water depth.
- Home Automation β Presence detection for lights/alarms.
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| Always reads 0 | Wrong wiring | Check TRIG/ECHO pins and voltage divider |
| Values fluctuate | Noise / unstable supply | Use capacitor across VCC & GND |
| ESP8266 resets | ECHO pin gives 5V | Use voltage divider (e.g., 1k + 2k resistors) |
| No output in Serial Monitor | Wrong baud rate | Set Serial Monitor to 115200 baud |
Can I connect HC-SR04 directly to ESP8266?
No. The ECHO pin outputs 5V, so you must use a voltage divider before connecting to ESP8266.
What is the range of HC-SR04?
It can measure 2 cm to 400 cm with good accuracy.
Can I power the sensor from 3.3V?
No. Always power it from 5V for reliable operation.
Can I display distance on an LCD or IoT dashboard?
Yes. You can connect an I2C LCD (1602/2004) or send data to ThingSpeak, Blynk, or MQTT for IoT applications.
How accurate is HC-SR04?
It has an accuracy of about Β±3 mm under ideal conditions.