All products featured on WIRED are independently selected by our editors. However, we may receive compensation from retailers and/or from purchases of products through these links.
Last week I explained how to use a motion sensor to trigger an event with an Arduino. The event we triggered was a simple blinking of an LED. You can do a lot with a bunch of LEDs if you use your imagination, but if you want to trigger a different type of device — say a strobe light or a motor — the Arduino doesn’t have the power to do that. At the most an Arduino output pin can supply 40 milliamps. That’s enough to drive an LED or a pager’s vibrating motor, but anything substantial will require some help.
In order to drive larger loads with the Arduino there are two things to consider: voltage and current. How much voltage does your load need to run? Let’s say we are using a DC motor to pull a rat across the path as a trick-or-treater approaches. A small DC motor will need about 5 volts and a few hundred milliamps for this application. In order to provide the power to the motor you need a separate power source and a way to switch that power source on and off.
One of the popular ways to drive larger loads is to use transistors. Transistors are essentially little electronic switches; instead of using metal contacts they use silicone direct the flow of electrons. This is a typical transistor driver circuit for a DC lamp:
TO-92 Image Courtesy of Radio Shack
On the left is a typical transistor package. Transistors like this are good for loads up to 500 milliamps. There are high power transistors that can switch larger currents. The tradeoff here is that they also get pretty hot and require some form of heat sink or fan — without that, the transistor will either get damaged or will act pretty funky. (While building my final design project in college I ran into a heat problem with my circuit. I realized the problem was heat-related and to confirm it I put a cold can of soda on the power transistor. The circuit worked fine and the next day I bought a small fan to save trips to the soda machine for cold soda.)
Crydom Solid State Relay
Transistors are good for low-power DC voltages, but once you either start approaching the 1 amp current requirements or you need to control an AC voltage you should consider using a solid state relay. Solid state relays are capable of switching higher currents and are capable of switching AC or DC voltages. They require no extra components to be driven by the Arduino. The Crydom HDC series of solid state relays can be operated with an input voltage of 4-32 VDC and a current of 25 ma. They are also capable of switching up to 160 Amps! That makes for one mean looking Halloween Decoration.
Solid state relays go up in price the more current you want to push through them. High power models for consumer applications can cost over $30.00. A cheaper alternative would be electro-mechanical relays. These are basically a pair of contacts controlled by an electromagnet.
An Electromechanical Relay
When power is applied to the electromagnet the contacts close when the power is removed a spring pulls the contacts apart. These are tried and true methods of switching electricity that go back to the late 1800’s. As a matter of fact the first computer bug was a moth that got caught in a relay of the Mark II Aiken Relay Calculator.
Electromechanical relays will require a transistor driver circuit in order to be driven by the Arduino. The typical circuit for driving a relay is the same as the transitor driver above with the noted exception of the diode.
Image Courtesy of http://pcbheaven.com/
The diode is called a flyback or snubber diode and protects the transistor from damage that can occur when the relay coil is de-energized. It is a good idea to include one of these diodes whenever you are switching a relay or motor with a transistor.
Now for the code to use the methods above.
void setup() { pinMode(2,output); }
void loop() { digitalWrite(2,HIGH); pause(1000); digitalWrite(2,LOW); pause(1000); }
That’s all there is to it. Make an output pin and write a high to it to turn it on and write a low to turn it off. Remember to pause for a reasonable amount of time. I can’t tell you how many times I’ve forgotten the pause statement and thought my circuit was bad when all that was really wrong is I didn’t leave it on long enough.
That’s a lot to chew on for now. In an upcoming project we will use the transistor driver circuit and some Ardunio code to create eerie lighting effects. Until then here are some resources to dig a little deeper into some of the subjects we covered here:
Transistor Driver Circuits at pcbheaven.com Wikipedia Page on Relays Radio Shack