ESP32 - Ultrasonic Sensor

by DIYables in Circuits > Microcontrollers

64 Views, 0 Favorites, 0 Comments

ESP32 - Ultrasonic Sensor

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-ultrasonic-sensor

This build uses an ESP32 with the HC-SR04 ultrasonic sensor to measure the distance from the sensor to an object. The ESP32 triggers the sensor, times the returning echo pulse, and converts that time into a distance reading printed to the serial monitor.

Supplies

Wiring

The HC-SR04 has four pins: VCC, GND, TRIG, and ECHO. Connect VCC to the ESP32's 5V, and GND to GND. Connect TRIG to a digital pin such as GPIO23, which sends the control pulse from the ESP32 to the sensor. Connect ECHO to another digital pin such as GPIO22, which carries the return pulse back to the ESP32. You can wire this on a breadboard, or use a screw terminal breakout board, and power the ESP32 either through USB or through the Vin pin.

How It Works

The ESP32 sends a 10-microsecond HIGH pulse on the TRIG pin to fire the sensor. The HC-SR04 then sends out an ultrasonic wave and raises its ECHO pin for the time it takes the wave to bounce off an object and return. The ESP32 measures how long ECHO stays HIGH with pulseIn(), and that duration is used to calculate the distance to the object in centimeters.

Code

The sketch sets the TRIG pin as output and the ECHO pin as input. In the loop, it pulses TRIG HIGH for 10 microseconds using digitalWrite() and delayMicroseconds(), then reads the echo duration on the ECHO pin with pulseIn(). That duration is converted into a distance value and sent to the serial monitor with Serial.println(), repeating with a short delay() between readings.

esp32-ultrasonic-sensor.ino - the sketch from the tutorial, unchanged.

/*
* 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);
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-ultrasonic-sensor