Arduino - Motion Sensor - LED Strip

In this tutorial, we'll explore the implementation of an Arduino, an HC-SR501 motion sensor, and an LED strip to craft a seamless lighting automation system. Specifically designed to activate the LED strip upon detecting human presence, this system is versatile and ideal for various applications, such as:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×HC-SR501 Motion Sensor
1×DotStar RGB LED Strip
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About 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

Arduino Motion Sensor LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

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

Arduino Code - Motion Sensor Controls LED strip

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-led-strip */ #include <Adafruit_DotStar.h> #define NUMPIXELS 144 // Number of LEDs in DotStar strip #define DOTSTAR_DATA_PIN 2 // Arduino pin connected to the data pin of DotStar #define DOTSTAR_CLOCK_PIN 3 // Arduino pin connected to the data pin of DotStar #define MOTION_SENSOR_PIN 12 // Arduino 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); // set arduino 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 Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate 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.
Arduino DotStar library
  • You will be asked to install the dependency. Click Install All button.
Arduino DotStar library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Move your hand in front of sensor
  • Check out the LED strip

You can modify the code to add a lighting effect.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES