Showing posts with label Relay. Show all posts
Showing posts with label Relay. Show all posts

Thursday, March 26, 2015

RFID Garage Door Opener with Arduino

This week I took a leap to add RFID access to my garage door. This was largely spurred by how flaky my manufacturer's wireless keypad is for my opener in the cold weather. I wanted something hard wired, and I knew RFID was fairly cheap to add to Arduino. And it's freaking awesome!

This setup isn't network aware, so you cannot monitor the status of the garage door or add/remove keyfobs remotely. You're getting short on digital pins on a standard UNO/Leonardo, so you'd probably want a Mega if that was something you wanted. I will probably add that in the future. I've never done WiFi with the Arduino, so this would be a good opportunity. I'll probably also look at adding a real time clock module so I can permit access only within certain windows (such as giving spare fobs/cards to family but only allowing entry during the day).

First you'll need an Arduino. I've used both UNOs and Leonardos with this build and they both work great. You'll also need an RC-522 reader, available on Amazon and eBay. This is easy to integrate with Arduino. Extra cards/fobs are also nice, and you'll want to get ones that work with "Mifare" in the 13.56Mhz band. These are also available for cheap in bulk from the 'Bay.

You'll also want to use some sort of relay or transistor as your output. In this case I used a regular Arduino-compatable relay module. I liked that it gives me an audible click for testing purposes, but a solid state relay or transistor would do just as well to trigger a garage door opener (which just momentarily touches two wires together). 

RC-522, with right angle pins (included) soldered on
I took my code and inspiration from miguelbalboa's GitHub. His code uses a single RGB led for feedback, which I found works the best. I tried paring this down to two leds, but you really want all three.

Hook the pins up as follows:

* 3.3V on Arduino to the 3.3V on the RFID reader.
* Pin 9 on Arduino to RST on RFID.
* Pin 10 on Arduino to SDA on RFID.
* Pin 11 on Arduino to MOSI on RFID.
* Pin 12 on Arduino to MISO on RFID.
* Pin 13 on Arduino to SCK on RFID.
* Red LED to Arduino Pin 7
* Green LED to Arduino Pin 6
* Blue LED to Arduino Pin 5
* Finally, your output relay on Arduino Pin 4
* There's an option for a reset button to be used on Pin 3. This reset button will wipe the memory of all cards, so you can start fresh. This is nice when you're first playing with things. 

The code from Miguel is annotated to allow for both common anode or common cathode RGB LEDs.

First time you boot it up, watch your serial console in the Arduino IDE to make sure you know what's going on. The first card you scan becomes the "master" card that puts the system into program mode (where you can add or remove cards).

Once you scan a master card, put the system into program mode. All three colors will flash in sequence. Now begin scanning additional cards. A successful "add" will be followed by two green flashes. If the card is already part of the system, then scanning it again removes it. Successful removal is indicated by two blue flashes. You'll see this written verbosely in the serial console. The LED feedback is needed once you install it in the field.

Cards are stored in the ATmega's EEPROM and so will survive a loss of power. This EEPROM has a limited number of writes (about 100k), but can be read unlimitedly. Realistically you could add and remove cards in normal usage for years before you hit the write limit.

I breadboarded mine, with an LED hooked up to the output to see how well it operated.


 Then it went into a case I had lying around.


In the right photo you can see the two relay module at the bottom. 

I used a Rugged Circuits Aussie Shield to connect everything up in the finished product. This was just easier than soldering a prototype shield, which would have been much cheaper. This is a great product to use, and in the future will be much easier to repurpose than a soldered up protoshield.

The wires coming into the case include the five control wires for the RFID reader and the four wires for the RGB feedback LEDs. There's also two wires that tie into the garage door opener's button circuit. I made the wiring harness myself, which is why the level of craftsmanship is so unbelievably high.

I have intentionally left the reset button off the physical build (but in the software). This means that to reset (erase all the memorized cards, including the master) the system, you need to manually jumper pin 3 to ground. To me, this works, since I don't want someone who is clicking buttons to accidentally reset everything.

