Interfacing Ultrasonic Sensor with ESP32

What is an Ultrasonic Sensor?

An Ultrasonic Sensor like the HC-SR04 measures distance by sending out ultrasonic waves and measuring the time it takes for the echo to return. It is widely used in robotics, obstacle detection, automation, and IoT projects.

Working Principle of Ultrasonic Sensor

The Ultrasonic Sensor operates on the time-of-flight principle:

  • The trigger pin sends an ultrasonic pulse.
  • The echo pin receives the reflected signal.
  • The time difference between sending and receiving the signal is used to calculate distance.

Formula: Distance = (Time × Speed of Sound) / 2

Components Required

  • ESP32 board
  • HC-SR04 Ultrasonic Sensor
  • Jumper wires
  • Breadboard (optional)

Pin Configuration of HC-SR04

  • VCC: Power supply (+5V or +3.3V)
  • GND: Ground
  • Trig: Trigger pin (Input)
  • Echo: Echo pin (Output)

ESP32 operates at 3.3V logic level. HC-SR04 can work with 3.3V, but for safe operation, use a voltage divider on the Echo pin if needed.

Wiring HC-SR04 to ESP32

  • VCC -> 3.3V (or 5V if supported)
  • GND -> GND
  • Trig -> GPIO 5
  • Echo -> GPIO 18

Arduino Code for ESP32 + HC-SR04

1#define TRIG_PIN 5
2#define ECHO_PIN 18
3
4long duration;
5float distance;
6
7void setup() {
8  Serial.begin(115200);
9  pinMode(TRIG_PIN, OUTPUT);
10  pinMode(ECHO_PIN, INPUT);
11}
12
13void loop() {
14  digitalWrite(TRIG_PIN, LOW);
15  delayMicroseconds(2);
16  digitalWrite(TRIG_PIN, HIGH);
17  delayMicroseconds(10);
18  digitalWrite(TRIG_PIN, LOW);
19
20  duration = pulseIn(ECHO_PIN, HIGH);
21  distance = (duration * 0.0343) / 2;
22
23  Serial.print("Distance: ");
24  Serial.print(distance);
25  Serial.println(" cm");
26
27  delay(500);
28}

Code Explanation (Line-by-Line)

  • #define TRIG_PIN 5: Assigns GPIO 5 as the Trigger pin for the sensor.
  • #define ECHO_PIN 18: Assigns GPIO 18 as the Echo pin to receive the reflected signal.
  • Serial.begin(115200);: Starts serial communication for debugging/output.
  • pinMode(...);: Sets the mode of TRIG_PIN as output and ECHO_PIN as input.
  • digitalWrite(TRIG_PIN, LOW); ...: This sequence sends a 10-microsecond HIGH pulse to trigger the sensor to emit ultrasonic waves.
  • duration = pulseIn(ECHO_PIN, HIGH);: Reads the time in microseconds that the echo pin stays HIGH (i.e., the wave has returned).
  • distance = (duration * 0.0343) / 2;: 0.0343 cm/µs is the speed of sound. Divide by 2 because the signal travels to the object and back.
  • Serial.print(...);: Shows the measured distance in the Serial Monitor.

Applications

  • Obstacle Avoidance in Robots
  • Smart Parking Systems
  • Liquid Level Monitoring
  • Industrial Proximity Sensors
  • Smart Bins / Trash Monitoring

Conclusion

Interfacing an Ultrasonic Sensor with ESP32 is straightforward. With just a few lines of code, you can measure distances accurately and integrate them into smart systems and IoT-based solutions. Whether you're working on a robot or a home automation system, this sensor adds real-time sensing capabilities with reliable accuracy.