ESP8266 with HC-SR04 Ultrasonic Sensor: A Comprehensive Guide





The HC-SR04 ultrasonic sensor is a valuable component for measuring distances accurately, and when paired with the ESP8266 microcontroller, it becomes a powerful tool for IoT and robotics projects. In this comprehensive guide, we will explore how the HC-SR04 sensor works with the ESP8266, how to connect it, write code to obtain distance measurements, troubleshoot common issues, and answer frequently asked questions.

How Does the HC-SR04 Sensor Work?

The HC-SR04 sensor utilizes ultrasonic waves to measure distance. Here’s a simplified explanation of its working:

  1. Trigger Signal: The ESP8266 sends a brief, high-frequency pulse (usually 10 microseconds) to the trigger pin of the HC-SR04. This pulse triggers the sensor to emit an ultrasonic sound wave.
  2. Sound Wave Emission: The sensor emits an ultrasonic sound wave (inaudible to humans) at a specific frequency, typically 40 kHz.
  3. Reflection: When the sound wave encounters an object, it reflects back towards the sensor.
  4. Echo Reception: The HC-SR04’s receiver (echo pin) detects the arrival time of the reflected sound wave.
  5. Calculating Distance: By measuring the time it takes for the sound wave to travel to the object and back, the ESP8266 calculates the distance using the speed of sound in the air (approximately 343 meters per second at room temperature).

Wiring the HC-SR04 Sensor to ESP8266

To connect the HC-SR04 sensor to your ESP8266, follow these wiring instructions:

  • VCC: Connect to ESP8266’s 5V pin.
  • Trig: Connect to any digital GPIO pin (e.g., GPIO2).
  • Echo: Connect to any digital GPIO pin (e.g., GPIO4).
  • GND: Connect to ESP8266’s GND pin.

Ensure there is a common ground connection between the ESP8266 and the sensor.

ESP8266 Code for HC-SR04

Here’s a sample Arduino code for ESP8266 to obtain distance measurements using the HC-SR04 sensor:

#include <ESP8266WiFi.h>
int trigPin = D2; // Define GPIO pin for trigger
int echoPin = D1; // Define GPIO pin for echo
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  WiFi.mode(WIFI_OFF); // Turn off WiFi to save power

void loop() {
  // Trigger pulse
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  // Read echo pulse
  duration = pulseIn(echoPin, HIGH);

  // Calculate distance
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(" cm");
  delay(1000); // Delay for 1 second before next measurement


The HC-SR04 ultrasonic sensor, when combined with the ESP8266, offers a powerful solution for measuring distances in IoT and robotics applications. With its straightforward working principle and ease of integration, you can create innovative projects. Whether you’re building a smart home device or a remote monitoring system, the HC-SR04 and ESP8266 provide accurate and reliable distance measurements.

Frequently Asked Questions (FAQ)

1. What is the maximum range of the HC-SR04 sensor with the ESP8266?

The HC-SR04 sensor typically has a maximum range of around 4-5 meters with the ESP8266. Beyond this range, accuracy may decrease.

2. How can I improve the accuracy of distance measurements?

To enhance accuracy, use a stable power supply, minimize interference, and account for the sensor’s initial delay in your code. Calibration may also help.

3. Can I use multiple HC-SR04 sensors with one ESP8266?

Yes, you can use multiple sensors by connecting each sensor to a unique pair of trigger and echo pins on the ESP8266.

4. What should I do if my sensor consistently returns incorrect readings?

Check the wiring, power supply voltage, and ensure there are no obstacles or interference in the sensor’s path. Adjust the code for precise calculations.

Troubleshooting Q&A

Q: My HC-SR04 sensor connected to the ESP8266 is giving erratic readings. What could be the issue?

A: Inconsistent readings can result from incorrect wiring, an unstable power supply, or interference. Double-check your connections, use a stable power source, and ensure a clear path between the sensor and objects.

Q: Why do I get inaccurate measurements at longer distances with the HC-SR04 and ESP8266?

A: Accuracy decreases with distance due to factors like sound wave dispersion. To enhance accuracy at longer ranges, consider using a different sensor or implementing error correction in your code.

Q: Can I use the HC-SR04 sensor outdoors with the ESP8266?

A: While the HC-SR04 can be used outdoors, keep in mind that environmental conditions like wind and rain may affect its performance. Consider using protective enclosures or specialized outdoor sensors for robust outdoor applications.

Q: Is it possible to measure the distance to multiple objects simultaneously?

A: The HC-SR04 measures distances to the nearest object in its path. To measure multiple objects simultaneously, use multiple sensors pointed in different directions and process their data accordingly.

By following this guide, you’ll have a solid foundation for working with the HC-SR04 ultrasonic sensor and the ESP8266 in your projects. Explore the endless possibilities of distance measurement in your IoT and robotics innovations!

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore our other blogs.

  • 8-bit vs. 32-bit Microcontrollers in Today’s Projects

  • Nintendo Sues Creators of Popular Switch Emulator Yuzu, Citing Piracy Concerns

  • Raspberry Pi CPU Temperature Range – Everything You Need to Know

  • image of tunnel

    Reverse Tunneling with Raspberry Pi: A Comprehensive Guide