Introduction: Motion Triggered Hallway Lamp

In this project we are building a simple motion triggered hallway lamp. It's super convenient to not need to worry about finding the light switch when coming home at night or getting up to get some water during the night. The sensor will detect your presence and illuminate the hallway for 2 minutes.

Step 1: The Parts

  • You will need:
  • Arduino (due to the simplicity of this project I would say any will do, or like in my other instructables an atmega standalone circuit will also do)
  • Resistor
  • HC-SR501 Motion Sensor
  • LED (optional)
  • A lamp, or anything you want to switch on
  • Relay board
  • ULN2x03A
  • Breadboard / Perfboard
  • Maybe a photoresistor if you don't want the lights to turn on during daytime

The tools:

  • Cable stripper
  • Knife
  • If you are not going to build the project on a breadboard: soldering iron
  • Pliers

Step 2: The Circuit

As we need multiple connections to 5V and GND, connect the 5V and GND Pins of your arduino to the rails of a breadboard.

Connect D1 and D2 to the inputs of your ULN2x03A.

Connect the according outputs of your ULN2x03A to IN1 and IN2 of your relay board. Connect VCC and GND of the relay board.

I used a ULN2x03A because you shouldn't switch inductive loads like relays directly with the arduino, as the current draw most likely exceeds the safe rating and may damage your chip.

Connect Pin 9 and 10 of the ULN2x03A to GND and 5V.

Connect an LED with a suiteable resistor (I used 450 Ohm) to GND and Pin 13 of the arduino.

Connect the GND and 5V pins of the motion sensor to your power rails, and connect the output pin to Pin 3 of the arduino.

Disconnect everything from power before you start cutting / connecting any cables. You are working with mains voltage. Mistakes can lead to serious injury or death. If not done correctly, you may also start a fire. I do not take responsibility for any damages done to you, others or your property. I'm not an electrician, so maybe the way I did is not correct either. If you do not know what you are doing or feel uncomfortable, leave the project be or consult someone who knows. Proceed at your own risk.

Cut the power cable of your lamp, and connect the cables from the power plug to the COM ports of your relay. Connect the cables from the lamp to the NO (normally open) terminals.

I live in europe and devices such as lamps with no earth lug usually are euro plugs, and do not have polarity protection. So in my opinion it is not enough to just switch one cable, as you may be just switching neutral and the lamp is still connected with the phase cable. With switching both cables I make sure the lamp is really disconnected from power when off. 2 channel relays are not expensive, you should do the same.

Step 3: The Code

I tried to comment all important steps. I included the watchdog timer because in my setup with the bare atmega chip, the system would hang occasionally every few days and I came home to a dark hallway. So far this seems to have fixed the problem.

Due to the bad lighting conditions in my hallway, even during day time, I didn't bother to include something to distinguish between day and night time, as my photoresistor wasn't sensitive enough.

If you can, get a cheap photoresistor like this one. You can use the trim poti to calibrate it. Add something like

if (value == HIGH && valuephoto == LOW) // motion detected and the photoresistor outputs that the hallway is dark

to the code.

int relayp1 = 1; // Relay Pin 1
int relayp2 = 2; // Relay Pin 2 int IRmotionPin = 3; // Input from Motion Sensor int LED = 13; // LED

int value; // value (HIGH or LOW) of the motion sensor

#include // include watchdog timer

void setup() {

// set all the pin modes pinMode(relayp1, OUTPUT); pinMode(relayp2, OUTPUT); pinMode(LED, OUTPUT); pinMode(IRmotionPin, INPUT);

// set all the pins to initial conditions digitalWrite(LED, HIGH); digitalWrite(relayp1, LOW); digitalWrite(relayp2, LOW); delay(2000); // LED should be on for 2 sec so I can see that the board is powered up, important to me as I used the bare chip, otherwise I wouldn't have any indication if the system is booting digitalWrite(LED, LOW); wdt_enable(WDTO_8S); // If the system hangs for more than 8s, the system should reboot }

void loop() { value = digitalRead(IRmotionPin); // get the state of the sensor

if (value == HIGH) // motion detected { digitalWrite(relayp1, HIGH); // turn on the lamp digitalWrite(relayp2, HIGH); // turn on the lamp

for(int i=0; i <=120; i++){ // lamp should stay on for 2 minutes delay(1000); wdt_reset(); // reset the watchdog timer }

digitalWrite(relayp1, LOW); digitalWrite(relayp2, LOW); } else { wdt_reset(); // if the reading is LOW, do nothing, BUT reset the watchdog timer }

}

Step 4: Putting It Together

I drilled a small holein the back of my cabinet in my hallway, to put the system and power outlet out of plain view. I used some double sided adhesive tape to fix the motion sensor.