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:

ESP32 C3 Super Mini - Motion Sensor LED

Hardware Preparation

1×ESP32 C3 Super Mini
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

Or you can buy the following kits:

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 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:

The wiring diagram between ESP32 C3 Super Mini Motion Sensor LED

This image is created using Fritzing. Click to enlarge image

The wiring diagram with power supply from 5V adapter:

The wiring diagram between ESP32 C3 Super Mini Motion Sensor LED  5V power 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 motion sensor initial setting

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.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-motion-sensor-led */ #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin connected to the OUTPUT pin of motion sensor #define LED_PIN 3 // The ESP32 C3 SuperMini pin 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(115200); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32 C3 SuperMini 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

  • 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.
How to upload ESP32 C3 Super Mini code on Arduino IDE
  • Open Serial Monitor: Open it on Arduino IDE and set the baud rate to 115200.
How to open serial monitor on Arduino IDE
  • 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:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
Motion detected!, turns LED ON Motion stopped!, turns LED OFF Motion detected!, turns LED ON Motion stopped!, turns LED OFF
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2
  • 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.

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