Quick and Easy Arduino Nightlight

About: I love building things and taking pictures.

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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Get the Required Materials.

1 of everything except where noted.

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.


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.


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.

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:

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.

//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;

void setup(){
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.println(" done");
Serial.println("SENSOR ACTIVE");

void loop(){

if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.print("motion detected at ");
Serial.println(" sec");
takeLowTime = true;

if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

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");

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.

The Mad Science Fair

Participated in the
The Mad Science Fair

Pocket-Sized Contest

Participated in the
Pocket-Sized Contest

Make It Glow Challenge

Participated in the
Make It Glow Challenge

4th Epilog Challenge

Participated in the
4th Epilog Challenge

Halloween Decorations Challenge

Participated in the
Halloween Decorations Challenge

Hack It! Challenge

Participated in the
Hack It! Challenge

1 Person Made This Project!


  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

3 Discussions


4 months ago

Here is my version, has a ultrasonic sensor to turn the light on, and a timer to turn the LED off after a set time. Also, uses joule thief type circuit so that on single AA battery needed.

Of course!

It can be but since I was entering this in a Arduino competition I used the Arduino. I could have used a transistor and possibly a Darlington transistor. Maybe I will make another one with a transistor...