ESP32 - Motion Sensor - LED Strip

In this guide, we will delve into creating a seamless lighting automation system using an ESP32, an HC-SR501 motion sensor, and an LED strip. This system is expertly designed to activate the LED strip upon detecting human presence, making it versatile and well-suited for a variety of applications, including:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×HC-SR501 Motion Sensor
1×DotStar RGB LED Strip
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to LED Strip and Motion Sensor

Unfamiliar with LED strip and motion sensor, including their pinouts, functionality, and programming? Explore comprehensive tutorials on these topics below:

You have the flexibility to use either NeoPixel, WS2812B, or DotStar LED Strips. For the sake of simplicity in wiring, this tutorial specifically uses the DotStar LED Strip. Adapting the code for other LED strip types is straightforward, simply refer to the tutorials above for guidance.

Wiring Diagram

ESP32 Motion Sensor LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
esp32 motion sensor initial setting

ESP32 Code - Motion Sensor Controls LED strip

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-motion-sensor-led-strip */ #include <Adafruit_DotStar.h> #define NUMPIXELS 144 // Number of LEDs in DotStar strip #define DOTSTAR_DATA_PIN 16 // The ESP32 pin GPIO16 connected to the data pin of DotStar #define DOTSTAR_CLOCK_PIN 17 // The ESP32 pin GPIO17 connected to the clock pin of DotStar #define MOTION_SENSOR_PIN 25 // The ESP32 pin GPIO25 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(9600); strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) strip.show(); // Turn OFF all pixels ASAP strip.setBrightness(255); pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 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(); } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “Adafruit DotStar”, then find the DotStar library by Adafruit
  • Click Install button to install DotStar library.
ESP32 DotStar library
  • You will be asked to install the dependency. Click Install All button.
ESP32 DotStar library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Move your hand in front of sensor
  • Check out the LED strip

You can modify the code to add a lighting effect.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES