ESP32 S3 - Ultrasonic Sensor - LED
This tutorial shows you how to use the ESP32 S3 together with an ultrasonic sensor to control an LED automatically based on the distance to a nearby object. By combining a non-contact distance measurement with a simple visual indicator, you will gain a practical understanding of how to trigger GPIO outputs in response to real-world sensor data on the ESP32 S3.
What you'll build:
- A circuit connecting an HC-SR04 ultrasonic sensor and an LED to the ESP32 S3
- A sketch that continuously measures the distance to an object using the ultrasonic sensor
- A system that automatically turns the LED on when an object is within a close threshold distance
- A foundation for building proximity-triggered alarms, presence detectors, and obstacle-avoidance projects
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 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
The HC-SR04 ultrasonic sensor and an LED are an effective pairing for building proximity-detection projects on the ESP32 S3. The sensor emits an ultrasonic pulse, measures the time it takes to reflect back from a nearby object, and the ESP32 S3 converts that timing value into a distance reading that is then used to switch the LED on or off via a digital GPIO output.
Key Specifications
We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:
Wiring Diagram
Connect the ultrasonic sensor and LED to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, paying particular attention to the LED polarity and the 220 ohm current-limiting resistor placement.

This image is created using Fritzing. Click to enlarge image
For a project that lasts, screw terminals hold the wires better than jumper wires. You can plug the ESP32 S3 into a Screw Terminal Block for ESP32 S3 and connect the wires to its screw terminals. The pins do not change. Just connect each wire to the matching screw terminal:

This image is created using Fritzing. Click to enlarge image
You can also use a GPIO Breakout Board for ESP32 S3. It gives each GPIO its own S, 3.3V and GND pins, so module wires plug straight in:

This image is created using Fritzing. Click to enlarge image
Safety Notes
Always include the 220 ohm current-limiting resistor in series with the LED to avoid drawing excessive current through the ESP32 S3 GPIO pin, which can permanently damage the microcontroller. The HC-SR04 ultrasonic sensor operates at 5V on its VCC pin; if your ESP32 S3 board supplies a 5V output pin, use it for the sensor's VCC and keep all signal lines (Trig and Echo) at the 3.3V logic level that the ESP32 S3 GPIO pins expect.
| Component | Pin | ESP32 S3 Pin |
|---|---|---|
| Ultrasonic Sensor | VCC | 5V |
| Ultrasonic Sensor | GND | GND |
| Ultrasonic Sensor | Trig | GPIO8 |
| Ultrasonic Sensor | Echo | GPIO9 |
| LED | Anode (+) | GPIO38 (via 220Ω resistor) |
| LED | Cathode (-) | GND |
ESP32 S3 Code
The following code triggers an ultrasonic pulse on the ESP32 S3, measures the round-trip echo time, converts it to a distance in centimetres, and switches the LED on when the measured distance falls below a defined threshold. The threshold value can be adjusted directly in the code to suit different proximity detection requirements.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the components following the wiring diagram shown above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to the ESP32 S3.

- Open the Serial Monitor at 115200 baud.
- Move your hand toward the ultrasonic sensor and observe the LED turn on.
- Move your hand away from the sensor and observe the LED turn off.
- Check the Serial Monitor to see the live distance readings printed in real time.
- Pro Tip: Try adjusting the distance threshold value in the code to fine-tune the detection sensitivity — a lower threshold triggers the LED only for very close objects, while a higher value activates it from further away, which is useful for different proximity alarm scenarios.
Serial Monitor Output
The following Serial Monitor output shows the ESP32 S3 reading distance data from the ultrasonic sensor in real time. Each line displays the current measured distance and the corresponding LED state triggered by the proximity threshold.
Application and Project Ideas
Once you can trigger an LED from an ultrasonic distance reading on the ESP32 S3, you can extend this pattern to a wide range of proximity-sensitive projects.
- Parking assistant: Mount the sensor in a garage and light an LED strip when a vehicle enters the safe stopping zone.
- Obstacle-avoidance robot: Trigger a motor stop or direction change when the sensor detects a wall or object within a set distance.
- Touchless hand sanitiser dispenser: Activate a pump relay when a hand is detected within a few centimetres of the sensor nozzle.
- Security proximity alarm: Sound a piezo buzzer instead of lighting an LED to alert when someone enters a restricted zone.
- Liquid level monitor: Mount the sensor above a tank to detect when the liquid level drops below a critical point and trigger a refill pump.
- Automatic staircase lighting: Use multiple sensors along a staircase to switch LED strips on and off as a person walks up or down.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenge Yourself
Now that you can control an LED with an ultrasonic sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable proximity detection systems.
- Beginner: Modify the threshold distance value in the code and observe how it changes the point at which the LED switches on, then print a message to the Serial Monitor confirming which threshold is active.
- Beginner: Add a second LED wired to a different GPIO pin and make it light up at a different (farther) threshold, so you have two proximity zones with separate visual indicators.
- Intermediate: Replace the simple on/off LED behaviour with PWM brightness control so the LED gradually brightens as the object moves closer and dims as it moves away.
- Intermediate: Add an I2C OLED screen to display the live distance reading and LED state in a formatted readout, removing the dependency on the Serial Monitor.
- Advanced: Store a configurable threshold value in ESP32 S3 non-volatile storage (Preferences library) so the setting persists across power cycles without re-uploading code.
- Advanced: Stream the distance readings over WiFi to a simple web page served by the ESP32 S3, and control the detection threshold remotely from a browser slider.