ESP32 S3 - Ultrasonic Sensor

by DIYables in Circuits > Microcontrollers

9 Views, 0 Favorites, 0 Comments

ESP32 S3 - Ultrasonic Sensor

esp32-s3-ultrasonic-sensor-cover.jpg

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://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor

This build adds an HC-SR04 ultrasonic distance sensor to the ESP32 S3, giving you non-contact distance readings from 2 cm to 400 cm. You wire the sensor, upload a sketch that triggers a pulse and times its echo, and watch live distance values print out on the Serial Monitor.

Supplies

Wiring

esp32-s3-ultrasonic-sensor-wiring-diagram.jpg

Connect the HC-SR04 VCC pin to the ESP32 S3 5V pin and GND to ground. Connect TRIG to a digital GPIO on the ESP32 S3 (GPIO8 in this build) and ECHO to another digital GPIO. Always use 5V for VCC, not 3.3V, since the sensor will not operate correctly at the lower voltage. Double check all four wires before powering the board so you do not send bad signals on TRIG or ECHO.

How It Works

The ESP32 S3 drives TRIG high for 10 microseconds to fire an ultrasonic pulse. The sensor then raises ECHO high for a duration equal to the round-trip travel time of the sound wave as it bounces off an object and returns. The sketch reads that pulse width with pulseIn(), then converts it to a distance in centimeters using the speed of sound, dividing by two to account for the round trip.

Code

The sketch sets up TRIG as an output and ECHO as an input in setup(). In loop(), it sends a trigger pulse every 500 milliseconds, measures the returning echo duration, converts it to centimeters, and prints the result to the Serial Monitor. Open the Serial Monitor at 115200 baud after uploading to see live readings as you move an object in front of the sensor.

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

/*
* This ESP32 S3 code was developed by newbiely.com
*
* This ESP32 S3 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor
*/

#define TRIG_PIN 8 // The ESP32 S3 pin connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN 9 // The ESP32 S3 pin connected to Ultrasonic Sensor's ECHO pin

float duration_us, distance_cm;

void setup() {
// begin serial port
Serial.begin (115200);

// Configure the trigger pin to output mode
pinMode(TRIG_PIN, OUTPUT);
// Configure the echo pin to input mode
pinMode(ECHO_PIN, INPUT);
}

void loop() {
// Produce a 10-microsecond pulse to the TRIG pin.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Measure the pulse duration from the 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://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor