Ingo Lohs
Published © GPL3+

PIR-Sensor Activated RGB Strip Controlled by Uno R3

At my age you have to get up at night and tend to be tired on the way to the bathroom. Use a PIR-Sensor to trigger the light on!

BeginnerShowcase (no instructions)2 hours5,045
PIR-Sensor Activated RGB Strip Controlled by Uno R3

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Seeed Studio Grove Base Shield
×1
Seeed Studio LED Strip Driver
×1
Seeed Studio Light Sensor
×1
Seeed Studio Universal 4 Pin Cable
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
DC connector
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor

Story

Read more

Code

MyPIR with Grove Components on Arduino Uno

C/C++
// BOM
// 1x Arduino UNO R3
// 1x Grove Base Shield V2 for Arduino from seeedstudio.com
// 1x Grove LED Strip Driver V1.2 from seeedstudio.com
// 1x Grove Light Sensor V1.2 from seeedstudio.com
// 1x PIR Sensor Standard
// 1x one colored Standard LED
// 6x Jumper-cable for Standard LED and PIR Sensor
// 2x Grove 4pin Jumper from seeedstudio.com to plug there components
// 1x 12V 1A or 2A power output to power LED Strip
// 1x 5V power jack to power your Arduino Uno
// 1x power adapter from power jack (12V) to connect jumpers to power an LED Strip Driver
// 1x 4pin Standard cable to connect from LED Strip Driver to analog RGB Strip
// in my case I soldered the 4pin cable to the analog RBG Strip
// for flashing from your Desktop (create.arduino.cc) to Arduino UNO is an USB-cable necessary

// MyPIR - v1.0 - Ingo Lohs - 17.04.2017

// LEDStripDriver - Version: Latest from seeedstudio - download libary here: http://wiki.seeed.cc/Grove-LED_Strip_Driver/
#include <RGBdriver.h>

#define CLK 2 // pin definitions for the driver        
#define DIO 3
RGBdriver Driver(CLK,DIO);           // create instance for LED Driver Stripe

const int inputPin = 8;              // choose the input pin (for PIR sensor)
const int ledPin = 4;                // Standard LED connected to digital pin 4 to check that PIR Sensor works
int pirState = LOW;                  // we start, assuming no motion detected
int val = 0;                         // variable for reading the pin status
int analogvalue;                     // variable for environment light
int calibrateTime = 10000;           // wait for the thingy to calibrate
float light_threshold = 200;         // max brightness is presents with value 1304; decide its time for more light - adjust this value according to your needs
#define photoresistor A0             // photoresistor for reading analog values - bought by seeedstudio.com
 
void setup() {
  Serial.begin(115200);
    
  pinMode(photoresistor,INPUT);      // Our photoresistor pin is input     
 
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW); 

    // LED Stripe off
  Driver.begin(); // begin
  Driver.SetColor(0, 0, 0); // set analog RBG Strip off
  Driver.end();
  
}
 
void loop() {
  
  // check to see what the value of the photoresistor is and store it in the int variable analogvalue
  analogvalue = analogRead(photoresistor);
  Serial.println(analogvalue); // --> output to Serial Monitor 
  delay(100);
  
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();
  // report it out, if the state has changed
    reportTheData();
  }
}
 

void readTheSensor() {
  val = digitalRead(inputPin);
   Serial.println(digitalRead(inputPin));
}


bool calibrated() {
  return millis() - calibrateTime > 0;
}


void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected - Control LED is on, but analogvalue from environment > light_threshold");  

      pirState = HIGH;
      setLED( pirState );

        if (analogvalue < light_threshold) {
                Serial.println("Motion detected - spot lights on");
                ColorFade();
        } //else {

        //}

    }
    
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}


void setLED( int state )
{
  digitalWrite(ledPin,state);
}


void ColorFade() {

  // LED Stripe on
  Driver.begin(); // begin
  Driver.SetColor(255, 255, 255); //all RGB colors on
  Driver.end();
  
  delay(15000); // 15 sec on
  
  // LED Stripe off
  Driver.begin();
  Driver.SetColor(0, 0, 0); // all RGB colors off
  Driver.end();
  
}

Credits

Ingo Lohs

Ingo Lohs

182 projects • 194 followers
I am well over 50 years and come from the middle of Germany.

Comments