ESP32 S3 - Motion Sensor - LED
This tutorial shows you how to use the ESP32 S3 together with an HC-SR501 PIR motion sensor to control an LED automatically based on detected movement. By wiring a passive infrared sensor to a GPIO input and an LED to a GPIO output, you will gain a practical understanding of how to respond to real-world motion events with a visual indicator on the ESP32 S3.
What you'll build:
- A circuit connecting an HC-SR501 PIR motion sensor and an LED to the ESP32 S3
- A sketch that continuously reads the motion sensor output pin on the ESP32 S3
- A system that automatically turns the LED on when motion is detected and turns it off when no motion is present
- A foundation for building motion-activated alarms, automatic lighting systems, and presence-detection 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 Motion Sensor
The HC-SR501 PIR motion sensor and an LED are an effective pairing for building motion-detection projects on the ESP32 S3. The sensor detects changes in infrared radiation caused by a person or animal moving within its field of view, outputs a HIGH signal on its data pin, and the ESP32 S3 reads that signal 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 motion 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
Loose wires are a common cause of strange bugs. Put the ESP32 S3 on a Screw Terminal Block for ESP32 S3, then tighten each wire under a screw. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

This image is created using Fritzing. Click to enlarge image
Another choice is a GPIO Breakout Board for ESP32 S3. Each GPIO has a signal pin with 3.3V and GND pins next to it, as shown below:

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-SR501 motion sensor requires a supply voltage between 5V and 20V on its VCC pin; use the 5V output pin available on the ESP32 S3 board for powering the sensor, while keeping the sensor's output signal line at a level compatible with the ESP32 S3's 3.3V GPIO input.
Initial Setting
| Detection Range Adjuster | Fully screw it in the clockwise direction. |
| Time Delay Adjuster | Fully screw it in the anti-clockwise direction. |
| Repeat Trigger Selector | Put jumper like the below image. |

ESP32 S3 Code
The following code reads the output pin of the HC-SR501 motion sensor on the ESP32 S3 and turns the LED on immediately when motion is detected, then turns it off once the sensor output returns LOW. The sensor's onboard time-delay trimmer controls how long the output stays HIGH after motion is detected, so you can adjust that hardware setting to change the LED hold time without modifying the code.
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.

- Move your hand in front of the motion sensor.
- Observe the LED turn on when motion is detected.
- Stay still and observe the LED turn off after the sensor's time-delay period expires.
- Check the Serial Monitor to see the live motion state printed in real time.
- Pro Tip: Adjust the two onboard trimmers on the HC-SR501 — the sensitivity trimmer sets the detection range (up to about 7 metres) and the time-delay trimmer sets how long the output stays HIGH after motion stops, so fine-tune both for your specific installation environment before finalising your project.
Serial Monitor Output
The following Serial Monitor output shows the ESP32 S3 reading motion data from the HC-SR501 sensor in real time. Each line displays the current motion state and the corresponding LED state triggered by the sensor output.
Application and Project Ideas
Once you can trigger an LED from a motion sensor on the ESP32 S3, you can extend this pattern to a wide range of motion-activated and presence-detection projects.
- Automatic room lighting: Turn on a relay-controlled light when someone enters a room and switch it off automatically after the time-delay period expires.
- Security alert system: Replace the LED with a piezo buzzer or relay-connected siren to trigger an audible alarm whenever motion is detected in a restricted area.
- Motion-activated camera: Use the motion detection signal to trigger a camera module or send a notification to capture images when presence is detected.
- Energy-saving display: Wake an OLED or LCD screen from sleep only when someone approaches the device, saving power in battery-operated projects.
- Pet activity monitor: Track when and how often a pet moves through a specific area by logging motion events with timestamps to an SD card.
- Smart doorbell: Detect when someone approaches the front door and trigger a buzzer, send an email notification, or log the event to a web server.
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 a motion sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable motion-detection systems.
- Beginner: Add a Serial Monitor message that prints the exact date and time (using millis() as a counter) each time motion is first detected, so you can track detection events over a session.
- Beginner: Add a second LED wired to a different GPIO pin and make it blink rapidly while the primary LED stays solid on, giving a distinct visual pattern to indicate active motion detection.
- Intermediate: Add a push button that allows the user to enable or disable the motion detection feature at runtime, with the LED indicating the current enable/disable state when no motion is present.
- Intermediate: Connect an I2C OLED display to the ESP32 S3 and show a live counter of how many motion events have occurred in the current session alongside the current detection state.
- Advanced: Send an email or MQTT message to a home automation platform whenever the motion sensor is triggered, using the ESP32 S3 Wi-Fi connection to report events remotely.
- Advanced: Build a multi-zone motion detection system using two HC-SR501 sensors on separate GPIO pins and use the combination of their outputs to determine the direction of movement across the monitored area.