Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us
 

Forward Logo (image)      

Home Automation
How to Add Relays to Arduino

by Matthew Ford 4th Jan 2022 (originally posted 10th September 2014)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.




Update: 5th Jan 2022 – Added ESP-01 Relay Module
Update: 29
th Dec 2021 – marked up a number of product links as “no longer available”. Classic Bluetooth shields have been replaced by BLE shields/boards. BLE is more difficult to use but pfoddesignerV3 and pfodApp makes it easy. See Bluetooth Low Energy (BLE) made simple with pfodApp.

Another simple solution for on/off Home Automation is to use pfodDesignerV3 to design your control for upto 4 switches and then use a Arduino Uno + Cheap/Simple Wifi Shield + DFROBOT Relay Shield for Arduino V2.1 powered by a USB power supply and controlled by pfodApp and, if switching mains power, have an electrician wire it in.


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)

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.


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) High/Low Triggered Relays for Use with 3.3V boards, e.g. ESP8266-01
h) ESP-01 Relay Module
i) FET Shields
ij Solid State Relays

Help My Relay Stopped Working (RC Snubbers)
ESP8266 + 4 Relays + USB = Failure

Reed Relays

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




Power Relays

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 (i.e. <10mA), 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.

Relay Module selection based power required to drive the relay.

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

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.

Here 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.

Single 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 images (the left hand one is from DX.COM's website).

Both operate the relay when the input (IN) is HIGH (+5V). The right hand one 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.


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 below. It the image below, 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 this 4 channel relay shield from Seeed Studio (no longer available) does.


The relays are controlled by D4,D5,D6 and D7 so plugging this relay shield on top the Bluetooh Shield (no longer available) shown in
Design Custom menus to switch Arduino outputs on and off will give will complete remote control system to switch things on and off controlled by Android using pfodApp. Using the free pfodDesignerV2, you can easily add the controls for all 4 relays.

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.

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 (no longer available) used in Design Custom menus to switch Arduino outputs on and off uses D2 as an output so be default only 3 relays can be driven if you use that Bluetooth Shield. However the DFROBOT relay shield can be configured to use any Arduino digital output to drive the relays.

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.

Separate multi-relay 5V 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.

  1. 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.

  2. 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. Here 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. Here is a typical optical isolated 12V 8 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.




Here is an example connection picture. 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.

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.

NOTE: Before doing any modifications, test if you can drive the 5V relay board from your 3.3V Arduino. In my case it just worked (see below)

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 1K (102) so adding another 1K resistor across the existing R1 should be sufficient. This will require some careful soldering.

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.

ESP-01 Relay Module

Another alternative is the compact ESP-01 Relay Module. The photo above shows the module with and ESP-01 inserted. Check when you purchase whether or not the ESP-01 is included. This module will work with either ESP-01 or ESP-01S. For an example of programming and using this module see ESP-01 Timer Switch

This module as a number of limitiations/problems:-

Although the relay is rated for 240V / 10A, the board layout has minimal track spacing and the screw terminals are too small to accept heavy duty wire. For comparison the stand alone single relay modules above have larger screw terminals and physical notches cut in the circuit board to separate the 240V from the low voltage coil pins. So the recommended use is for switching voltages less than 30V.

The relay will flick on/off when the module is powered up. Depending on your application this may or may not be a problem. See ESP-01 Timer Switch for how to add a capacitor to the relay module to prevent this relay flicker on power up.

The relay has a 5V coil, but V4 of the Relay Module drive the coil from the 3V3 supply. (V1 used the 5V input instead). The schematic for the Relay Module V4 is here. Operation of a 5V relay from 3V3 is marginal. The two examples tested both operated, but if you find the relay does not switch on reliably the ESP-01 Timer Switch has a simple modification to convert the board to use the 5V input to drive the relay.


FET Shields

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 (no longer available)
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, retired product)

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 of 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 solid 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 (local copy here). While this posting discusses snubbers (local copy here).

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.

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 (local copy here) also has information on choosing snubbers. RC snubbers are also available pre-built in a variety of sizes, e.g at element14.

ESP8266 + 4 Relays + USB = Failure

In this case you are using an ESP8266 base board and 4 or more optically isolated relays. You have separate VCC and JD-VCC connections. The JD-VCC is the 5V supply for the relays and the VCC is the opto interface supply, the ESP8266 3.3V supply. You have plugged your board into your computer's USB port to program it but you find that while using only 2, and possibly 3, relay work, when you add the fourth relay the ESP8266 stops working correctly. The problem is likely to be the limited 5V power supplied via the USB port.

The ESP8266 draws about 220mA when using WiFi (and more on startup) while each 5V relay draws about 90mA so ESP8266 + 4 Relays >= 580mA. This 580mA is greater than the 500mA must USB ports are limited to. To solve this problem disconnect the Relay board's 5V supply while programming your ESP8266 from your computer and then connect a larger USB supply, 1A or 2A, when you test out the relays.

Remember it is all about POWER.

Conclusion

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 pfodDesignerV3 to design your control for upto 4 switches and then use a Arduino Uno + Cheap/Simple Wifi Shield + DFROBOT Relay Shield for Arduino V2.1 powered by a USB power supply and controlled by pfodApp and, if switching mains power, have an electrician wire it in.

AndroidTM is a trademark of Google Inc. For use of the Arduino name see http://arduino.cc/en/Main/FAQ


The General Purpose Android/Arduino Control App.
pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.


Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2020 Forward Computing and Control Pty. Ltd. ACN 003 669 994