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:

  1. A circuit connecting an HC-SR501 PIR motion sensor and an LED to the ESP32 S3
  2. A sketch that continuously reads the motion sensor output pin on the ESP32 S3
  3. A system that automatically turns the LED on when motion is detected and turns it off when no motion is present
  4. A foundation for building motion-activated alarms, automatic lighting systems, and presence-detection 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×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion 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 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.

The wiring diagram between ESP32 S3 Motion Sensor LED

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.

The wiring diagram between ESP32 S3 Motion Sensor LED Screw Terminal

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:

The wiring diagram between ESP32 S3 Motion 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-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 motion sensor initial setting

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.

/* * 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-motion-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-motion-sensor-led */ #define MOTION_SENSOR_PIN 42 // ESP32-S3 pin GPIO42 connected to the OUTPUT pin of motion sensor #define LED_PIN 38 // ESP32-S3 pin GPIO38 connected to LED's pin int motionStateCurrent = LOW; // current state of motion sensor's pin int motionStatePrevious = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32-S3 pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32-S3 pin to output mode } void loop() { motionStatePrevious = motionStateCurrent; // store old state motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!, turns LED ON"); digitalWrite(LED_PIN, HIGH); // turn on } else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!, turns LED OFF"); digitalWrite(LED_PIN, LOW); // turn off } }

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. Move your hand in front of the motion sensor.
  2. Observe the LED turn on when motion is detected.
  3. Stay still and observe the LED turn off after the sensor's time-delay period expires.
  4. Check the Serial Monitor to see the live motion state printed in real time.
  5. 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.

∞
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] Motion: NOT DETECTED | LED: OFF [2026-06-16 10:22:02] Motion: DETECTED | LED: ON [2026-06-16 10:22:03] Motion: DETECTED | LED: ON [2026-06-16 10:22:04] Motion: NOT DETECTED | LED: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

  1. 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.
  2. 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.
  3. Motion-activated camera: Use the motion detection signal to trigger a camera module or send a notification to capture images when presence is detected.
  4. Energy-saving display: Wake an OLED or LCD screen from sleep only when someone approaches the device, saving power in battery-operated projects.
  5. 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.
  6. 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.

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

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