Monday, March 2, 2015

Using Arduino to open and close gate at home

I've had the annoying experience of rushing from home to work, and only to have the security guard in my housing area to call me and tell me that I've left my auto gate open. My suspicion is that while I have already pressed the button on my remote control to close the gate, the gate's wheel could have gotten stuck due to a pebble in the tracks which causes the closing to stop, and the gate re-opening again. Given that my office is at least 45km away from home, going back all the way to just close it is not an option for me. I normally just hope no one walks into my home and steal my shoes that I leave outside my porch.

I already have IP cameras installed at various locations in my home. One of them is pointed at the front gate, which allows me to see whether if the gate is open or close. This is useful for times when I'm driving halfway, and then having the thought of wondering whether I have closed the gate or not.
But seeing the gate open 40km away is no help as well. So I decided to blow off the dust from my knock-off Arduino Duemilanove and put it to some good use.

My gate is controlled via a 315MHz (I think) RF keychain remote control. I know there are options where I could just buy a multifunctional RF Transmitter, but I wanted to go on the cheap, and also, I needed to solve this problem fast. These remotes are relatively cheap, and I keep a spare just in case I lose the one I normally carry. You can get these remotes clone for about RM50 - RM100, depending on the style and quality of the remote.

My remote runs on 12v battery. Getting this to work with the Arduino (as I have learned painfully) requires the use of other components (as Arduino tends to run 5V). Also, the button on the remote as I've found out is a pull-up, meaning, the voltage across the button is constantly on 12V, and when you depress the button, it goes to 0V and that's when the RF signal is sent out. I've tried using the digitalWrite and analogWrite function to see if I can short the button via 2 pins on the Arduino, but to no avail.

After figuring which of the 4 pins of the button is required, I soldered the 2 wires from the 2 pins. In case you don't know, this is the bottom side of the remote. The button is on the other side.
Good thing I found couple of reference on the internet of people who were trying to do the same thing. I will put those links later when I find them again. The proper way to do this is to use an NPN transistor, in which case I used the 2N2222 923 for this. There are examples of people using a 12V relay, but given that the remote i'm using runs on a very low current, the relay is pretty much an overkill.

Before I move on, I must give the disclaimer that I have pretty much zero background in electronics. What I mean is that while I'm interested in electronics, my theory of how transistors works is pretty much useless.

Here's the remote (the case removed) sitting on an Arduino breadboard shield. This makes it easier to wire everything up.

OK, moving on. The 2N2222 923 has 3 legs. The middle one is known as the base, and that's the one you will want to connect to the arduino digital pin (i used pin 2 for this). The other 2 legs of the transistor is connected to 2 pins of the remote button. Polarity matters in this case. I can't tell for sure which goes to which, however, I noticed that if I have the polarity reversed, the pins will short and the my remote button (which has a led indicator) will light up. So all I need to do is reversed it. Then that leaves the ground cable which you will connect from the arduino ground pin (look for GND), and you need to connect that to pin 1 of the transistor (if the flat side of the transistor is facing you, then pin 1 is the one on the far left).



Here's the Arduino sketch which I use:

void setup()
{
  digitalWrite(2, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(2, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

void loop()
{
}


I know this is extremely lazy, as I have to upload the sketch everytime I want to open or close the gate. But this is just a conceptual test. Eventually, I guess you could hook this up to a webservice and have the gate control via an Android or iOS app.

Lastly, if case you're wondering how I'm going to control this remotely, I have VNC installed on a desktop sitting in my home, and the Arduino, along with the hooked up remote control connected to it. All I need to do is just to remote home, and upload the sketch to open or close the gate.

Nifty eh? Given all this done in less than 2 hours.