Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

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.


Monday, October 18, 2010

Date and Time on Arduino LCD Shield

Here's a project you can try if you have an Arduino Duemilanove and a compatible LCD Shield. Both boards I'm using is an Arduino knock-off and they're from DFRobot, a Chinese retailer specializing in sale of electronic and robotic components. You can find more of their products here: http://www.dfrobot.com/. If you're in Malaysia, you can try out their distributor at http://myduino.com/. They carry most of DFRobot's inventory, and also some of the original Arduino boards.

For this hack, I used the following:
1. A DFRduino Duemilanove (Arduino Compatible) 328 board


















2. DFRobot LCD Shield for Aduino
















To program the codes, I use the latest Arduino Software, ver 0021, which you can download here: http://arduino.cc/en/Main/Software. This software allows you to write the script as well as compile the codes necessary for the Arduino to run.

Here are the codes I used:

#include <LiquidCrystal.h>
#include <DateTime.h>
#include <DateTimeStrings.h>

#define dt_SHORT_DAY_STRINGS
#define dt_SHORT_MONTH_STRINGS

// simple sketch to display a digital clock on an LCD
// see the LiquidCrystal documentation for more info on this

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int backLight = 13; // pin 13 will control the backlight

void setup(){
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.

DateTime.sync(DateTime.makeTime(0, 44, 13, 16, 10, 2010)); // sec, min, hour, date, month, year // Replace this with the most current time
}

void loop(){
if(DateTime.available()) {
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;

DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
}

void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits,DEC);
}



void digitalClockDisplay(){
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(3,0);

//lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
if(DateTime.Day <10)
lcd.print('0');
lcd.print(DateTime.Day,DEC);
lcd.print("/");

//lcd.print(DateTimeStrings.monthStr(DateTime.Month));
if(DateTime.Month <10)
lcd.print('0');
lcd.print(DateTime.Month,DEC);
lcd.print("/");
lcd.print((DateTime.Year,DEC)+2000);

//lcd.print(" ");
if(DateTime.Hour <10)
lcd.setCursor(5,1);
lcd.setCursor(4,1);

// digital clock display of current time
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
}

You will need two additional libraries for it to work. They are the DateTime.h and DateTimeStrings.h. You can download them here: http://www.arduino.cc/playground/uploads/Code/DateTime.zip. Just copy both the folders to the 'libraries' folder in your Arduino directory.

Once you have all that done, stack the LCD shield on the Arduino board (make sure the pins line up), plug in the USB cables to the board as well as to your PC, and hit Compile on the Arduino software. The codes will be uploaded and you shall be able to see the result on your LCD screen!

If all goes well, you should be able to see the following:




















Note that the keypad on the LCD shield is not functional as I did not include any codes for them to respond to. That itself may be the next hack for me to try!

Bugs/Problems/Feedback/Comments? List them down in the comments and I'll try my best to help you out!