ESP32 with HC-SR04 Ultrasonic Sensor: A Comprehensive Guide





The HC-SR04 ultrasonic sensor is a versatile tool for measuring distance accurately, and when combined with the power of the ESP32 microcontroller, it opens up a world of possibilities for your IoT and robotics projects. In this comprehensive guide, we will explore how the HC-SR04 sensor works with the ESP32, 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 uses ultrasonic waves to measure distance. Here’s a simplified explanation of its working:

  1. Trigger Signal: The ESP32 sends a short, high-frequency pulse (typically 10 microseconds) to the trigger pin of the HC-SR04. This 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, often 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 reflected sound wave’s arrival time.
  5. Calculating Distance: By measuring the time it takes for the sound wave to travel to the object and back, the ESP32 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 ESP32

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

  • VCC: Connect to ESP32’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 ESP32’s GND pin.

Ensure a common ground connection between the ESP32 and the sensor.

ESP32 Code for HC-SR04

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

const int trigPin = 2;
const int echoPin = 4;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop() {
  digitalWrite(trigPin, LOW);

  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(" cm");


The HC-SR04 ultrasonic sensor, when paired with the ESP32, offers a powerful combination for measuring distances in IoT and robotics projects. With its simple working principle and ease of integration, you can create innovative applications. Whether you’re building a smart surveillance system or a weather station, the HC-SR04 and ESP32 deliver accuracy and reliability.

Frequently Asked Questions (FAQ)

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

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

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

To improve 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 ESP32?

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

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 ESP32 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 ESP32?

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 ESP32?

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.

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