You'll also notice that I used a separate box for the "brains" of the system, the Arduino and relay. My initial inclination was to shove this all into a single gang box with only a power wire and the garage door triggers coming in/out. My wife insisted that it be "hack proof", so I put the smarts (and the garage hook ups) on the inside. If you take off the wall plate outside, in order to hack into the system you'd still need to simulate the RFID board (protocol and the correct key codes).

I hotglued the RFID board directly to a plastic faceplate. This doesn't seem to interfere too much with the signal, but you do have only a couple of millimeters range to begin with. You basically need to touch the fob to the plate to get a read. This is a little more awkward with the credit card style than it is with a keyfob. 


Here's the finished outdoor portion, with my wiring harness fed through a hole into the garage. The box (initially surface mounted) has been recessed into the garage trim.

The blue is intentionally dim. I put an extra resistor on that part of the circuit when I put the recessed box together so that it wasn't a bright blue beacon for the neighborhood. The green and red are of "normal" brightness to give a clear indication of a read. 

Overall this was a simple build, since it doesn't involve network access or a RTC. However, since it doesn't use those you get a rather "dumb" appliance. This can be good, since it seems like the more functionality you add, the more points of failure you encounter.  I'm also only using one output. I've got an extra relay that might find some usage in the future.

Friday, September 5, 2014

Control Stop Light with an ATtiny85 or Arduino

One of my first projects with Arduino was to provide a little automation to an old stop light (and crosswalk signal) I had out in the garage. I'd already wired them to individual switches (good for games of "red light, green light"), but the allure of having it operate light a real light (green, then yellow briefly, then red, repeat) was too great. Arduino seemed like the perfect fit.

Of course, the Arduino can't switch anything high voltage nor high amperage. So you'll need to use a relay. I am a big fan of the cheap relay boards like these. For a regular stop light, you'll need three individuals or a four in one. I used a four in one relay board and a two in one separate board for the crosswalk lights. I stuffed it all in a plastic enclosure box. Don't undertake the high voltage part of this project without a firm understanding of the risks involved. This post isn't about high voltage relays or how to integrate them with the Arduino, so I'll assume you know how to do that. Follow along if you want to build a stop light simulator with 5mm LEDs, the same principles apply.

