[Arduino] – Motion night lamp

Last night i destroyed everything in my kitchen to not turn on the light, and after that i decide to make this stupid motion night lamp.

It’s a simple RGB led lamp that power on when the room is dark and some motions are detected.

How it works?

It’s made using a Photoresistor and a Pyro sensor.

Photoresistor recognize if the light is on or off and the Pyro detect if there are some movements. If both condition are verified, RGB led start fading colours until motions stops or light is on.

Parts used:
  • 3) RGB leds
  • 3) 330ohm resistors
  • 1) 10Kohm resistor
  • 1) Photocell
  • 1) Pyro sensor
  • 1) Arduino (of course)

[ Click image to enlarge ]
Schema_bb

Before start coding, connect all the parts like the image upper.
RGB leds goes to Arduino digital pin 131211. Anodes go to GND.
Photocell go to Arduino analog A0 pin.
Pyro go to Arduino digital 2 pin.

Ready?

Arduino C Code

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/*
Motion night lamp
Alberto Pasca
*/

// led pins
int redPin   = 11;
int greenPin = 12;
int bluePin  = 13;
//
// photo resistor pin
int photoPin = A0;
//
int lightLevel = 0;
int minLight, maxLight;
// pyro pin
int piroPin  = 2;
//
int pirState = LOW;

void setup()
{
pinMode ( redPin,   OUTPUT );
pinMode ( greenPin, OUTPUT );
pinMode ( bluePin,  OUTPUT );

lightLevel = analogRead ( photoPin );
minLight = lightLevel – 20;
maxLight = lightLevel;

pinMode ( piroPin,  INPUT );
}

void loop()
{
int nigthMode = nightMode ();

if ( nigthMode == 0 ) powerOn ( LOW );
else
{
int motionDetected = motion ();
powerOn ( nigthMode && motionDetected );
}

delay ( 1000 );
}[/code]

Powering led(s)

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]void powerOn ( int off )
{
if ( off == LOW )
{
analogWrite ( bluePin,  0 );
analogWrite ( redPin,   0 );
analogWrite ( greenPin, 0 );
}
else fade();
}[/code]

Detect if light is ON or not

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]int nightMode ()
{
lightLevel = analogRead ( photoPin );
if ( minLight > lightLevel ) minLight = lightLevel;
if ( maxLight < lightLevel ) maxLight = lightLevel;

int med = map ( lightLevel, minLight, maxLight, 0, 100 );
return med < 20;
}[/code]

Detect a motion

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]int motion()
{
int ret = 0;
int val = digitalRead ( piroPin );

if ( val == HIGH ) {
ret = 1;
if ( pirState == LOW ) pirState = HIGH;
} else {
ret = 0;
if ( pirState == HIGH ) pirState = LOW;
}

return ret;
}[/code]

Led fading

[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]void fade()
{
for ( int i = 0 ; i &lt; 255 ; i += 1 )
{
…  // this is for you, enjoy!
}
}[/code]

Final result:
http://goo.gl/0FLrN7

My lamp is this… obv that is under construction!

arduino mood lamp

cheers.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.