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:


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.

  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:
  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 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:


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.



  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

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

  9. My solution to this is I use the mechanical relay to trigger an Opto 22 240D25 SS relay. Its an additional cost, but adds an additional layer of isolation. You can find these Optos on ebay, I paid $15 for 4.

    • I use a single, isolating power supply for the relay coils that does it for $5. As a fabricator, I can’t use eBay as a vendor.

      • optos are obviously available retail from suppliers like Grainger, but they aren’t going to be 4 for $15, they run around $25 – $50 brand new.

        I think the issue is that these relay boards aren’t necessarily made with the highest standards, and you’re still running 110-240V to the relay board which in theory COULD short the trigger

  10. Traceback (most recent call last):
    File “/usr/local/lib/python2.7/dist-packages/piplates/”, line 24, in,1)
    FileNotFoundError: [Errno 2] No such file or directory
    Cold you do a simple video on how to connect the rele module? and tested.

    • In the documentation on our website, there’s a page called “Getting Started.” Refer to the section labeled “Enable SPI on Your Raspberry Pi” to fix your bug.
      As for videos, we prefer written documentation. However, we may consider videos in the future if the demand is high enough.

  11. Hello,
    I am just getting started with Raspberry Pi and am looking for a relay to “input” a status into the Raspberry Pi. The situation I am working on is on a boat, that has 240v/125 50amp service. I am looking to build a way to monitor if there is a power outage and use the Raspberry Pi to send an SMS message.
    The relay will be energized across the 120v circuit holding the relay closed (could be open I guess) and when the state changes, the 3.3 or 5v on the other relay circuit would be sensed by ?? on the Raspberry Pi?

    thank you

Leave a Reply

Einstein was a giant. His head was in the clouds, but his feet were on the ground. But those of us who are not that tall have to choose! Richard Feynman