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:

  1. A circuit connecting an HC-SR04 ultrasonic sensor and an LED to the ESP32 S3
  2. A sketch that continuously measures the distance to an object using the ultrasonic sensor
  3. A system that automatically turns the LED on when an object is within a close threshold distance
  4. A foundation for building proximity-triggered alarms, presence detectors, and obstacle-avoidance projects

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera 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×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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

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.

The wiring diagram between ESP32 S3 Ultrasonic Sensor LED

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:

The wiring diagram between ESP32 S3 Ultrasonic Sensor LED 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:

The wiring diagram between ESP32 S3 Ultrasonic Sensor LED GPIO Breakout Board

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.

/* * 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-led */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-ultrasonic-sensor-led */ #define TRIG_PIN 8 // ESP32-S3 pin GPIO8 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 9 // ESP32-S3 pin GPIO9 connected to Ultrasonic Sensor's ECHO pin #define LED_PIN 38 // ESP32-S3 pin GPIO38 connected to LED's pin #define DISTANCE_THRESHOLD 50 // centimeters // variables will change: float duration_us, distance_cm; void setup() { Serial.begin (115200); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32-S3 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32-S3 pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32-S3 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 ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Copy the above code and paste it into Arduino IDE.
  7. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Open the Serial Monitor at 115200 baud.
  2. Move your hand toward the ultrasonic sensor and observe the LED turn on.
  3. Move your hand away from the sensor and observe the LED turn off.
  4. Check the Serial Monitor to see the live distance readings printed in real time.
  5. 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.

∞
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
[2026-06-16 10:22:01] Distance: 85 cm | LED: OFF [2026-06-16 10:22:02] Distance: 42 cm | LED: OFF [2026-06-16 10:22:03] Distance: 18 cm | LED: ON [2026-06-16 10:22:04] Distance: 9 cm | LED: ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

  1. Parking assistant: Mount the sensor in a garage and light an LED strip when a vehicle enters the safe stopping zone.
  2. Obstacle-avoidance robot: Trigger a motor stop or direction change when the sensor detects a wall or object within a set distance.
  3. Touchless hand sanitiser dispenser: Activate a pump relay when a hand is detected within a few centimetres of the sensor nozzle.
  4. Security proximity alarm: Sound a piezo buzzer instead of lighting an LED to alert when someone enters a restricted zone.
  5. 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.
  6. 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

※ 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!