The Problem with Relays

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:

Safety
Safety
Safety

We can also think of two more reasons:

Cost
Power

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.

wMG6R

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.

  1. 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:
    TheFinger
  2. 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.
  3. 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:expeltec-creepage
    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?

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

51zxwPReQSL41qudaT+ugL

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:

SainSmart4

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.

 

 

23 Comments
  1. Which relay board do you prefer to use? Have you found one with isolated grounds?

    • We never found one that was acceptable so we’re in the process of designing our own. It will have 7 relays and is designed to meet the UL 60950 standards. We should have these for sale in about six weeks.

  2. Excellent. I ordered one of the cheap ones to experiment with. I look forward to seeing your relay board.

  3. Hi, excellent article! We are also making a consumer device which incorporates relay modules. I was digging for safety information about these relays and ended up here. Did you guys have finished your design?

    • Yes, they are available for purchase here on the site.

    • Yes, they are available for purchase here on the site.

  4. what is the value of Transistor and diode.
    PLease reply
    Thanks

    • The components would have to be selected based on the the amount of current that the relay coil requires. But let’s say it needs something like 100mA. Then the transistor could be a PN2222A, and the diode could be a 1N4001. You could also use a MOSFET instead of a bipolar transistor.

      • Hi Thankyou for the reply.
        The relay I’m using is 7A 250V, 10A 24VDC,
        so which transistor & diode should I use
        Relay name is HL JQC-3FC(T73) DC 5V

        • The components I called out should work with that relay assuming you’re driving it with 5 volts DC.

  5. Have you ever thought of making a 24V opto output board?

    • We’re considering lots of different things at the moment that involve industrial process control. Can you provide an example of what you’re referring to?

      • At the moment we need to switch various outputs from our system 2-3 times a second for about 10 years. We also need ~1ms switching for some of the outputs.

        • So you just need isolated digital inputs and/or outputs?

  6. Can anyone suggest a link to a design for a robust relay circuit with improved protection that can be drive from the Pi? Looking at 10A max load, 24V

    • What do you mean by “improved protection”?

  7. It’s not necessary to connect the GND pin for the opto secondary side back to the Pi, IF you are using a separate JD-VCC. You discussed this somewhat, and I’m sure a lot of users won’t be aware of this or go to that length, but they could. It’s problematic that the GND connection is included in the connector that has primary side VCC and INx, but only VCC and INx are required to drive the opto primary. They may not explicitly say so, but I think the construction of many relays can be interpreted as resilient to single fault. Certainly interpreting the datasheet and any agency recognitions is necessary. With MOST relays (yes, maybe not all) the relay coil is enameled mag wire, wound on an insulating bobbin, and is then often wrapped with transformer tape for additional insulation. The relay coil is physically separated from the contacts as well. All that to say, YMMV and not a lawyer, but a relay is a very safe device from an isolation perspective and has multiple levels of protection between coil and contacts. The optoisolator can be used as an additional protection, but is often done to avoid transients making their way back to the controller, considering the often non-ideal grounding and wiring used in a proto/breadboard setup.

    Of course, better safe than sorry, especially for the inexperienced, and it wouldn’t hurt to work off of a GFCI outlet when prototyping with mains power. In the final device, it will depend whether the user is actually exposed to any electrically connected part of the device in any way, and otherwise proper case grounding.

    Certainly, the product you now make seems very safe at a glance, with added features to boot. I’m not sure if agency testing is required in order to say you are compliant with 60950, but you may want to reconsider that wording. Designing according to a standard is fine, but saying you are compliant implies you can guarantee it legally.

    • Good feedback JL. To be fair, I am a Systems Engineer as well as an EE so I do have to be a wordsmith at times. And, if I’m not mistaken, I believe I say everywhere that the board is “designed to be UL 60950 compliant.” We’re a small company and cannot afford to go down the rabbit hole of a UL compliance process so we have to choose our words carefully. And as you seem to know, 60950 compliance is more than just protection against single point failures – it’s also meeting creepage and clearance requirements as well as preventing access to potentially lethal voltages.

      So how many can I put you down for 🙂

      • OK, I suppose I can see how “designed to be” modifies the statement, it was just using the word compliant that got me. I guess that would be interpreted as not guaranteeing it to be, but just intended to be. I work for a company that makes both mil=spec products and “pseudo” mil-spec COTs products, and I believe the word compliant was given the thumbs down for COTS products, in favor of “according to”, in instances where processes are similar to but not guaranteed to mil-spec. Again YMMV. Thanks for replying, and cheers.

  8. Excellent article!
    My board is identical but don’t separate VCC and JD-VCC. If I power the module with the 5V of the rpi, these 5V return me to the GPI PIN through the opto isolator. If the GPI PI only accepts 3.3V, could I damage my raspberry with this 5V?

    • You don’t have to worry about the voltage mismatch since your relay board will not be driving the the RPi pins.

Leave a Reply


There are 10^11 stars in the galaxy. That used to be a huge number. But it's only a hundred billion. It's less than the national deficit! We used to call them astronomical numbers. Now we should call them economical numbers. Richard Feynman