Hey! If you want to get rid of the boring light switches and make your room lights automatic for cheap, you're in the right place! This project is going to be very easy to build. Don't go by it's simplicity, its going to be really cool and 100% working. And yes, it's made using arduino, so get ready and lets get making.
Relay module (the number of channels is up to you, depending on the number of lights you want to automate)
BC547 (or any general purpose) transistor
2x IR obstacle sensors (NOT PIR SENSORS)
A box to stuff all the electronics in
Tape and scissors
Glue gun (optional)
You'll also require a laptop/desktop to program your arduino.
Step 1: How It Works
Let's talk about the logic.
Basically, the room lights should turn on when someone enters and turn off when he/she exits. That's not all. In case person 1 enters and then person 2 enters. In that case the lights shouldn't turn off when any one of them exits. They need to turn off only when both of them exits. So our device should be able to count the number of people entering the room and number of people exiting. Seems complicated? Yes it is if you think of building a dedicated circuit for the purpose. But we have a lifesaver. Drum roll please. Introducing ARDUINO! Ok probably you know about it.
We just need to write the code for the purpose and our project is 90% done! We will talk about the code later. First let's talk about how we can give the information of our entering or exiting to the Arduino.
What we are going to build is called a bidirectional visitor counter. If you search online, you can find a lot of such projects. But most of them use a 8051 microcontroller. And we need a dedicated circuit to program it. Why do so much when you can buy an Arduino nano for about $5 and program it?
So here's what we're gonna do. We will have two obstacle sensors attached to the side of our door. One just outside the room (let's call it sensor1) and one just inside (sensor2). When a person enters the room, sensor 1 detects him first and when he exits, sensor 2 detects him first. We can use this logic to tell the Arduino if a person is entering or exiting the room. No worries, the code isn't complicated.
Step 2: The Code!
You can find the code in the end of the instructable. Before you scroll down to download it, let me inform you that my code is to control only a single light i.e. a single relay. In case you want to modify it to control more, you're free to do so as long as you're familiar with the Arduino ide.
As usual, there are two ways to code your Arduino. One (the simplest) is to copy the code here, paste it in the Arduino ide and upload it. The other is to understand the working and code by yourself. I am leaving the choice to you, but I shall do a code walkthrough in the end.
Make sure your Arduino isn't connected to anything else other than your PC when you're uploading the sketch (code). Once uploading is complete, we can go ahead and test it.
Step 3: Test 1
Just connect the IR sensors to the respective pins of the Arduino (in this case sensor which is just outside the room to pin 14 and the sensor just inside to pin 15). Again, refer the code to make sure you've connected the sensors to the right pins. I used an LED to test the output instead of a relay. So, I connected the LED's positive pin to pin 2 of the Arduino (based on the code) and the negative pin to GND. Now move your hand from sensor 1 to sensor 2 to simulate someone entering the room.
You should see the LED turn on. Move your hand from sensor 2 to sensor 1 and the LED should turn off. Great right. No. Unfortunately it didn't work for me. Again! My first trial always fails!
I double checked my code and found a small mistake. A delay was needed after the hand(person) passes through each sensor. Or else the other sensor would soon detect the person and turn the LED off. So I made the necessary changes in the code and tried again. The device worked as expected. Don't worry, the code I attached in this instructable is the updated one. So it shouldn't go wrong for you too. Unless there are any connection problems in the circuit. Cool let's make the relay circuit!
Step 4: Who's Drivin' the Relay?
In case you're using an arduino relay module, you can skip this step. Because such modules already come with built in relay driver circuit.
First of all you may ask, why a separate circuit for relay? The output of an Arduino doesn't have enough power to drive a relay. So, we need a separate supply for the relay. We will be using the 5v output of the Arduino. So obviously, our relay should be rated 5v dc and an output of 250v AC 10A. Just connecting the relay to the 5v Arduino supply won't work. We need to still trigger the relay from our programmed output (in this case, pin 2 of Arduino).
So we will be using a general purpose transistor for this. You can connect the circuit as per the diagram. Basically, the base of the transistor receives the trigger and completes the circuit between the relay and 5v to activate it and in turn activate the bulb connected to it.
Step 5: Hook Up the Device!
Now that everything is ready and working, we need to connect the relay in between the live and bulb of the household wiring. WARNING! You're going to deal with 220v AC and this is not a small thing. Please don't try to make any changes to the household wiring by yourself (as long as you're not a trained electrical engineer). I shall not be responsible if anything happens to you during this (including if you get shocked and turn into a superhero like flash, etc :-p)
Just kidding, don't try to become a superhero by getting shocked by AC mains. Those logic are bullshit.
I would suggest using a high power rechargeable led lamp instead of messing with an AC bulb. However I never messed with AC wiring of my house. I used a separate bulb holder, connected a pair of copper wires, soldered the relay in between and hooked the wires to the wall socket (making sure the relay is connected in series with the bulb holder through the live wire, NOT THE NEUTRAL). I made a small cardboard box to put the relay in.Then I fixed a 9 W LED bulb to the bulb holder and powered up everything. The device worked flawlessly! Cool!
Step 6: Final Touches...and We're Done!
Finally, I fixed the sensors outside and inside the room beside the door and hanged the bulb holder on the ceiling. Now when I go into the room the bulb turns on and when I come out, it goes off. I tried with many people entering the room and everything worked just fine.
Although there are two problems that I faced. When two people enter the room simultaneously, side by side, the sensor registers them as a single entry. Obviously because the sensor detects only one obstacle. The other problem is, the sensor was a bit weak. It couldn't detect if a person is moving too far away from it. I can fix the second problem by getting a better IR sensor module but the first one would require more sensors and programming. But it is a very rarely occuring problem and you need not worry if you have a small door. Overall, it looks great for the money spent for the components.
I couldn't get any images of the final product because I have removed everything for another project. Sorry for that. It was pretty cool but I was more excited for the next project.
Step 7: Code Walk Through
As usual, before you hit the back button, please vote my instructable for the contest. Thank you.
The code begins by initializing a variable count to store the number of people entering/exiting the room. We declare 14 and 15 as the input pins and 2 as the output to the relay. In the loop function, lies the heart of the code. Each time pin 14 reads high, count is increased by 1 and each time pin 15 reads high, count is decreased by 1. I've discussed the need for the delay in step 3. When count is zero, the relay pin, i.e. pin 2 is set low (off). We have added an extra statement count=0 to set count to zero in case it goes negative due to some reason.
As long as count is not zero, the relay (pin 2) is in high state (on).
Hope you understand. Thank you, and see you in the next one!
Participated in the