Introduction: Quick and Easy Arduino Nightlight
Honestly, how many people have been in this scenario (leave a yes or no in the comments) : It is 2:30 in the morning and you wake up with a need to do something whatever that may be (bathroom, forgotten homework assignment, project due in 5 or 6 hours, etc.) it takes you 5-10 minutes to find the light switch (give or take...)
My mother likes a night light for the bathroom but she didn't want one that stayed on all night so I needed one that could detect a person. Many thoughts popped up into my head on what sensors to use. I could us esuch as the Linescan Imaging Sensor Daughterboard by Parallax but that was overkill and too expensive. A cheaper solution would be to use a few ir leds with ir phototransistor but still too complicated. I also thought of the Ping Sensor from Parallax but too hard to program with my beginner standards in C and, it used too much power. (I'm more of a Spin and Assembly language person.) I finally looked through my sensors box and found...(...drumroll please...) ...Parallax's Passive Infared Motion Sensor! It was fairly inespensive ($9.99), it could work in the dark, and, I have had experience in programming these!
My mother likes a night light for the bathroom but she didn't want one that stayed on all night so I needed one that could detect a person. Many thoughts popped up into my head on what sensors to use. I could us esuch as the Linescan Imaging Sensor Daughterboard by Parallax but that was overkill and too expensive. A cheaper solution would be to use a few ir leds with ir phototransistor but still too complicated. I also thought of the Ping Sensor from Parallax but too hard to program with my beginner standards in C and, it used too much power. (I'm more of a Spin and Assembly language person.) I finally looked through my sensors box and found...(...drumroll please...) ...Parallax's Passive Infared Motion Sensor! It was fairly inespensive ($9.99), it could work in the dark, and, I have had experience in programming these!
Step 1: Get the Required Materials.
1 of everything except where noted.
Arduino
PIR Motion Sensor from Parallax
LED ( Preferably Ultra Bright White. I bought mine along with other colors at Ebay from this seller: http://myworld.ebay.com/led.shop*2010/?_trksid=p4340.l2559
They will compile your own personal mix of colors if you want them to...they will charge more but it is a personalized order. I also found this order of only white LEDs but very cheap.
http://www.ebay.com/itm/100pcs-5mm-Ultra-Bright-White-LEDs-200pcs-resistors-/220610858794?pt=LH_DefaultDomain_0&hash=item335d6e8f2a#ht_2715wt_1017
Many jumper wires
Optoisolator/ optocoupler/ relay
Resistor. Define the value yourself.
Arduino
PIR Motion Sensor from Parallax
LED ( Preferably Ultra Bright White. I bought mine along with other colors at Ebay from this seller: http://myworld.ebay.com/led.shop*2010/?_trksid=p4340.l2559
They will compile your own personal mix of colors if you want them to...they will charge more but it is a personalized order. I also found this order of only white LEDs but very cheap.
http://www.ebay.com/itm/100pcs-5mm-Ultra-Bright-White-LEDs-200pcs-resistors-/220610858794?pt=LH_DefaultDomain_0&hash=item335d6e8f2a#ht_2715wt_1017
Many jumper wires
Optoisolator/ optocoupler/ relay
Resistor. Define the value yourself.
Step 2: Program.
Program the code into the Arduino.
This will make the Arduino read the sensor's output and trigger an LED for seven seconds and turn off. Modify to meet your needs. It takes 30 seconds to calibrate the sensor.
Use this link and download open the editor and program.
https://www.rapidshare.com/#!download|833tl3|3411132297|Sketch.pde|1|R~2964C5D605113ECA37CDDB4ED2C02EFB|0|0
If you have trouble...here it is!
//be sure the project box is insulated or false motion triggers will happen
int calibrationTime = 30;
int Time = 0;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockinHigh = false;
int LEDsop = 13; // pin 13 includes resistor sop- signal out pin
int outpin = 12; // for any other device that doesn't require resistor
int SensorInpin = 2; // can vary, must be digital from 1-12
boolean sensorready = false;
void setup (){
pinMode (LEDsop, OUTPUT);
digitalWrite (LEDsop, LOW);
pinMode (outpin, OUTPUT);
digitalWrite (outpin, LOW);
pinMode (SensorInpin, INPUT);
pinMode (0, INPUT);
pinMode (1, INPUT);
pinMode (3, INPUT);
pinMode (4, INPUT);
pinMode (5, INPUT);
pinMode (6, INPUT);
pinMode (7, INPUT);
pinMode (8, INPUT);
pinMode (9, INPUT);
pinMode (10, INPUT);
pinMode (11, INPUT);
do {
digitalWrite (LEDsop, HIGH);
delay (500);
digitalWrite (LEDsop, LOW);
delay (500);
Time = Time + 1;
}
while (Time < calibrationTime);
digitalWrite (LEDsop, LOW); //if led turns off, sensor is ready
sensorready = true;
}
void loop (){
if (sensorready == true){
if (digitalRead(SensorInpin))
{
digitalWrite (LEDsop, HIGH);
digitalWrite (outpin, HIGH);
delay (7000); //modify to meet your required delay time
digitalWrite (LEDsop, LOW);
digitalWrite (outpin, LOW);
delay (250);
}
}
}
This will make the Arduino read the sensor's output and trigger an LED for seven seconds and turn off. Modify to meet your needs. It takes 30 seconds to calibrate the sensor.
Use this link and download open the editor and program.
https://www.rapidshare.com/#!download|833tl3|3411132297|Sketch.pde|1|R~2964C5D605113ECA37CDDB4ED2C02EFB|0|0
If you have trouble...here it is!
//be sure the project box is insulated or false motion triggers will happen
int calibrationTime = 30;
int Time = 0;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockinHigh = false;
int LEDsop = 13; // pin 13 includes resistor sop- signal out pin
int outpin = 12; // for any other device that doesn't require resistor
int SensorInpin = 2; // can vary, must be digital from 1-12
boolean sensorready = false;
void setup (){
pinMode (LEDsop, OUTPUT);
digitalWrite (LEDsop, LOW);
pinMode (outpin, OUTPUT);
digitalWrite (outpin, LOW);
pinMode (SensorInpin, INPUT);
pinMode (0, INPUT);
pinMode (1, INPUT);
pinMode (3, INPUT);
pinMode (4, INPUT);
pinMode (5, INPUT);
pinMode (6, INPUT);
pinMode (7, INPUT);
pinMode (8, INPUT);
pinMode (9, INPUT);
pinMode (10, INPUT);
pinMode (11, INPUT);
do {
digitalWrite (LEDsop, HIGH);
delay (500);
digitalWrite (LEDsop, LOW);
delay (500);
Time = Time + 1;
}
while (Time < calibrationTime);
digitalWrite (LEDsop, LOW); //if led turns off, sensor is ready
sensorready = true;
}
void loop (){
if (sensorready == true){
if (digitalRead(SensorInpin))
{
digitalWrite (LEDsop, HIGH);
digitalWrite (outpin, HIGH);
delay (7000); //modify to meet your required delay time
digitalWrite (LEDsop, LOW);
digitalWrite (outpin, LOW);
delay (250);
}
}
}
Step 3: Assemble.
Plug the sensor's power pin (red) to 5 volts, the ground (black) to ground and the signal (white) to digital pin 2.
Add the LED to Pin 13. Make a note of it as in the troubleshooting step I will discuss this.
Solder (or breadboard the optoisolator if you need any more than one LED to turn on.) This is connected to Pin 12.
Use the relay if necessary. This is also for Pin 12.
Make sure the sensor's jumper is towards the H position.
Add the LED to Pin 13. Make a note of it as in the troubleshooting step I will discuss this.
Solder (or breadboard the optoisolator if you need any more than one LED to turn on.) This is connected to Pin 12.
Use the relay if necessary. This is also for Pin 12.
Make sure the sensor's jumper is towards the H position.
Step 4: Use.
Have fun with this. It is really fun to see what you can do with the code.
I was going to use this to trigger intensely red LEDs for Halloween. (I'll save that for another time.)
Then I was going to have it trigger some creepy sound source such as this one:
https://www.instructables.com/id/Spooky-Sounds-With-555-Timer/
You can use it as a burglar alarm but then you would need to put a piece of paper around it to prevent false triggering...If you do use it as an alarm I think you would want to use this code from the Arduino website:
Link at: http://www.arduino.cc/playground/Code/PIRsense
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
I was going to use this to trigger intensely red LEDs for Halloween. (I'll save that for another time.)
Then I was going to have it trigger some creepy sound source such as this one:
https://www.instructables.com/id/Spooky-Sounds-With-555-Timer/
You can use it as a burglar alarm but then you would need to put a piece of paper around it to prevent false triggering...If you do use it as an alarm I think you would want to use this code from the Arduino website:
Link at: http://www.arduino.cc/playground/Code/PIRsense
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
Step 5: Troubleshoot.
If it doesn't work then here are some reasons why. (I had trouble with these.) (I will try to be as helpful as possible.)
It doesn't start up and do the motion sensing when I power it up.
Chances are you didn't realize there is a 30 second calibration time.
The light remains on with an occasional off time.
If it does so try pushing the reset button. Check for shorts between connections the Arduino and check for water. water+Arduino=disaster. If all else fails switch the short to L and the middle. Use overnight and switch back in the morning.
If any other problems are experienced, state the problem below in a comment and I will try to figure it out.
It doesn't start up and do the motion sensing when I power it up.
Chances are you didn't realize there is a 30 second calibration time.
The light remains on with an occasional off time.
If it does so try pushing the reset button. Check for shorts between connections the Arduino and check for water. water+Arduino=disaster. If all else fails switch the short to L and the middle. Use overnight and switch back in the morning.
If any other problems are experienced, state the problem below in a comment and I will try to figure it out.