ESP32 - Ultrasonic Sensor

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

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Ultrasonic Sensor
4×Jumper Wires
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

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

How Ultrasonic Sensor Works

See How Ultrasonic Sensor Work

Wiring Diagram between Ultrasonic Sensor and ESP32

The wiring diagram with power supply from USB cable

wiring diagram between esp32 ultrasonic sensor

This image is created using Fritzing. Click to enlarge image

The wiring diagram with power supply from 5v adapter

wiring diagram between esp32 ultrasonic sensor

This image is created using Fritzing. Click to enlarge image

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 GIOP22:
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 GIOP23 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 22 // ESP32 pin GIOP22 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

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • 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.

※ NOTICES