Intro: Home Automation - How to Add Relays to Arduino
How to select Relays – Its all about Power.
When you are selecting a relay to connect to your Arduino you need to consider
a) the power (voltage and current) of what you want to switch on and off and
b) the power (voltage and current) required to drive the relay (i.e. to energise the relay coil)
We will look at a number of configurations for connecting relay modules to Arduino boards:-
1) Direct driven reed relays
2) Single 5V power relays powered from the Arduino board's 5V supply
3) Four (4) relay shields using a separate supply. The shield powers the Arduino board.
4) Separate multi-relay 5V modules high triggered and low triggered
5) Separate multi-relay 12V modules high triggered and low triggered
6) Modifications for Driving a 5V relay module from a 3.3V Arduino board
7) High/Low Triggered Relays for Use with 3.3V boards, e.g. ESP8266-01
8) FET Sheilds
9) Solid State Relays
Finally there small section on Help My Relay Stopped Working (RC Snubbers)
If you are looking for a simple way to switch these relays on and off from your Android mobile, via bluetooth, wifi or SMS, with optional 128 bit security, check out pfodDesigner (www.pfod.com.au) and this instructable Code generator for custom Android/Arduino menus to switch Arduino outputs on and off Absolutely NO programming required. No Android programming and no Arduino coding. The pfodDesigner generates all the Arduino code you need for your custom menus and the pfodApp takes care of displaying the menus on your mobile and handling your selections.
Step 1: Relay Selection Based on What You Want to Switch on and Off.
This is relative straight forward so we will consider it first. While there are may different types of relays (see http://en.wikipedia.org/wiki/Relay), for home automation and control there are four main types that would be used.
i) Reed relay for switching low voltage and low current devices
ii) FET switches for switching 30V to 60V DC voltages and up to 20Amps
iii) 110V and 240V power relay for switching mains powered devices
iv) Solid State switches for switch mains powered devices.
Reed relays are small low power relays suitable for switching <100V at current <0.5Amps. While designs vary, total switched power is usually less than 10 VA (volts x Amps < 10). Examples of IC form factor reed relays are TRR-1A relays and COTO TECHNOLOGY – 8L01-05-001
Most relay modules sold to connect to Arduino are power relays and are rated for either 110V or 240V. The ratings are usually printed on the relay case.
Notice that the maximum DC voltage that can handled is much less then the AC rating.
This is the type of relay you will need to use of switch mains powered devices. These relays will handle most devices used in homes except the highest powered ones like room heaters, stoves, motors. Make sure the VA (Volts x Amps) of the device you are switching on/off is less than the relay rating.
Any wiring to the mains power should only be done by a qualified electrician.
A final point, the power relays commonly use silver alloy contacts and are not suitable for switching very low currents like switching Arduino digital inputs. If you are only switching a few milliamps, for example by putting a relay across a low voltage garage door push-button, you should use a reed relay (or a relay with gold contacts) as the power relays need a “wetting current” to break through the surface film resistance and will not switch very small currents reliably.
“Normally, a switch's wetting current rating is far below its maximum current rating, and well below its normal operating current load in a properly designed system. However, there are applications where a mechanical switch contact may be required to routinely handle currents below normal wetting current limits (for instance, if a mechanical selector switch needs to open or close a digital logic or analog electronic circuit where the current value is extremely small). ... Normal silver or copper alloy contacts will not provide reliable operation if used in such low-current service!” (http://www3.eng.cam.ac.uk/DesignOffice/mdp/electric_web/Digital/DIGI_4.html )
FET switches do not have this problem and can be used to switch very low currents.
Step 2: Relay Module Selection Based Power Required to Drive the Relay
The Arduino digital outputs can only supply +5V (or +3.3V) and less than 40mA max. (typically <20mA for continuous use). So, except for some reed relays, you cannot drive the relay coil directly from the digital output since 5V power relays typically require ~100mA to operate the relay coil. This means for most (all) relay modules you need to provide a separate supply to the module in addition to the Arduino digital output which is used to control the relay switching.
We will look at a number of configurations for connecting relay modules to Arduino boards:-
a) Direct driven reed relays
b) Single 5V power relays powered from the Arduino board's 5V supply
c) Four (4) relay shields using a separate supply. The shield powers the Arduino board.
d) Separate multi-relay 5V modules high triggered and low triggered
e) Separate multi-relay 12V modules high triggered and low triggered
f) Modifications for Driving a 5V relay module from a 3.3V Arduino board
g) Solid State Relays
Direct Driven Reed Relays
5V Reed Relays can be driven directly from the Arduino digital output provided their coil current is less than 20mA. TRR-1A relays and COTO TECHNOLOGY – 8L01-05-001 only require 10mA each so you can easily drive 8 or more of these relays from an Arduino board supplied by a USB 500mA power supply plugged into the USB connector (The Arduino Uno has 500mA resetable fuse on the USB input).
Alternatively if the Arduino board is powered via 12V to its power plug, limit the number of 10mA reed relays to less than 6 as the on board 12V to 5V regulator limits the maximum current that can be drawn from the 5V supply to about 70mA or so.
Above is a circuit for a single 5V reed relay being driven directly from D7
Direct Drive Clamping Circuit
Two things to note:
a) The 1N4004 diode across the coil of the relay and
b) the 33 ohm resistor between D7 and the relay coil +
The diode is there to clamp the voltage/current spike the relay coil produces when it is switched off. Do a search on “flyback diode” for more details on this. (http://en.wikipedia.org/wiki/Flyback_diode for example)
The 33 ohm resistor protects the D7 output from excessive current and allows the diode to do the clamping. When a digital output is turned off it does not go open circuit, rather it connect the output pin to GND via a very low resistance. Without the additional 33 ohm resistance, the current spike from the coil would preferentially go back into the microprocessor via D7 to GND.
The coil resistance is about 500 ohms so when D7 is high (+5V) most of the voltage appears across the coil (I.e ~4.7V ) (search for Ohm's Law or checkout Sparkfun's tutorial) The “pull in” voltage on the datasheet for the TRR-1A 5V reed relay is 3.75V which means any voltage higher that than must close the contacts so 4.7V is more then enough to ensure the relay operates.
All commercial relay modules should already have the clamping diode fitted across the relay coil.
Step 3: 5V Power Relay Modules Powered From the Arduino Board's 5V Supply
There are a number of different single 5V relay modules available on-line. But most do not have any circuit diagrams available. Here are two examples (the first one from DX.COM's website).
Both operate the relay when the input (IN) is HIGH (+5V). The second board as an optical isolator and separate connectors for the IN ground (COM) and the main 5V supply ground (GND). The jumper is connecting these two grounds together so you can use it just like the left hand board.
Removing the jumper lets you have isolated supplies, one for the the Arduino and another completely separate one for the relays. Separate supplies provides some extra protection against mains volts getting back into the microprocessor. But this extra protection is mostly illusory. The pcb tracks on the back of the board are quiet close and nothing will protect you against a lightning strike on the power line outside your house.
So for either board, connect the relay GND to one of Arduino's GND pins and the relay 5V or Arduino's 5V pin and the relay IN to one of Arduino digital outputs, say D4. Then when the digital output, D4, is High the relay will operate.
The photo above shows this wiring. Note the Arduino is being powered by the 5V USB power. The relay is 5V so everything is at the same voltage.
The relay's contacts consist of a Normally Closed (NC) and Normally Open (NO) connection and a COM connection. When the relay is un-powered the NC terminal is connected to the COM terminal and the NO terminal is not connected. When the relay is operated, the COM terminal switches over and is now connected to the NO terminal and NC terminal is not connected. The screw terminals are either marked NO, NC and COM or small drawing is shown like the image above. It the image above, the top terminal is the NO one the centre terminal is the COM and the bottom terminal is the NC one.
Connecting these relay modules to the Arduino's 5V pin means both the trigger current (IN) and the current to drive the relay coil are being supplied by the Arduino board. From the datasheet for the SRD-05VDC-SL-C you can read that the relay coil takes ~72mA to operate. Added to this is the current required by the IN input to trigger the coil. For the optically isolated relay that is about 4mA and perhaps a bit more for the non-isolated one. (Measure it for your module).
In any case each of these relay modules will take <100mA from the Arduino's 5V supply. So if a USB supply is used to power the Arduino,you can attach up to 4 of these modules. Indeed that is just what the 4 channel relay shield from Seeed Studio does.
In Seeed Studio's 4 channel relay board, the relays are controlled by D4,D5,D6 and D7 so you can just plug this relay shield on top the Bluetooh Shield V2 to get a complete remote control of up to 4 relays controlled by Android and pfodApp. Use pfodDesigner to design the Android menu and generate all the code you need.
This shield is quoted as drawing 250mA, but 300mA would be more realistic based on the datasheet for the relays. In any case it is fine if you are powering the Arduino board via a USB supply. However it is not suitable if you are using a 9V or 12V supply due to the limitations of Arduino's on-board regulator.
Step 4: Four Channel Relay Shields Using a Separate Supply
An example of this type of relay shield is the DFROBOT Relay Shield for Arduino V2.1
This shield can be configured in a number of ways using the jumper strips. When operating all 4 relays it should be powered by 9V applied to the screw terminal on the left of the picture.
By default the relays are controlled by D2, D7, D8 and D10. The Itead Bluetooth Shield that I usually use already uses D2 as an output so only 3 relays can be driven in the default configureation, if you use that Bluetooth Shield.
However the DFROBOT relay shield can be reconfigured to use any digitial pins to drive the relasy. It also includes an Xbee socket so you can plug in an RN-42XV Bluetooth or RN-XV Wifi module and configure the jumpers to work with it. Note: in this case you should cut off the reset pin from the relay shield as it is driven by the RS-232 flow control on the Xbee socket.
Step 5: Separate Multi-relay 5V and 12V Modules, High Triggered and Low Triggered
Separate multi-relay 5V and 12V modules, high triggered and low triggered
If you want more the 4 relays then there are a number of multi-relay modules available. Typically with 8 relays but some with 16 relays. SainSmart has an 8 channel module.
The specification for the SainSmart say each channel needs 15mA to 20mA to drive it. I expect this is an error as all the modules I have measured need <5mA to drive them. However I suggest you measure the actual current needed to drive the inputs of any 8 channel board you use. The Arduino microprocessor has an absolute maximum total digital output current of 200mA, so 8 times 20mA = 160mA is higher then I would like to run continuously.
Since both the Arduino drive voltage and the relay coil voltage is the same, +5V, there is no problem with low triggered inputs since the voltage from the relay board being pulled down by the digital output will not be more the 5V. But as will discussed below this is not the case for 12V relay modules. It is also not the case if you are driving a 5V relay module from a 3.3V Arduino board like the Due or Fio or SparkFun's FioV3.
Separate multi-relay 12V modules, high triggered and low triggered
It is sometimes more convenient to use 12V relays driven by a 5V Ardunio board. You may have a 12V battery supply or need to use 12V for some other part of the circuit, or the lower current used by the 12V relay coils may be a consideration. In these cases you have to be careful about how the 12V relay coil is driven by the 5V Arduino output.
As mentioned above a lot of the relay modules available do not come with circuit diagrams. Problems can occur if the relay module is low triggered. That is the relay control input is pulled to GND by the Arduino digital output pin. In that case, then when the Arduino output pin is high there may be 12V applied to it from the relay module. This may damage the Arduino microprocessor.
This is also an issue if you are driving 5V relay module from a 3.3V Arduino board like the Due or Fio or SparkFun's FioV3. Again the +5V from the relay board applied to the 3.3V digital output pin may damage the microprocessor.
There are two ways to avoid this problem.
i) Use a module that is high triggered by +5V. In this case there will not be any excess voltage coming back from the module when the Arduino output is low.
ii) Use an optically isolated module that has a separate trigger supply terminal in addition to the 12V supply terminals for the relay coils. In this case the Arduino +5V supply goes to the trigger supply terminal and no excess voltage will be applied to the digital outputs.
Above is an indicative circuit of one channel of 8 channel relay module (This circuit from SainSmart)
As you can see with an optical isolator and a separate Vcc input (+5V from Arduino) and the Arduino digital inputs are isolated from the 12V relay voltage by the optical isolator.
Also shown is a typical optical isolated 12V 8 channel relay board. Notice the Gnd and VCC near the IN terminals. That VCC is for the 5V supply from Arduino. At the right hand end is the JD-VCC pin which is for the 12V for the relay coils.
The photo above shows an example connection for 12V optically isolated relay. Note that the Arduino is being powered by 12V via the plug in power. The Vin socket on the Arduino is the same voltage as the power plug, i.e. 12V, so the relay module JD-Vcc is connect to Arduino Vin to supply the 12V for the relay coils. The Arduino Vcc is 5V and is connected to the relay module Vcc to set the trigger voltage. The trigger GND is connected to Arduino GND and D4 is connected to IN1 to drive relay 1. In this module the JD-Vcc and the Vcc connections both have the same GND point so only one ground connection is needed and no jumper is needed.
Step 6: Modifications for Driving a 5V Relay Module From a 3.3V Arduino Board
One final point about drive 5V relay modules from 3.3V Arduino boards. In addition to using one of the above two solutions to prevent the 5V being feed back to your 3.3V digital outputs, you may need to modify relay board, if the 3.3V output does not reliably drive the relay. The problem is the circuit was designed for 5V to drive the optical isolator U1, and U1 may not be getting sufficient current from your 3.3V Arduino's output.
There are two common modifications for 3.3V operation of a 5V relay board:-
1) If the drive circuit has a led in series with it, i.e. IN1 in the circuit above, just short it out by soldering a wire from one side to the other. The led was using up about 2V so shorting it out means now you only need about 3V instead of 5V to drive the relay.
2) If there is not led in series with the drive pin, then add another resistor across R1 to reduce the series resistance and so increase the current available from you 3.3V drive. R1 is typically 10K (102) so adding another 10K resistor across the existing R1 should be sufficient. This will require some careful soldering.
Step 7: High/Low Triggered Relays for Use With 3.3V Boards (e.g. ESP8266)
I recently went looking for a single relay board to use with an ESP8266 module. The ESP8266 has 3.3V supply rails. The module I found did not have a separate IN+/-, instead it featured a High/Low Trigger selection jumper (see the above photo). Unfortunately there is no schematic provide for the very inexpensive module, so you need to test it to see if it is suitable for use with a 3.3V output from your microprocessor board.
Testing consists of selecting the High Trigger jumper, powering up the module with 5V and then measuring the voltage on the IN terminal, with nothing connected to that terminal. In my case the IN open circuit voltage, when in the High Trigger setting, was a few millivolts. So this in will not harm the 3.3V microprocessor board. Next apply 3.3V to the IN input and check that the relay operates. In my case the relay did operate with a 3.3V input and only draw 1mA. If your relay board does not operate you may have to find a series resistor in the input circuit and reduce its value as described above.
I actually did this testing using an UNO board. I used the UNO 5V and GND pins to power the relay module and then measure the open circuit IN voltage. Then I used the 3.3V pin on the UNO to test driving the relay with this voltage.
Step 8: FET Shields and Solid State Relays
An alternative to relays for switching low to moderate DC voltages is FETs. There are a number of boards available:-
- LEDSEE 8 channel fet shield
- SparkFun Power Driver Shield (6 channel – 3 x 5V switches and 3 x 12V switches)
- Yourduno 4 channel optical isolated (Note: the optical isolator limits the maximum voltage this board can switch to <30V)
- SparkFun Mini FET Shield (8 channels)
All these can be driven by a 5V Arduino output and can control voltages up to 30V to 60V at 0.6A to 20A.
The actual voltage and current that can be controlled depends on the the FET used on the particular shield. You need to check the FET specifications. Look for the MIN Drain-to-Source Breakdown Voltage to see how much voltage the board can switch. The current carrying capacity depends of the how well the FET can get rid of the heat. Check the boards published specifications.
The LEDSEE 8 channel FET shield (with jumper JP1 removed) and the Yourduno 4 channel optical isolated board should also be able to be driven by 3.3V Arduino outputs, but I have not tested this.
Solid State Relays
Solid State relays are inexpensive and can handle high current loads. The one shown above will switch a load of up to 40Amps from a 24 to 380V AC supply. The control input is DC 3 to 32 Volts. However there are a number of things to look out for when using Solid State Relays with Arduino.
a) Make sure the relay is controlled by DC volts 3 volts and up. Some solid state relays have AC control inputs of higher voltages instead on DC control inputs.
b) Choose a relay that is rated for the current you need to switch. The one in the photo is a 40Amp relay
c) Make sure the relay can handle more then 1.414 times the AC line voltage. That is >340VAC for a 240V supply and >180VAC for a 120V supply.
d) Always attach the relay to a suitable heat sink using a thin smear of thermal compound that completely covers the base of the relay. Solid State relays can get very hot and burn out if the heat sink is too small. For a 10amp load, the relay needs to get rid of about 16Watts of heat.
e) Solid State relays are best suited for resistive loads like heating elements. If you are trying to turn a motor on and off a normal relay and a snubber is probably a better choice. This article has some information on Solid State Relays Common Precautions. While this posting discusses snubbers.
The main advantage of Solid State relays is that if it still working after 3 months (infantile failure) it will probably work for many years.
The solid state relay can be controlled with 3V, so is suitable for the 3.3V arduino boards. However it takes about 12mA to switch the solid state relay, so do check that your board can supply that amount of current from a digital output. Some 3.3V boards, like LinkIt ONE, will only supply 3mA from a digital output. Arduino Uno boards can supply about 20mA.
Step 9: Help My Relay Stopped Working (RC Snubbers)
You have tested your relay on the bench with a multi-meter, but after you install it, it stops working. That is it won't turn OFF. This happens most commonly if you are switching inductive loads, like motors, ceiling fans, etc, but can also happen when switching incandescent lights and is caused either by the contacts welding close due to high initial current OR arcing on opening and continuing to pass current.
Assuming you can normally hear the relay operate (a small click), then if you can not hear it click when it should be opening then the contacts may be welded together, you will need to replace the relay in this case. If you can hear it click when it opens but the motor keep running then the contact are probably arcing.
The solution to these problems is to add a snubber network. This pdf has some examples and suggested sizings. This discussion also has information on choosing snubbers. RC snubbers are also available pre-built in a variety of sizes, e.g at element14.
There are a number of types of relays you can add to your Arduino to switch real things on and off. You need to choose a relay appropriate to your needs.
The simplest solution for on/off Home Automation is to use the free pfodDesigner to design your control for upto 4 switches and then use a Arduino Uno + Bluetooth module + 4 channel relay board powered by a USB power supply and controlled by pfodApp and, if switching mains power, have an electrician wire it in.