ESP32 - Ultrasonic Sensor

This tutorial instructs you how to use ESP32 with the ultrasonic sensor HC-SR04 to measure the distance to an object.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Ultrasonic Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Ultrasonic Sensor

The ultrasonic sensor HC-SR04 is used to measure the distance from the sensor to an object by using ultrasonic waves.

Ultrasonic Sensor Pinout

The ultrasonic sensor HC-SR04 includes four pins:

  • VCC pin: connect this pin to VCC (5V)
  • GND pin: connect this pin to GND (0V)
  • TRIG pin: this pin receives a control pulse from ESP32.
  • ECHO pin: this pin generates a pulse corresponding to the measured distance to ESP32.
Ultrasonic Sensor Pinout
image source: diyables.io

How Ultrasonic Sensor Works

See How Ultrasonic Sensor Work

Wiring Diagram between Ultrasonic Sensor and ESP32

  • How to connect ESP32 and ultrasonic sensor using breadboard (powered via USB cable)
ESP32 Ultrasonic Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and ultrasonic sensor using breadboard (powered via Vin pin)
ESP32 Ultrasonic Sensor Wiring Diagram
How to connect ESP32 and ultrasonic sensor
How to wire ESP32 and ultrasonic sensor

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How To Program Ultrasonic Sensor

digitalWrite(23, HIGH); delayMicroseconds(10); digitalWrite(23, LOW);
  • Measures the pulse duration (µs) in ESP32's pin by using pulseIn() function. For example, pin GPIO22:
duration_us = pulseIn(22, HIGH);
  • Calculate distance (cm):
distance_cm = 0.017 * duration_us;

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ultrasonic-sensor */ #define TRIG_PIN 23 // ESP32 pin GPIO23 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 22 // ESP32 pin GPIO22 connected to Ultrasonic Sensor's ECHO pin float duration_us, distance_cm; void setup() { // begin serial port Serial.begin (9600); // configure the trigger pin to output mode pinMode(TRIG_PIN, OUTPUT); // configure the echo pin to input mode pinMode(ECHO_PIN, INPUT); } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Quick Instructions

How to upload ESP32 code on Arduino IDE
  • Open Serial Monitor on Arduino IDE
How to open serial monitor on Arduino IDE
  • Move your hand in front of ultrasonic sensor
  • See the distance from the sensor to your hand on Serial Monitor
COM6
Send
distance: 19.4 cm distance: 17.6 cm distance: 16.9 cm distance: 27.4 cm distance: 26.9 cm distance: 24.3 cm distance: 25.6 cm distance: 23.1 cm
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Line-by-line Code Explanation

The above ESP32 code contains line-by-line explanation. Please read the comments in the code!

How to Filter Noise from Distance Measurements of Ultrasonic Sensor

See How to Filter Noise from Distance Measurements of Ultrasonic Sensor

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES