Introduction: DIY Motion Sensing Ceiling Fan/light ***5$ Energy Saver!!!***

Now when I walk into my room (or my cat :) my lights automatically pop on for me! In this project I'm going to show you how to for under $5 hook up a motion sensor to you ceiling fixture! This is what happens. When the motion sensor senses motion it sends a HIGH signal to microcontroller. The microcontroller then turns on the light and starts a 5 minute countdown. Everytime the motion sensor senses motion it resets the 5 minute timer. So say I walk in my room at 5:30 and immediately walk out. At 5:35 the light will turn off. Later in the day my cat walks in at 6:00, then walks out. Three minutes later, she sees a mouse in my room, so she runs back in at 6:03, but realizing that the mouse if just a play mouse, runs back out. At 6:03 The timer is reset and the light won't go off for another 5 minutes (6:08).
The second mode of this project just keeps the light on as long as the circuit has power. So if you sleeping at night and want the fan to stay on all night you can switch it to that.
Just think, next time you leave the house and forget to turn the light off you can have the satisfaction that the light will automatically turn off in 5 minutes! This whole project can be built for about 5 dollars and takes quite a bit of time but is very worth it in the end! Watch the video, make the project, and vote and share!
Of course you can change how long the light stays on if you want. Change it to 10 minutes if you'd like! Just remember you have to convert however minutes you want the light on into milliseconds!




Step 1: Ingredients

Link to codehttp://www.mediafire.com/view/xgnd2ba71hqur5d/DIYMotionsensor.ino


I would explain it but I really don't know how to so!
Thanks to lewisb42 for helping me out with the resetting timer.
(https://www.instructables.com/community/Arduino-programming-help-Do-two-things-at-once/)

Electronics:



• Arduino
• ATtiny85
• ATtiny programming shield or 10 uf capacitor
• 8 DIP socket
• PIR motion sensor
• Relay- the kind that comes on a pcb with a protection diode and transistor
• SPDT switch (with an off position)
• IC circuit perf board
• 1/8 or 1/4 inch male and female audio jacks (mono is easier)
• Breadboard and jumper wires
• 10k ohm resistor
• USB cable
• Telephone cable wire
• Electrical wire
• Electrical tape
• Brush on electrical tape
• Cell phone charger



Parts and tools:



• Multimeter
• Project box
• Dremel
• Philips screwdriver
• Hot glue gun
• Heat shrink
• Soldering Iron
• Heat gun
• Wire cutter & strippers

Step 2: Breadboard the Circuit

As always, it's best to test the circuit before you make it permanent.
I attached the schematic to this step so you can see how to make this circuit. 

Step 3: Circuit P.1

  1. Start by inserting to the perf board the 8 DIP socket. Turning the board over bend the leads over on the socket and solder them to the pads. 
  2. Cut and strip a 2 inch red and black wire. Solder the red wire to the right side of the circuit and solder the black wire to the left side of the circuit. Make sure you use the correct left and right-from the topside of the board. Also make sure the front of the socket is facing the red and black wires. 
  3. Solder the other side of the red wire coming from the perf board to the middle lead of a audio jack. 
  4. Solder the other side of the black wire coming from the per board to the end lead on the far end of the audio jack. 

Step 4: Circuit P.2

Your circuit so far should have a female audio jack hooked up to two solder pads on the perf board, then an 8 DIP socket soldered down to the middle. Lets add the switch. 

The kind of switch we will be using is an SPDT switch with an off position. Basically it directs current through one of two wires or neither of them. So the switch three positions. 

1. OFF           2. ON (wire 1)      3. ON (wire 2)

        |                          \                           /
    |       |                 |       |                     |       |
    |       |                 |       |                     |       |


  1. Solder the middle lead of the switch to the +5v power pad on the perfboard.
  2. Solder the right wire of the switch to pin 8 on the ATtiny85.
  3. Solder the left wire of the switch to pin 2 on the ATtiny85.

Step 5: Circuit P.3 (The Last Part!!!)

There should be three wires coming from the relay and motion sensor. On is +5v, the other is GND, and the third is control. It may be labeled (CT, C or Vout).

The Motion Sensor

  1. Solder the GND and the +5v of the sensor to GND and +5v pads you made on the perf board. 
  2. Solder Vout to pin 7 on the ATtiny85.
The Relay
  1. Solder the GND and the +5v of the sensor to GND and +5v pads you made on the perf board.
  2. Solder CT to pin 6 of the ATtiny85

Step 6: Program the ATtiny85

  1. Find and install the ATtiny85 library
  2. Open a new window and copy and paste this code into your Arduino sketch
  3. In a new sketch window, open and upload the ArduinoISP (it's under example sketches)
  4. Upload the ArduinoISP to your Arduino
  5. Press the ATtiny85 into your breadboard. Connect a 10uf capacitor between pins GND (+ lead on cap) and RESET (- lead on cap) on the Arduino.Connect the following pins on the ATtiny85 to the Arduino.

    Arduino        ATtiny85

       GND--------- pin 4
       5v ------------ pin 8
       Pin 13 ------ pin 7 
       Pin 12 ------ pin 6
       Pin 11 ------ pin 5
       Pin 10 ------ pin 1
     
  6. Select the ATtiny85 as the board (Tools>board>ATtiny85)
    Change the programmer to "Arduino as ISP" (Tools>Programmer>Arduino as ISP)
  7. Go to your motion sensor sketch and click upload. You should get to errors (avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85) but that's OK. 
Now you can stick the ATtiny85 into it's socket and make sure the circuit is working properly. 
If you can't get the ATtiny85 to program get more info here.

Step 7: Making the Power Cable

It is time to make the power cable. For this step you will be needing a phone cable, USB cable, and an male audio cable.  What we are trying to do is make a long cable to carry 5v to our project. I figured that the easiest way to do that was to use a cell phone charger. 

  1. Measure about 5 inches down from the USB plug and cut the cable. Strip the wire. Cut away the white and green wires. AGAIN get out your wires strippers and strip the red and black wires. 
  2. Strip both ends of a telephone cable. 
  3. On one end of the telephone cable connect the red and green wires to the red and black wires of the USB cable. 
  4. On the other end of the telephone cable connect the red and green wires of the phone cable to the red and black wires of the audio jack. 
Now when you plug in the USB cable you should be getting 5v at the Audio jack. 
Plug the male mono jack into the female audio jack and make sure the circuit is getting power.

Step 8: Trimming

The perf board won't fit in the project box so it will take a little cutting with the dremel to get it to fit. Take a sharpie and mark off a section. Happy cutting!

If your relay won't fit in the box you may want to trim it down too.

Step 9: Fitting the Fresnel Lens, Switch, and Audio Jack

  1. Remove the Fresnel lens from the motion sensor if possible. 
  2. Unscrew the light switch cover.
  3. Use a sharpie to draw out a square that the Fresnel lens can fit into.
  4. Cut! Be careful, the plastic melts easily.
  5. Drill out the holes that the screws go through. The idea is to make a hole big enough for the female audio jack to go through and big enough for the switch to go through
  6. Glue down the audio jack and the motion sensor. Insert the switch through the hole and screw it down.

Step 10: Protecting the Relay

Now I got some brush-on electrical tape and I brushed it over all the contacts on the back of the relay. This. would help eliminate any risk of shorting out the 120v AC wires.

Step 11: Readying the Wall

  1. Now it's out to your basement or garage. Find the circuit breaker that is controls your light switch and turn it off. Double check to make sure the power is off.
  2. Unscrew the light switch from the box and pull it out.
  3. Cut the wires going to the switch.
  4. Strip the ends of the wires that were going to the light switch. Keep it short.
  5. Wrap electrical tape around the ends of the two wires.
     
  6. Stick a wire through the left hole of the project box and then connect it to the middle terminal on the relay [C].
  7. Stick the other wire through the right hole of the project box and then connect it to the right terminal [NC] (normally closed).

Step 12: Fitting the Project to the Wall

Now it's time to sew everything back up. First press the project box into the wall and then try to get the outlet cover to fit flat on the wall without any pressure. I would up just cutting the project box in half. I wasn't counting on all the wires they jammed in the wall box. Wrapping the circuit and relay in electrical tape might have been better. Anyhoo you do what works best for you.
Squeeze hot glue around the edges of the light switch cover and press it onto the wall. Make sure to wipe away any excess glue.

Step 13: Creating the Sticker

I didn't like the way the outlet cover looked because of my horrible cutting job, so I made a label. I'm just going to upload some of the pictures of the steps I took to make the sticker.

  1. Design your label. Be creative. I put a QR code on my that take your smart phone to my profile.
  2. Cut the label out.
  3. Press out holes for the audio jack and the switch.
  4. Gently press the label on the outlet cover.
  5. Guestimate where you need to cut a hole for the Fresnel lense and cut out the hole.
  6. Press the label on the wall.

Step 14: Plug Up & Enjoy!

Plug your cellphone charger into the wall. Then plug the USB end of the power cable into the phone charger and then plug the male audio jack into the female jack on the cover. Tape or tack the wire out of the way so that nobody will trip on it.

Now impress your friends by having them walk into your room before you do so that they can experience the awesomeness of your project!

Depending on the person you may want to change the time the light stays on on sensor mode. All you have to do is reprogram the ATtiny85. Where you see 300000 milliseconds in the code change it to whatever you want.

Step 15: Conclusion

This was my first attempt at making a good video for my instructable. I hope I did a good job. Any tips on making the video better?

Thank you for watching the video and viewing this instructable. If you like this i'ble please give it votes for the contests and don't forget to subscribe for more!


~EE

Comments

author
Bits4Bots (author)2014-11-08

Najmin: All Instructables are now available in PDF format for easy printing, sharing and offline viewing.

This feature is only available with a Pro membership.

Become a Pro member today!

author
najmin (author)2014-07-09

can somebody help me how to download the pdf ?

author
HavocRC (author)najmin2014-07-10

int relay = 1;
int pir = 2;
int b1 = 3;

int onState;
int pirstate;

unsigned long lastHigh = 0;


void setup()
{
pinMode(relay, OUTPUT);
pinMode(pir, INPUT);
pinMode(b1, INPUT);
delay(1000);
}

void loop()
{

pirstate = digitalRead(pir);
onState = digitalRead(b1);

if(millis() - lastHigh > 300000) // Change this number to however minutes you want the light off
{
digitalWrite(relay, LOW);
}

if(pirstate == HIGH)
{
digitalWrite(relay, HIGH);
lastHigh = millis();
}

if (onState == HIGH)
{
digitalWrite(relay, HIGH);
}


}

author
sriramsrikp (author)2014-05-24

Hai, could you tell me that which line of the coding is reseting the timer for next 5 mins?? You mentioned that suppose a person is walk through the sensor means the light is glowing for 5 mins. Again if the person cross the sensor in the next 3 mins means the timer will reset to next 5 mins. I am not able to find that part in the coding... Pls help...

author
HavocRC (author)sriramsrikp2014-05-24

If you open the sketch in media fire look at line 25. . .

if(millis() - lastHigh > 300000)

300000 is 5 minutes in milliseconds. 1000 milliseconds = 1 second.

Do the math!

author
Leon_dupl (author)2014-05-06

code for free year membership: "EE's_free_gift"

author
HavocRC (author)Leon_dupl2014-05-06

Hey man I'm sorry I forgot to take those down, two people already got the year memberships like a long time ago. But to be nice I have two more 3 month subscribtions. If you aren't already pro I will PM you the codes tomorrow. (Its almost 2 AM here :). So you'll get 6 months-assuming you don't have a pro account.

Peace,

author
Leon_dupl (author)HavocRC2014-05-07

Thank you, code worked fine.

author
Leon_dupl (author)HavocRC2014-05-06

Thank you, will come in handy as I am design student at Massey Uni, Wellington NZ. Doing project on light sensor for bedroom, attempting also to dimm light by 50% when sensor triggered. May need help later. Thank you. Sorry I woke you up.

author
HavocRC (author)Leon_dupl2014-05-07

Haha no worries! Sounds cool! Yeah, just PM me anytime and I'll see if you can help you out!

Check your inbox, I sent you the 2 codes. Enjoy!

author
phlophlorin90124 (author)2013-11-16

Hi, how can I add another motion sensing ? I want to use it for lighting my staircase? Thanks!

author
HavocRC (author)phlophlorin901242013-11-16

If you want to hook up multiple sensors you can just hook all the outputs to the same input pin so if any of the sensors goes high then the light will turn on.

Does that help?

author
J-Salcedo (author)2013-10-14

Excuse me, I am a little confused when I treat to understand how the circuit and the Sketch works together. So, if you turn the switch in OFF mode, ok, the light turn off, because the attiny is not powered; when you put the switch in SENSOR mode, ok too, the attiny is powered and it can run the Sketch, if PIR detect someone the Light turn ON. The problem I see is when you put the Switch in ON mode, the Attiny is not powered when you do this, so how can the Attiny run the Sketch and control the RELAY?

Could Someone say to me if am I wrong or there is some error in this Instructable?

Thanks...

author
HavocRC (author)J-Salcedo2013-10-14

Hey! I can see your concern. The Attiny us powered through that pin as well as the Vcc Try it. Hook up a 5v wire to any pin on the Arduino and then the GND to the Arduino's GND and it will turn on! Trust me the Attiny 85 works, and I am using the circuit right now!

author
J-Salcedo (author)HavocRC2013-10-14

OK...I supposed it while I was writing but I didn't search information about that.
Is Good Know this, it will be helpfull for me in futures proyect...
Casually I am working in a proyect that is very similar, but has another features. I hope I will make an instructable with that idea, for this reazon I put your Instructable in my favorite List.

Thanks. A very ingenious idea to use an Attiny like this.

author
HavocRC (author)J-Salcedo2013-10-15

Thanks! This project is just a start. Add distance sensors, sound sensors, IR communications, :D

author
Irwan2 (author)2013-07-20

Is it 4?

author
HavocRC (author)Irwan22013-07-22

Yep but Dolla already got it!

author
DollaBillionare (author)2013-07-14

I see 4 lights on step 5 pic. Hook me up!

author
HavocRC (author)DollaBillionare2013-07-15

Nicely done check your inbox!

About This Instructable

25,416views

94favorites

License:

Bio: 19 year old hobbyiest and future EE.
More by HavocRC:DIY FPV Antenna Protector CaseControl Your RC Plane With Your Phone's AcclerometerDIY Receiver Controlled Switch (Cheap and Easy)
Add instructable to: