ESP32 S3 - Ultrasonic Sensor
by DIYables in Circuits > Microcontrollers
9 Views, 0 Favorites, 0 Comments
ESP32 S3 - 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://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
Everything below is what the build uses. The links go to the parts we make and test.
- ESP32 S3 WROOM N16R8
- ESP32 S3 SuperMini Dev Module
- ESP32 S3 Uno-form Board
- ESP32 S3 Camera Board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Ultrasonic Sensor
- Breadboard
- Jumper Wires
- DC Power Jack
- Screw Terminal Expansion Board for ESP32 S3
- Breakout Expansion Board for ESP32 S3
- Power Splitter for ESP32 S3
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
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.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor