Make your room lights automatic for cheap!




Hello! Do you want to step into automation? Want to have your room lights automated? Are you afraid of lengthy coding and spending too much money? No worries! Here I present to you the visitor counter! A very simple to build and code automatic room light controller! And no, it’s not at all expensive. It should cost you around 450 rupees or $6.5 USD. Don’t believe me? Follow along to find out…

Supplies



Arduino Uno/nano
Relay module (the number of channels is up to you, depending on the number of lights you want to automate)
Jumper wires
BC547 (or any general purpose) transistor
2x IR obstacle sensors (NOT PIR SENSORS)
A box to stuff all the electronics in
Soldering iron(optional)
Tape and scissors
Glue gun (optional)
You'll also require a laptop/desktop to program your arduino.









The heart of the project is the popular micro-controller Arduino UNO. So first, let’s understand the logic of it’s working.
We have two obstacle sensors attached to the door of the room which you want to automate. One sensor is placed just outside the room (let’s call it sensor 1) and the other just inside (let’s call it sensor 2).

Case 1: Person enters the room.
* He obviously moves from outside to inside
*Sensor 1 detects him first, and then sensor 2

Case 2: Person exits the room.
*He moves from inside to outside
*Sensor 2 detects him first and then sensor 1

Great! We have something here. We just need to code the arduino so that it can find out which sensor is detecting the person first. Based on this data, the arduino should be able to tell if a person is entering the room or leaving it.



Code...



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).



Here's the complete code:


int count=0;
void setup()
{
pinMode(14,INPUT);
pinMode (15,INPUT);
pinMode(2,OUTPUT);
}
void loop()
{
if(digitalRead(14))
{
count++;
delay(1000);
}
if(digitalRead(15))
{
count--;
delay(1000);
}
if(count<=0)
{
count=0;
digitalWrite(2,LOW);
}
else
digitalWrite(2,HIGH);
}


Pretty simple right? It's a perfect code for beginners to understand an arduino program because most of the code resembles a typical C/C++ program. 


Testing







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.

The Relay Driver!










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. Or if you're like me, and want to save some money and use a normal relay, read ahead... 





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.



Hook it up.




Now that everything is ready and working, we need to connect the relay in the live bulb wire 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 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 seems to work as expected. Cool!

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 occurring problem and you need not worry if you have a small door. Overall, it looks great for the money spent for the components. 




0 Comments