ATtiny 85
I recently wanted to find a way to experiment with using an ATtiny (baby brother of the ATmega microcontroller in the Arduino). I picked up some ATtiny85 and ATtiny84 chips off an eBay vendor. The ATiny can be programmed, with a new library, in the Arduino IDE, are much cheaper than an ATmega ($1.3/ea for the ATtiny85 vs $2.30 for the ATmega328p, and $5+ if you don't want it shipped from China), but are limited in the number of input/output options. The ATtiny runs slower (internal clock, the ATmega uses an external), uses less energy, and comes in a bare chip form factor (need some soldering skills to get beyond the breadboard phase). Here's a good comparison of the two chips.

Since the ATtiny chips run off 5v I planned on using a commodity USB power supply. Since the ATtiny85 has an internal clock chip, it's easier to build a circuit (no external crystal).  My original stop light controller was powered by an Arduino Micro (about $20). So I wanted this new controller to be smaller and cheaper. My total cost to replace the Micro was about $3.

Since I used only 5 outputs and no inputs for my light controller, the ATtiny85 is about the smallest and cheapest of the Atmel chips that will do the job.

First, to program the chip. I used a programmer shield, but you can use some hookup wire and do it yourself or build your own shield. Follow these directions if you want to take that route.

Program it!
Here's the code:

"


int Walk = 3;
int DontWalk = 4;
int Green = 0;
int Yellow = 1;
int Red = 2;

void setup()
{
 

  pinMode(Walk, OUTPUT);
  pinMode(DontWalk, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Red, OUTPUT);
}


void loop()
{

  //HIGH = ON, LOW = OFF
 
  digitalWrite(Walk, LOW);
  digitalWrite(DontWalk, LOW);
  digitalWrite(Green, LOW);
  digitalWrite(Yellow, LOW);
  digitalWrite(Red, LOW);
 
  digitalWrite(Green, HIGH);
  digitalWrite(Walk, HIGH);
  delay(800);
  digitalWrite(Walk, LOW);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(200);            
 
  digitalWrite(Green, LOW); 
  digitalWrite(Yellow, HIGH);
 
  delay(350);
 
  digitalWrite(Yellow, LOW);   
  digitalWrite(Red, HIGH);  
 
  delay(1200);             

}
"


Now to breadboard it. Follow this pinout guide to wire it up. Here I used LEDs to take the place of the relay outputs to see if it looked like a stop light and to test my timings. Since my relays are triggered by a "high" signal, when the LED is on, the relay would be triggered to send the high voltage to the light.


It works! Now to solder.

Top of the board
Bottom, my lackluster soldering skills...
Now back out to the garage to plug it in to the relays and the lights.


Here is the final, ready to be buttoned up project.

This photo shows the dramatic size reduction you can realize switching from the already small Arduino Micro to a basic ATtiny85 on some perfboard.

Provided you don't need a bunch of inputs/outputs the ATtiny might be a cost, and perhaps more importantly size, effective solution.

Here's the Fritzing diagram:


Thursday, August 21, 2014

Integrating a Generic Relay Module with Insteon



SainSmart 4-Channel Relay Module

Relay modules such as the one pictured above, available in 1, 2, 4, 8, and 16 versions, are readily available from a number of sellers. Click the picture above to be taken to Amazon for the SainSmart version.

All of these modules are similar. They work on a 5v system, both requiring 5v to operate and switching when they receive 5v or ground (depends on the model, some can be switched by a jumper), and switch up to DC30v or AC250v at 10A.  They are an excellent addition to any budding Arduino enthusiast's toolbox. I used relays like this in my Arduino-powered stoplight, which uses an Arduino to give the logic to a standard stoplight, which is powered by 120v mains.


My home automation system of choice is Insteon, a powerline communication system.

This summer, I installed recessed can lighting in my living room. Six lights in total, but I wanted to be able to turn the one directly above my TV off, without having to resort to such archaic means as unscrewing the lightbulb.

I initially tried a Micro On/Off module hidden in the can light I wanted to remotely control. If I hadn't wired all of the lights together in series, adding this to the scenes for the living room would probably have given me what I want. The problem is, that though once the living room can lights were turned on  the micro module received power and would work to switch that can at will, it didn't boot up instantly. So it wasn't a seamless solution. When you'd turn on the cans, all of them would come up, then the micro module would boot up, and only then could you turn that TV can on and off.

This solution would also cause Insteon network communication issues, since anything with that micro module in the scene would continue sending requests and eventually give failed communication notices. This isn't good networking.

My lack of foresight required some clever electrical engineering. I remembered some of these relay modules, more than able to handle switching a single 120v led can light, but how to integrate it into my Insteon network?

I took a rather simple approach, avoiding the inclination to make a wifi-enabled, Arduino-powered, relay switching, network-enabled do-it-all box. I wanted to use an older I/O Linc Insteon module to provide the power and then activate and deactivate the 120v relay to the TV light.

Though the I/O Linc has ports for 5v, GND (ground), S (sense), N/C (normally closed), N/O (normally open), and COM (common). The latter three are used for the output of the I/O Linc, as it has a built in relaying function. The in-built relay function will only switch low voltage, for example, I have one hooked to my garage door opener to trigger the door to open or close as needed. I/O Lincs respond to any Insteon input, so if I could get the I/O Linc to switch one of my relay modules, with those relays switching the high voltage side, I'd be in business.

One word of note, the output of the I/O Linc can function in either momentary or latching mode. In momentary mode the I/O linc will open the N/C contact and close the N/O contact (sending it to common) for a period of time before switching it back. This is how it functions as part of my garage door operation, it shorts the N/O and COM for a second, simulating the physical press of the garage door button on the wall.  Latching mode makes the relay output work as a switch, going back and forth between the N/C and N/O being sent to COM. Think of latching mode working like a light switch, it's either on or off until you flick the switch again, nothing is timed.

On the bench, the 5v output of the I/O Linc will power the relay modules, no problem. So I ran the 5v and GND from the I/O Linc to the relay module, jumped the GND to the COM, then used the output from the I/O Linc (set to latching mode) to send ground (from the COM) to my output of choice (in latching mode it doesn't matter which, the N/C or N/O is used, as it just moves back and forth).

After crawling into the attic, the relay functioned just as expected. My remote (either a Remotelinc or a Keypadlinc) can send a command to the I/O Linc to cycle the high voltage relay on and off. This worked beautifully for all of about 10 minutes. Back up into the attic I go, rewire everything, thinking the attic was just too hot for the I/O Linc or that the I/O Linc failed. Replace everything, bring the Remotelinc up with me to check, and get it working again. Ten minutes later, it quits working.

I figure out, from all these failures, that either using the 5v from the I/O Linc constantly or (more likely) shorting the ground from the I/O Linc to COM was causing the I/O Linc to overheat and become inoperable. I fried one of them doing this, so DO NOT SEND AN I/O LINC'S GND OR 5V TO COM or else you'll crap the bed.

I hooked an old cellphone power brick into the mix, to both power the relay board and to become the new source for my ground (or 5v into COM if your relays switch with a high input). What you end up with is a relay that is controlled by the I/O Linc, can switch a 120v line signal, and the relay board is powered by any 5v power adapter which can be plugged into the pass through mains plug on the I/O Linc.

***DO NOT UNDERTAKE SWITCHING HIGH VOLTAGE IF YOU DO NOT KNOW AND UNDERSTAND WHAT YOU ARE DOING. YOU CAN KILL YOURSELF. HIGH VOLTAGE IS DANGEROUS.****

You'll also, if making a permanent installation, need an understanding of the rules, laws, codes, etc. for high voltage wiring.

Here's how to wire the relay up. Remember to switch the I/O Linc into latching mode. Since Fritzing doesn't have either of these devices, I resorted to wiring up another on the bench.

I use a mini breadboard to be the backbone of the system, but it's not needed. Once you get it working, you can solder the wires together. The line on the left, with the black at the top, is the ground/negative lead from a USB 5v power supply. Yellow and orange leads are tied to ground.

The line on the right, the red wires, are 5v power.


Yellow, tied into the COM port on the I/O Linc is the ground from the external power supply.

Orange is the N/C port on the I/O Linc here, but if you're in latching mode, it really doesn't matter. Tying it into N/C means that it'll default to "on", so in the event of some failure, the relay will be triggered to close (i.e. on).


On the relay board side, the brown wire from the breadboard brings ground to the GND pin (far left) and the red lead brings 5v from the breadboard (far right). The orange wire comes from the I/O Linc and is the low side trigger for the IN1 (relay on the left, where my finger is). Though I have a yellow wire in IN2, ignore it, that's not the same as the other yellow wires, it was just a spare wire on the test leads.

If you've got it all wired up, toggling the I/O Linc (either by sending commands from a linked remote or by tapping the black button on the side) should toggle the relay (you'll hear a click and the led on the relay board will turn on).

You can now use the relay's high voltage side to switch a load (switch it on the "hot" wire, typically black, not on the neutral, typically white). There's a little diagram on the relay's output side to tell you which terminal is normally open and which is normally closed. Use a multimeter to help make sure your circuit is wired correctly and operating how you want before you put the more dangerous high voltage hookups in.

The biggest advantage of switching my TV can light this way is that the relay doesn't care what the voltage or current is, it just switches back and forth. So I can toggle that one can light on and off while the upstream circuit is powered by a dimmer switch. For example, I can dim the lights to 50% and then toggle the TV can off and on. It truly operates seamlessly now, essentially like a bulb I can remotely unscrew when I don't want the TV washed out with light from above.

Search This Blog