ESP32 C3 Super Mini - Motion Sensor - LED
This tutorial shows you how to use the ESP32 C3 Super Mini with an HC-SR501 motion sensor and an LED. The board turns the LED on when motion is detected, and turns it off when motion stops. It is a simple, fun project for beginners.
In this tutorial, you'll learn:
- How to wire the HC-SR501 motion sensor and an LED to the ESP32 C3 Super Mini
- How to set up the motion sensor before you use it
- How to detect when motion starts and when it stops
- How to turn the LED on and off from the motion sensor
- How to watch the motion events on the Serial Monitor

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 Motion Sensor
The HC-SR501 is a motion sensor that detects moving people and animals, and an LED is a small light you can turn on and off.
Key Features:
- Motion Sensor: Senses movement from the infrared heat of people and animals.
- LED: Turns on and off with a digital signal from the board.
- Together: The sensor reports motion, and the board turns that report into light.
Technical Specifications:
- Motion sensor power: 5V
- Motion sensor output: digital, HIGH when motion is detected and LOW when there is no motion
- Detection range: up to 7 meters
- LED resistor: 220 ohm
Each part has its own tutorial with the pinout, how it works, wiring and code:
Pinout
Here are the pins you will connect in this project.
- Motion Sensor VCC: connect to 5V
- Motion Sensor GND: connect to GND (0V)
- Motion Sensor OUTPUT: sends the HIGH or LOW signal to the digital input pin
- LED Anode (+): the longer leg, controlled by the output pin through the resistor
- LED Cathode (-): the shorter leg, connects to GND
Wiring Diagram
The motion sensor uses three wires, and the LED uses two wires plus a resistor.
- Note: The HC-SR501 needs 5V on its VCC pin for reliable operation.
- Note: Always use the 220 ohm resistor with the LED, or the LED can burn out.
| Component Pin | ESP32 C3 Super Mini Pin |
|---|---|
| Motion Sensor VCC | 5V |
| Motion Sensor GND | GND |
| Motion Sensor OUTPUT | GPIO5 |
| LED Anode (+) | GPIO3 (through 220 ohm resistor) |
| LED Cathode (-) | GND |
The wiring diagram with power supply from USB cable:

This image is created using Fritzing. Click to enlarge image
The wiring diagram with power supply from 5V adapter:

This image is created using Fritzing. Click to enlarge image
Initial Setting
Set up the motion sensor like this before you run the code.
| Setting | Configuration |
|---|---|
| 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 C3 Super Mini Code
The code below lets the motion sensor on GPIO5 control the LED on GPIO3.
What This Code Does:
- Starts Serial: opens the Serial Monitor at 115200 baud.
- Sets the pins: GPIO5 is an input for the sensor, and GPIO3 is an output for the LED.
- Reads the sensor: digitalRead() checks the sensor state again and again.
- Motion starts: when the state changes from LOW to HIGH, the LED turns on.
- Motion stops: when the state changes from HIGH to LOW, the LED turns off.
Detailed Instructions
- New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
- Wire it up: Connect the motion sensor and the LED as shown above.
- Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Type-C cable.
- Open the IDE: Open Arduino IDE on your computer.
- Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
- Select the port: Pick the COM port of your board in Tools > Port.
- Copy the code: Copy the code above and paste it into Arduino IDE.
- Upload: Click the Upload button on Arduino IDE.

- Open Serial Monitor: Open it on Arduino IDE and set the baud rate to 115200.

- Test it: Move your hand in front of the motion sensor.
- Watch the LED: See the LED turn on, then turn off when the motion stops.
- Check the result: See the events on the Serial Monitor. It looks like the below:
- Pro Tip: If the LED stays on or acts strangely right after power-on, wait about one minute. The motion sensor needs a short time to settle.
Line-by-line Code Explanation
The above ESP32 C3 Super Mini code contains line-by-line explanation. Please read the comments in the code!
Project Ideas
This motion sensor LED idea can control many real things.
- Automatic Hallway Light: Light the way when someone walks past.
- Closet Light: Turn on a small light when you reach into a closet.
- Night Stair Light: Show the steps only when someone is moving.
- Intruder Indicator: Light a warning LED when someone enters a room.
- Energy Saver: Keep a small light off when nobody is around.
Challenge Yourself
Try these small upgrades to learn more about the ESP32 C3 Super Mini.
- Easy: Reverse the logic, so the LED is on when there is no motion.
- Easy: Count the motion events and print the total on the Serial Monitor.
- Medium: Keep the LED on for 10 seconds after the motion stops, using millis().
- Medium: Make the LED blink instead of staying on while there is motion.
- Advanced: Add a second LED on GPIO7 that turns on when there is no motion.
- Advanced: Add a button on GPIO6 that turns the motion light feature on and off.