ESP32 C3 Super Mini - Motion Sensor - LED Strip

This tutorial shows you how to build a motion sensor LED strip light with the ESP32 C3 Super Mini, an HC-SR501 motion sensor and a DotStar LED strip. The strip turns on when someone moves near the sensor, and turns off when the motion stops. It is a great first lighting automation project for beginners.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Motion Sensor LED Strip

This project fits many places around the home:

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×DotStar RGB LED Strip
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires

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 .

Overview of LED Strip and Motion Sensor

The HC-SR501 is a motion sensor that detects moving people and animals, and a DotStar LED strip is an addressable RGB LED strip that the board controls with two signal wires.

Key Features:

  • Motion Sensor: Senses movement from the infrared heat of people and animals.
  • DotStar LED Strip: Lets the code set the color of every LED.
  • Together: The sensor reports motion, and the ESP32 C3 Super Mini turns that report into light.
  • Strip Choice: You can use a NeoPixel, WS2812B or DotStar LED strip. This tutorial uses DotStar to keep the wiring simple.

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 strip power: 5V, from the power adapter
  • LED strip signals: clock (CI) and data (DI)
  • LEDs in the example code: 144

The motion sensor 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 Strip 5V: power input of the strip
  • LED Strip GND: connect to GND (0V)
  • LED Strip CI: clock input of the strip
  • LED Strip DI: data input of the strip

Wiring Diagram

The 5V power adapter powers the board, the motion sensor and the LED strip through the DC power jack.

  • Note: Connect the wires before you plug in the power adapter.
  • Note: All GND wires must be connected together, or the strip will not work correctly.
The wiring diagram between ESP32 C3 Super Mini Motion Sensor LED Strip

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 C3 Super Mini Pin
Motion Sensor OUTPUT GPIO5
LED Strip DI (Data) GPIO6
LED Strip CI (Clock) GPIO4
DC Power Jack (+) 5V
DC Power Jack (-) GND
Motion Sensor VCC DC Power Jack (+)
Motion Sensor GND DC Power Jack (-)
LED Strip 5V DC Power Jack (+)
LED Strip GND DC Power Jack (-)

Initial Setting

Set up the motion sensor like this before you run the code.

Setting Configuration
Time Delay Adjuster Screw it in anti-clockwise direction fully.
Detection Range Adjuster Screw it in clockwise direction fully.
Repeat Trigger Selector Put jumper as shown on the image.
ESP32 C3 Super Mini motion sensor initial setting

ESP32 C3 Super Mini Code - Motion Sensor Controls LED Strip

The code below lets the motion sensor on GPIO5 control the LED strip on GPIO6 and GPIO4.

What This Code Does:

  • Starts the strip: sets up 144 LEDs and turns them all off.
  • Reads the sensor: digitalRead() checks GPIO5 again and again.
  • Motion starts: when the state changes from LOW to HIGH, all the LEDs turn red.
  • Motion stops: when the state changes from HIGH to LOW, all the LEDs turn off.
  • Reports events: prints each event to the Serial Monitor at 115200 baud.
/* * 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-strip */ #include <Adafruit_DotStar.h> #define NUMPIXELS 144 // Number of LEDs in DotStar strip #define DOTSTAR_DATA_PIN 6 // The ESP32 C3 SuperMini pin GPIO6 connected to the data pin of DotStar #define DOTSTAR_CLOCK_PIN 4 // The ESP32 C3 SuperMini pin GPIO4 connected to the clock pin of DotStar #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin GPIO5 connected to the OUTPUT pin of motion sensor int motion_state = LOW; // current state of motion sensor's pin int prev_motion_state = LOW; // previous state of motion sensor's pin Adafruit_DotStar strip(NUMPIXELS, DOTSTAR_DATA_PIN, DOTSTAR_CLOCK_PIN, DOTSTAR_BRG); void setup() { Serial.begin(115200); strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) strip.show(); // Turn OFF all pixels ASAP strip.setBrightness(255); pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode } void loop() { prev_motion_state = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); // turn on the led strip for (int pixel = 0; pixel < NUMPIXELS; pixel++) { // red color int r = 255; // CHANGE COLOR AS YOUR DESIRE int g = 0; // CHANGE COLOR AS YOUR DESIRE int b = 0; // CHANGE COLOR AS YOUR DESIRE strip.setPixelColor(pixel, g, r, b); // set color for each pixel } strip.show(); } else if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); strip.clear(); // turn off all pixel on LED strip strip.show(); } }

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 everything as shown in the wiring diagram 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.
  • Open Library Manager: Click the Libraries icon on the left bar of the Arduino IDE.
  • Search for the library: Type "Adafruit DotStar", then find the DotStar library by Adafruit.
  • Install the library: Click the Install button.
  • Search for Adafruit DotStar created by Adafruit and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Library Manager
Type:
All
Topic:
All
Adafruit DotStar by Adafruit
Adafruit DotStar LED Library More info
1.2.5
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32C3 Dev Module on COM15
1
  • Install the dependency: When you are asked to install the dependency, click the Install All button.
  • 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 strip: See the LED strip 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! Motion stopped! Motion detected! Motion stopped!
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2
  • Pro Tip: Is your strip not 144 LEDs long? Change NUMPIXELS at the top of the code to match your strip.

You can also modify the code to add a lighting effect.

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 ESP32 C3 Super Mini motion sensor LED strip idea can light many real places.

  • Staircase Light: Light the steps automatically as people come near.
  • Under-Bed Night Light: Turn on a soft light when you step down from the bed.
  • Christmas Tree Lights: Turn on the tree lights only when people are around.
  • Hallway Light: Light the way when someone walks past.
  • Closet Light: Turn on the strip when you reach into a closet.
  • Shelf Display: Light up a display shelf when someone comes close.

Challenge Yourself

Try these small upgrades to learn more about the ESP32 C3 Super Mini and your LED strip.

  • Easy: Change the strip color by editing the r, g and b values in the code.
  • Easy: Lower the brightness with strip.setBrightness().
  • Medium: Keep the strip on for 10 seconds after the motion stops, using millis().
  • Medium: Light the LEDs one by one when motion starts, to make a wipe effect.
  • Advanced: Change the code to use a NeoPixel or WS2812B LED strip instead of DotStar.
  • Advanced: Add a button on GPIO7 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!