For my plant growth project I want to control a fluorescent bulb (among other things) with an Arduino. Fluorescents can’t exactly be powered by the measly 5V available on an Arduino so instead we’ll be using a relay to control mains power. I’ve structured this post as a tutorial so that hopefully it might help anyone learning to use relays with an Arduino.
Safety Warning: This is dangerous, mains power isn’t to be trifled with. If you don’t feel comfortable playing around with mains wires then don’t try it! There are kits such as the Powerswitch Tail Kit that will do this job for you. You can get the 120VAC model prebuilt, but it looks like you have to put the 240VAC model together yourself.Parts list:
- 3m Extension cord (must be the 3 prong variety)
- 1K Resistor
- 2N2222 Transistor
- 1N4004 Diode
- Appropriate mechanical 5V relay (250VAC rated for Australians)
- A lamp to test with
- An Arduino to control it with
Assuming you already have an Arduino and other bits and pieces lying around this is a pretty cheap project. The most expensive part is the relay which will only set you make $10 or so.
I started out with the intention of using a Solid State Relay. Apart from being quieter and quicker, I really liked the design of the solid state relay pictured above. Being able to hide the live wire under a cover was important. This didn’t work out though because solid state relays and fluroescent bulbs don’t mix. Even when the relay is in an open state the light flickers every couple of seconds. I heard of a few ways to get around this, but I opted just to use a mechanical relay instead. I couldn’t find one of the same design as that solid state, but I did fine one with big terminals on top that I could use for the live wires.
First step is to cut the extension cord in half. Using this page I know that since I’m in Australia blue is nuetral, green is ground and brown is the live wire. You can confirm this too with a continuity test between the connector and the exposed wires as well (good extension cords will label the blades L/N/E). The live wire is the one I need to splice into my relay so really I could have not cut the blue or green wires and saved my self the trouble of soldering them back to together.Connect the live wire to the armature terminals on your relay. I’ve put insulated female disconnect terminals on the live wire so I won’t accidentally electrocute myself when working near the relay. Notice I removed the armature contacts on the bottom of the relay too. Again this is so I don’t accidentally electrocute myself.
Now for the Arduino! Using this diagram you can very easily create a circuit to control a relay using a single digital pin. Mechanical relays uses an electromagnet to pull closed an internal switch. The transistor in the linked circuit will respond to the state of the digital pin and open/close the relay appropriately.I’ve set my breadboard up exactly like the relay diagram linked above, so you can copy either if you need a reference. Wire up your breadboard and plug in your extension cord once everything else is done.
Once you’re all wired up you’ll need to upload a sketch to the Arduino. Remember, all we need to do is write a HIGH or LOW to the third digital pin so this is probably one of the simplest sketchs you’ll ever use. Example:
#define RELAY_PIN 3
At this point I got distracted and decided to play around with the delays to time the light with a song. My attempt was pretty bad. Trying to time the upload of a sketch with the start of a song isn’t ideal. It does give me an idea for another project though…
Well that concludes this tutorial. Among a few other things, it has become apparent that using relays with an Arduino is very simple. This is great since it opens up a world of possibilities. Being able to control any mains powered device with one pin on a microcontroller? The applications of this simple circuit are literally endless. Personally I’ll be using it very soon to turn my herb light on and off.