Why is it hard to find relay boards that are designed to work specifically with the Raspberry Pi? After all, they’re pretty simple devices, right? Well, we can think of three reasons to start:
We can also think of two more reasons:
Let’s tackle the first topic since it’s really the meat of the issue. What are relays good for? Relays are excellent for switching high voltage / high current loads with low voltage devices like a Raspberry Pi. The most typical application is to switch AC line voltages which are 120V in the U.S. and 240V in the EU. Since the coils in most relays require more current that the RPI can provide, it is common practice to drive them with a transistor as shown in the schematic below. When the GPIO signal to the left of R1 goes high, the transistor switches on and pulls current from the +5V supply through the relay coil. That, in turn, produces a magnetic field that pulls an armature into contact with a normally open terminal thus closing the switch. The coil in the relay is electrically isolated from the contacts with breakdown voltages typically around 3000VAC. Therefore, relays are an ideal component for using a low voltage drive to control a high energy load.
So, what are the safety issues? The primary safety standard in the U.S. for IT equipment is called UL60950. Among other things it prescribes the rules for the design of devices that connect to hazardous voltages.
- When connected to line voltages, some of the leads and pins on the relay are at lethal potentials and the user should not be able to touch any of these points. The solution here is an enclosure or a layer of insulation that is either permanent or requires a tool to remove. The UL standard goes so far as to include the design of a “finger” that is used for testing access to traces and pins that may be at high potential:
- In the event that the relay breaks, high voltages can cross over onto the low voltage side and destroy your Raspberry Pi as well as putting those lethal voltages on all of your wires and traces. UL 60950 states that a design should continue to be safe after a “single point failure.” Therefore, a safe design requires an extra layer of electrical isolation such as an opto-isolator between the RPI and the relay coil and an isolated power supply for driving the relay coils. In the event of a single point failure, these components may have hazardous voltages on them and so they also have to be covered and made inaccessible to the user.
- When using high voltages on a printed circuit board, attention has to paid to the distances between the low voltage traces and the high voltage traces. Specifically, the PCB designer has to be cognizant of what is called creepage and clearance. The following diagram explains these two terms:
These parameters also apply to the traces between adjoining relays if more than one is in use. There is a website called creepage.com where you can calculate these values based on the voltages you are designing for as well as your pollution class. If we select 120VAC, reinforced insulation, and pollution degree 2, we see that we need 2mm of clearance and 3mm of creepage for a FR4 circuit board.
And those other two reasons?
- Cost: a) All components that can energize the low voltage side of a relay circuit with hazardous voltages after a single point failure have to be electrically isolated. This requires the addition of opto-isolators and isolated power supplies for the relay coils. b) To make a safe relay board, all exposed pins and traces that can be at a hazardous potential have to be enclosed or covered. This also includes any pins and traces that can be hazardous after a single point failure.
- Power: this is an often overlooked issue but relay coils are not low power devices. A moderately powered one can pull 40mA at 5VDC. If you have 8 of those on a single board you’re pulling 320mA when they’re all on. If you’re driving the coil power supply with the 5VDC from your Raspberry Pi you have to make sure that your power source can handle the additional load. And if you have more than 8 relays, you may have to use a dedicated supply.
Let’s take what we’ve discussed and examine a popular relay board that is sometimes connected to the Raspberry Pi: the SainSmart 5V Relay Module. This board is available at Amazon Prime for $10.19 and there’s quite a bit of documentation for it. Here’s pictures of the top and bottom:
The first thing we see is that there’s obviously no enclosure or cover over the bottom of the board. So, all those exposed terminal block and relay pins on the bottom will potentially be “live” when the board is in use. There are U shaped slots between the middle relay contact and the ground to satisfy the creepage requirement. Looking at the top of the board we see that there’s no cover over the screw heads on the terminal blocks and these too may be at a hazardous voltage. Now let’s look at the schematic and discuss protection against single point failures:
SainSmart does isolate the relay coils from low voltage side through opto-isolators (U1, U3, U5, and U7). And it even appears that if you choose, you can drive the relay coils with an isolated power supply attached to pin 2 of J? (<– that’s not a typo) called “JD-VCC”. The problem is that everything is operating from a common, non-isolated ground. In the event of a single point failure that ground will float up to a hazardous voltage potential which pretty much makes the opto-isolators nothing more than a decoration in this design. So, if you choose to purchase and use this board, all we can say is: BEWARE.