ESP32 S3 UNO - Ultrasonic Sensor - LED

This ESP32 S3 Uno ultrasonic sensor LED tutorial shows you how to turn an LED on and off by distance. You will wire an HC-SR04 distance sensor and an LED to the ESP32 S3 Uno form board. When an object comes close, the LED lights up. When it moves away, the LED goes dark.

In this tutorial, you will:

  1. Wire an HC-SR04 ultrasonic sensor and an LED to the ESP32 S3 Uno
  2. Protect the 3.3V pin from the 5V ECHO signal
  3. Measure distance with pulseIn() in Arduino code
  4. Turn an LED on when an object is closer than a set distance
ESP32 S3 Uno ultrasonic sensor LED

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Ultrasonic Sensor
1×LED Kit
1×LED (red)
1×LED Module
1×Alternatively, Button and LED Kit
1×220Ω Resistor
1×5V to 3.3V Level Converter (for the ECHO signal)
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED and Ultrasonic Sensor

This project uses one input and one output. The ultrasonic sensor tells the board how far away an object is, and the LED shows the result. If you are new to either part, learn it alone first. The tutorials below explain the pinout, how each part works, and how to program it.

How the project works

The HC-SR04 sends out a short burst of sound that people cannot hear. The sound hits an object and bounces back. The sensor sets its ECHO pin HIGH for as long as the sound takes to go and come back. The ESP32 S3 Uno measures this time and turns it into a distance in centimeters.

The code then compares this distance with a threshold of 50 cm. If the object is closer than 50 cm, the LED turns on. If it is farther, the LED turns off.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to match the Uno header pins (D3, D6, D7) to their GPIO numbers when you build the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The sensor's TRIG pin goes to D6 and its ECHO pin goes to D7. The LED goes to D3 through a 220 ohm resistor.

The wiring diagram between ESP32 S3 Uno Ultrasonic Sensor LED

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Ultrasonic Sensor VCC 5V
Ultrasonic Sensor TRIG D6 (GPIO3)
Ultrasonic Sensor ECHO (through level converter) D7 (GPIO14)
Ultrasonic Sensor GND GND
LED Anode (+) via 220 ohm resistor D3 (GPIO17)
LED Cathode (-) GND

WARNING

The HC-SR04 runs on 5V, so its ECHO pin also gives a 5V signal. The ESP32 S3 Uno pins are NOT 5V tolerant. Put a 5V to 3.3V level converter on the ECHO wire, or use a voltage divider (for example 1 kΩ from ECHO to D7, and 2 kΩ from D7 to GND). You can also use a 3.3V version of the sensor. The TRIG wire needs no change, because a 3.3V pulse is enough to trigger the sensor.

WARNING

Pin D6 is GPIO3 on the ESP32 S3 Uno. It is a boot strapping pin, so the board reads its level at startup. The TRIG input of the sensor normally does not cause a problem. But if the board does not boot or upload, remove the TRIG wire from D6 for a moment, upload the code, then put the wire back.

ESP32 S3 Uno Code

The sketch sends a 10 microsecond pulse to the TRIG pin, then measures how long the ECHO pin stays HIGH. It turns that time into centimeters, switches the LED on or off, and prints the distance to the Serial Monitor every half second.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-ultrasonic-sensor-led */ #define TRIG_PIN 3 // The ESP32 S3 Uno pin GPIO3 (D6) connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 14 // The ESP32 S3 Uno pin GPIO14 (D7) connected to the ultrasonic sensor's ECHO pin #define LED_PIN 17 // The ESP32 S3 Uno pin GPIO17 (D3) connected to LED's pin #define DISTANCE_THRESHOLD 50 // centimeters float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 S3 Uno pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode } 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; if(distance_cm < DISTANCE_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram, with the level converter on the ECHO wire.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into the Arduino IDE.
  6. Upload the code by clicking the Upload button.
Arduino IDE Upload Code
  1. Move your hand slowly toward the sensor, then away from it.
  2. Watch the LED. It turns on when your hand is closer than 50 cm and turns off when it is farther.
  3. Tip: open the Serial Monitor at 9600 baud to see the measured distance.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
distance: 112.45 cm distance: 78.20 cm distance: 41.36 cm distance: 18.92 cm distance: 63.07 cm
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Code Explanation

Each line of the sketch has a comment that explains it. The key line is distance_cm = 0.017 * duration_us. Sound travels about 0.034 cm per microsecond, and the pulse goes to the object and back, so the code multiplies by half of that value. To change when the LED turns on, edit DISTANCE_THRESHOLD.

※ NOTE THAT:

This code is made for learning. Ultrasonic readings can be noisy, and a single bad reading can make the LED flicker. To get stable readings, filter the noise as shown here.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Why does the code use 3, 14 and 17 instead of 6, 7 and 3?

The board has Uno header labels, but the code uses GPIO numbers. Header D6 is GPIO3, D7 is GPIO14, and D3 is GPIO17. So TRIG_PIN is 3, ECHO_PIN is 14, and LED_PIN is 17.

Can I connect the HC-SR04 ECHO pin straight to the ESP32 S3 Uno?

It is not safe. The ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant, but the HC-SR04 gives 5V on ECHO. Use a level converter or a 1 kΩ / 2 kΩ voltage divider on the ECHO wire.

Can I power the HC-SR04 from 3.3V?

The classic HC-SR04 needs 5V and often does not work well at 3.3V. Some newer versions (like the HC-SR04P) work from 3.3V to 5V. With a 3.3V supply, the ECHO signal is already safe for the board.

How do I change the distance that turns on the LED?

Change the value of DISTANCE_THRESHOLD in the code. For example, set it to 20 to turn the LED on only when an object is closer than 20 cm.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to them and write the GPIO number in code. For a second sensor, use GPIO35 to GPIO42 for TRIG and ECHO, and never GPIO47/GPIO48 for ECHO because they are output only. For more LEDs, GPIO35 to GPIO42 are good choices. Be careful with two things. First, GPIO47/GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Does this code work the same as on the UNO R4?

Yes. pulseIn(), digitalWrite() and delayMicroseconds() work the same way on the ESP32 S3 Uno. Only the pin numbers change, and you need a level converter because the board uses 3.3V logic.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or bad cable Install the CP210x or CH340 driver, try a data USB cable, or hold BOOT while pressing RESET
Distance is always 0 cm No echo received or ECHO wire not on D7 Check the ECHO wire and the level converter, and make sure the sensor VCC is on 5V
Distance values jump around Noise or soft object in front of the sensor Point the sensor at a flat hard surface and filter the readings
LED never lights LED is the wrong way round or wrong pin in code Put the long leg (anode) to the resistor and D3, and use 17 in the code
LED is always on Threshold too high or readings stuck at 0 Lower DISTANCE_THRESHOLD and check the sensor wiring
GPIO14 stops working 5V ECHO signal went straight into a 3.3V pin Add a level converter or voltage divider, and try another pin if GPIO14 is damaged
Upload fails or board does not start The TRIG wire on D6 (GPIO3) affects the boot strapping pin Remove the wire from D6, upload the code, then reconnect it
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!