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:
- Wire an HC-SR04 ultrasonic sensor and an LED to the ESP32 S3 Uno
- Protect the 3.3V pin from the 5V ECHO signal
- Measure distance with pulseIn() in Arduino code
- Turn an LED on when an object is closer than a set distance

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.

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.

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.
Detailed Instructions
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram, with the level converter on the ECHO wire.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into the Arduino IDE.
- Upload the code by clicking the Upload button.

- Move your hand slowly toward the sensor, then away from it.
- Watch the LED. It turns on when your hand is closer than 50 cm and turns off when it is farther.
- Tip: open the Serial Monitor at 9600 baud to see the measured distance.
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 |