ESP8266 - Motion Sensor - LED Strip

This tutorial instructs you on how to create a seamless lighting automation system using an ESP8266, an HC-SR501 motion sensor, and an LED strip. Tailored to trigger the LED strip upon detecting human presence, this versatile system is well-suited for a range of applications, including:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×HC-SR501 Motion Sensor
1×DotStar RGB LED Strip
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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

If you do not know about LED strip and motion sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:

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

The wiring diagram between ESP8266 NodeMCU and Motion Sensor LED strip

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

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.
esp8266 motion sensor initial setting

ESP8266 Code - Motion Sensor Controls LED strip

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-motion-sensor-led-strip */ #include <Adafruit_DotStar.h> #define NUMPIXELS 144 // Number of LEDs in DotStar strip #define DOTSTAR_DATA_PIN D6 // The ESP8266 pin D6 #define DOTSTAR_CLOCK_PIN D5 // The ESP8266 pin D5 #define MOTION_SENSOR_PIN D1 // The ESP8266 pin 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); // Configure the ESP8266 pin to the 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 = 255; // 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect ESP8266 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.
ESP8266 NodeMCU DotStar library
  • You will be asked to install the dependency. Click Install All button.
ESP8266 NodeMCU DotStar library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Move your hand in front of sensor
  • Check out the LED strip

You can modify the code to add a lighting effect.

Video Tutorial

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