Extending the test code I built-in yesterdays LED post, I’ve added a PIR motion sensor and a LDR (light detection) What we now have is when motion is detected AND the light level is below a certain level the LED’s will fade on to full brightness, stay on until motion is no longer detected and then fade down until off. A bit sexier than just on and off 🙂 I plan on setting something like this up using LED strips or the 2W LED’s. Living in an older house means that there’s not as many light switches nor sometimes enough lighting in the first place. Some of the hallways and outside entrances could really benefit from some sexy automatic lighting. I will rig something together temporarily soon(tm) to get the light thresholds correct before designing a nice encased unit.
Below is the code as it currently stands. Feel free to recommend improvements!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #define LEDPin 6 //pin feeding TIP120 #define onboardLED 13 //onboard LED for some debugging #define motionPin 4 //PIR connection #define fadeSpeed 10 //Adjust how fast slow you want the fade effect to function #define lightPin 5 //LDR connection int led; int motion; int counter = 0; int light; void setup() { //set pinmodes pinMode(LEDPin, OUTPUT); pinMode(onboardLED, OUTPUT); pinMode(motionPin, INPUT); pinMode(lightPin, INPUT); Serial.begin(9600); //start-up serial for outputting debug info int motion = LOW; led = 0; } void loop() { light = analogRead(lightPin); //read light levels Serial.print("Light Level: "); Serial.println(light); //send light levels to serial motion = digitalRead(motionPin); //read motion from PIR Serial.print("Motion: "); Serial.println(motion); //send motion state to serial Serial.println(led); if((light > 500) && (motion == 1)) { //if motion detected and light level below a certain level, do this while (led < 255) { //led below full brightness? led++; //increment fade up to full powahs analogWrite(LEDPin, led); //write change to LED control Pin into TIP120 delay(fadeSpeed); break; //need to try this remarked out when arduino is out of testing location } } else if((motion == 0) && (led > 0)) { //otherwise if motion not detected and led value is on, fade down led--; analogWrite(LEDPin, led); delay(fadeSpeed); } else { // delay(250); // Serial.println("delay"); } } |
Links to some Parts:
2W LEDS – DealExtreme
PIR Sensor – Deal Extreme
TIP120 Power Darlington Transistors – Adafruit
Leave a Reply