Controlling an AC Load from a Microcontroller: An Introduction to Relays
Picture this, you have a great idea for an IOT project which involves making your electronically challenged home appliance smarter. However, you come across an obstacle, your microcontroller's 5V or 3.3V DC output is incompatible with mains 110VAC or 230VAC. You need a component that can somehow interface between the two while providing isolation between AC and DC voltages. This component is known as the relay which comes in two main varieties, electromechanical and solid-state relays.
Solid State Relays
Solid state relays (SSR) operate on the same principle as optocouplers, they contain an infrared LED which is turned on by the low voltage side (the microcontroller). This LED activates a photodiode or a photo transistor which switches on the high voltage circuit. This kind of setup benefits from the galvanic isolation between the low voltage and high voltage sides. SSRs are beyond the scope of this article. Feel free to learn about them here.
Electromechanical Relays
Electromechanical relays on the other hand are electromagnetism-based. A coil wound around an iron core creates an electromagnet when energized by a low current. The resultant magnetic force moves a contact which closes or opens the high voltage circuit. Electromechanical relays are typically cheaper than SSRs but SSRs are smaller and, due to lack of moving components, live longer (Storr, 2022b). Electromechanical relays are susceptible to the arcing effect especially with inductive loads. The arcing effect not only degrades the connectivity of contacts overtime, but it can also weld them.
Parameters:
Relay G5LE-1 DC5 will be used as an example throughout this article. To choose a relay for our application, we must understand the relay's parameters.
Mounting Style and Size:
Relays come in a variety of mounting styles. Two popular styles are DIN rail mounted relays which are mostly used in control panels and systems, and PCB relays such as the example relay. Some are designed to be mounted on sockets. Remember to ensure that the size fits your application.
Environmental ratings:
To ensure reliability, the relay must be selected to withstand the weather conditions in its installation site. The first environmental rating is the maximum or minimum operating temperature. Our example relay can be operated between -25C and 85C making it suitable for outdoor use if enclosed properly. The second environmental rating is humidity. Care must be taken to protect the PCB or the system as a whole from humidity because even if the relay can withstand it, it might cause short circuits between the pins.
Coil Ratings:
The coil's voltage rating is the voltage it is meant to be operated at. Typical voltages are 3V, 5V, 12V, and 24V. Some relays are designed with AC coils, they are typically more expensive and are usually used for indication purposes. The coil's power/current rating or resistance is used to determine the energy required to operate the coil. The example relay has a current rating of 79.4 mA. Quite strangely, the datasheet neglects to mention an existing model which is G5LE-1 DC3 however, the current requirement can be easily obtained using the coil's power consumption of 400 mW which is constant across all models to obtain a current rating of 133mA.
Note how the datasheet includes a chart illustrating how temperature derates the maximum coil voltage. At 60C, the coil voltage may not go above 150% of the coil's voltage rating even if for a short duration.
Contact Ratings
The contact's voltage and current ratings are not to be exceeded when connecting an AC power supply. In fact, according to figure 7, the contact's current rating derates as the contact's voltage increases. Operation cycles also derate with increasing voltage. The example relay is not suitable for use with 220-230V which is common in many countries. It is worth noting that relays include two different current ratings. One is for resistive loads which is higher, and the other for inductive loads such as motors.
Relay Circuit Design
Circuit 1
Illustrated above in figure 8 is a circuit in which the microcontroller's GPIO pin directly powers the relay's coil. Our example relay draws approximately 80mA of current at 5V. Typical microcontroller GPIO pins supply no more than 20mA of current. The best-case scenario in this circuit is that the relay will not operate. while the worst-case scenario is that the GPIO pin if not the whole microcontroller will be fried.
It is apparent that another component is needed to interface between the microcontroller and the relay. A component which can operate with the little current that a GPIO pin can provide and switch a higher current. This component is either a transistor or a an optocoupler.
Circuit 2
In this circuit, the transistor acts as an electronic switch to drive the relay. The transistor must be sized such that the following:
Assume that the transistor was enabled by the GPIO pin then switched off. What will the transistor's current state be? Will the gate's charge remain leaving the transistor switched on? or will the gate discharge switching off the transistor. This is the purpose of adding a pull-down resistor connected to the gate. It ensures that the transistor is always at a known state. (Storr, 2022a)
What if the coil is switched off while it is energized. The relay's coil is essentially an inductor, it stores energy in its magnetic field. If the circuit is switched off abruptly. The coil will attempt to push current through thereby increasing its voltage which may break the transistor. A surge protection component must be added to the driver circuit to safely discharge the coil's energy without damaging the transistor. This component is a flyback diode.
Recommended by LinkedIn
Circuit 3
Just like the transistor, the diode must be sized appropriately with respect to the coil:
How does the diode affect the relay besides adding the aforementioned path? Relay contacts will experience the arcing effect during de-energization and small tack welds may form intermittently. If the contact's movement is fast enough, these welds may be broken off easily. The observed effect of adding diodes for surge protection is that they slow down discharging the coil. This in turn slows down the contact's movement which prolongs the arcing effect and suppresses the contact's ability to break the tack welds. At some point, this may lead to permanently welding the contacts as discussed earlier. This situation can be improved with the addition of a single component to the driver circuit.
Note: The negative effects above apply to normally open contacts only, normally closed contacts benefit from slow contact speed because it reduces contacts bouncing while closing.
Circuit 4
Adding a Zener diode can improve the speed of discharging the coil's energy. The diode's purpose in this case is to avoid forward biasing the Zener diode thus restricting the current flow to one direction. Another option is to have the Zener diode parallel to the transistor, but the first option is better because the Zener diode is closer to the coil.
Other surge protection mechanisms exist such as using a varistor parallel to the coil or a diode in series with a resistor, but the solution presented in circuit 4 was cited as a good balance between performance and cost. (TE Connectivity, 2021)
Note: The solution in circuit 4 applies to DC coils. For AC coils, using a varistor is recommended. (TE Connectivity, 2021)
PCB Layout
Proper placement and routing of the relay is as important as designing its circuit. The following recommendations must be considered:
Some SPDT relays are packaged oddly such that one of their contact pins is on the coil's side. This makes PCB layout challenging because the pin is within the DC section. Avoid these relays if possible and ensure enough clearance between the contact pin and other DC circuitry.
Safety
Considering that relays are typically used to control AC loads. Exercise great caution while installing the relay. Ensure that power is isolated while working on your electronic device and insulate the AC conductors to avoid shock. Follow IPC-2221 with regards to PCB design and IEC-60950-1 for power supply safety requirements. (Altium Designer, 2017)
Contactors
It can be deduced from the information above that relay's performance worsens with high power inductive loads such as pumps or compressors. It is recommended to use contactors instead in these applications. Contactors are essentially rugged relays with stronger contacts built to withstand higher powers (RS, 2021). Contactors are commonly found in DIN rail mounted packages such as the one in figure 13 or chassis mounted varieties. Contactors typically require higher coil voltages than relays thus, you may need a relay, an SSR or at the very least a transistor to operate a contactor. At the time of writing this article, no PCB mounted varieties were found.
Conclusion
With this article, you, hopefully, learned how to select a relay for your application, how to design a circuit around it, and other considerations that drive your decisions. SSRs generally follow similar principles in selecting and designing them but they deserve their own article. Please, consider sharing this article if you found it useful.
References
Altium Designer. (2017, August 11). High voltage PCB design: Creepage and clearance distances for high voltage. Altium. https://resources.altium.com/p/high-voltage-pcb-design-creepage-and-clearance-distance#creepage-vs-clearance
Gust, B. (2020, August 5). Component options for relay coil surge suppression. TechForum │ Digi-Key. https://forum.digikey.com/t/component-options-for-relay-coil-surge-suppression/7789
Omron Corporation. (n.d.). Cubic, Single pole 10A Power Relay Datasheet. Omron. https://components.omron.com/us-en/sites/components.omron.com.us/files/datasheet_pdf/K100-E1.pdf
Panasonic Industry. (2020, April 15). Protecting a Relay Coil from a Surge. https://ac-blog.panasonic.com/relay/protecting-a-relay-coil-from-a-surge
RS. (2021, October). A Complete Guide to Contactors. RS. Retrieved September 2, 2023, from https://uk.rs-online.com/web/content/discovery/ideas-and-advice/contactors-guide
Storr, W. (2022a). Relay Switch circuit. Basic Electronics Tutorials. https://www.electronics-tutorials.ws/blog/relay-switch-circuit.html
Storr, W. (2022b, August 2). Solid state relay. Basic Electronics Tutorials. Retrieved September 2, 2023, from https://www.electronics-tutorials.ws/power/solid-state-relay.html
TE Connectivity. (2020a, September). Coil Suppression Can Reduce Relay Life. Retrieved September 2, 2023, from https://www.te.com/commerce/DocumentDelivery/DDEController?Action=srchrtrv&DocNm=13C3264_AppNote&DocType=CS&DocLang=EN
TE Connectivity. (2020b, September). Contact arc phenomenon. Retrieved September 2, 2023, from https://www.te.com/usa-en/products/relays-contactors-switches/relays/intersection/contact-arc-phenomenon.html?tab=pgp-story
TE Connectivity. (2021, September). Relay Coil Suppression with DC Relays. https://www.te.com/usa-en/products/relays-contactors-switches/relays/intersection/relay-coil-suppression-dc-relays.html?tab=pgp-story
We often give relays to our customers and use them more.
And do not that DC voltage is often limited to 30Vdc if you want to do some serious currents.
Nice doc, thanks for sharing!
Boa tarde! Show de bola, você é excepcional.
Here important point is Which type of load you connect to relay so Relay rating decide.