The 16 relay module and the Raspberry Pi: not an ideal marriage

  • If you need lots of Relays to switch, the 16 channel relay board(above) that is available at various Chinese webshops seems like a good idea, and in fact it is if you use 5Volt logic.
    It is a complete different matter if you like to use it with a 3v3 system such as the raspberry Pi, or  the ESP series.

To understand why, it is best to have a look at the circuit:

and as that may appear a bit daunting, let’s bring it back to one channel:
The relay is driven by an ULN2803 and the ULN2803 is driven by an optocoupler. In this configuration ofcourse that is completely useless as there is no galvanic separation between the microprocessor and this board and the optocoupler doesn’t add much other than a problem.

  • First problem: the optocoupler inverts the signal: you need a LOW to activate the relay and a HIGH to deactivate the relay.
  • Second problem: you need a HIGH that is pretty close to 5Volt to deactivate the relay:
        • Suppose that you use a raspberry that in its HIGH state puts 3Volt on a GPIO pin. That means that over the optocoupler and R1 there is a voltage of 2Volt.
        • Suppose that the optocoupler has a forward voltage of 1.2Volt, that means that a current of 0.8mA will flow. Not much, but it might just be enough to keep the relay activated.

    On the internet you will find people using this relay board with a raspberry without problems while others struggle. That is simply a matter of variation in specs: suppose you have a module in which the LM2576 step down regulator is at the low end of its specs with 4.8 Volt, the HIGH the raspberry puts out is closer to 3.3 Volt, the optocouplers forward voltage is 1.3Volt and R1 is 1100 ohm rather than 1k.
    Then with a HIGH there is 1.5Volt left over the optocoupler and R1, so a current of 0.18mA will flow which is hardly enough to ignite the optocoupler.
    So, what to do when you have this board and want to connect it to your raspberry and it will just not switch off. Well there are a few options and none is perfect:

    1. You could replace the LM2957 with a 3V3 type, but that is quite an undertaking.
    2. You could get 16 NPN transistors and 32 resistors and build the below circuit 16 times. That will  adapt the raspberry to the  required level of the board and it inverts the signal so you have an active HIGH relay again.The irony though is that each of those transistor circuits in itself is capable of driving a relay.
    3. Instead of the transistor you could try to add an LED to each input line: it still will be OK for when the input signal is O volt, and this time when it is HIGH, the voltage drop of 1.7 volt will be over 2LEDs and a resistor with virtually no current flowing.  Add the LED in the same direction as the optocoupler, so with the cathode towards the raspberry output)
    4. You could just remove the optocoupler and link the emitter and kathode contacts (suggestion from reader Jeroen)

    A python program to test the relays could look like this:

    import time
    pins = [2, 3, 4, 17, 27, 22, 10, 9, 14, 15, 18, 23, 24, 25, 8, 7]
    for pin in pins:
       GPIO.setup(pin, GPIO.OUT) 
       GPIO.output(pin, GPIO.LOW)
       GPIO.output(pin, GPIO.HIGH)
  • Other relay module posts:
    The 16 relay module and the Raspberry Pi: not an ideal marriage
    Re-inverting an inverting relay
    Simple WiFi relay board (3)
    Simple WiFi relay board: a 4 channel DIY WiFi relay board (2)
    Simple WiFi relay board: an overview (1)
    Adding a popular 5Volt 4 channel relay board to a 3V3 processor (beginners)

16 thoughts on “The 16 relay module and the Raspberry Pi: not an ideal marriage”

  1. Seems like these “high side designs” are popular, but they are indeed a total mess. An alternative approach to solve the problem might be to simply remove the optocouplers altogether (4 pins, should be doable), and bridge the input side directly to the output side. It would leave the PCB quite intact, and as you said things don’t get worse in terms of galvanic separation.

    Downsides I can think of seem minor: slightly higher driving current and inverted operation. Back-feeding impulses into the microcontroller seems very implausible to me: these transistor arrays are used as direct drivers to stepper motors in millions of projects.

    1. you are right, total overkill those boards. I could seethe point if the optocoupler indeed completely galvanically isolated the board but primary side and secondary side share the same Vcc.

      Would have been much better if the optocoupler Vcc was separate and an LED was added. That is actually how the 4 channel board works.

      I was wondering if it would be possible to separate the primary Vcc’s from the optocouplers, but that probably will take too much cutting and soldering wires. Your proposal might in fact be the most sensible. Making 16 level adapters with NPN transistors is as I said slightly ironic as one could just as well directly hang the relays on those.
      On the board itself, just the 2x ULN2803 would have been more than capable to take direct input from a microcontroller and drive the relays.
      By the way, your comment was the 1500th one, not counting spam

    2. a couple of years ago you described what sounds like the best solution to make the 16 relay pcb connect to a Rpi. I’m not an electronics expert like you but I’m trying to build a project with my son using these two parts. Can you expand on your simple answer buy explaining what IC’s and which pins to cut and where jumper need to be placed?

      And your description was as follows:
      “simply remove the optocouplers altogether (4 pins, should be doable), and bridge the input side directly to the output side.”

      Thank you in advance for any help you can provide.

      1. Harvey, Isaac, it was reader Jeroen who suggested that and i am not sure how soon he will read your question. I think that before i would start cutting tracks, especially on a 16 channel board i would consider just building a new board with just 16 transistors, 16 relays, 16 diodes and 16 resistors.
        Can do it even simpler with 2 uln2803 ic’s and 16 relays.
        Not sure how old your son is but it might be eve more educational to do that.
        Anyway if you insist on cutting traces on an existing board i’d be more than happy to help you along. Also if you decide to build it completely yourself, i d be happy to assist with advice should that be necessary

  2. Very informative post. Kinda makes you wonder how the optocoupler made it in there in the first place, because normally everything the drives the cost down is removed and it clearly wasn’t added by someone who knows how optocouplers work (but maybe that’s why?) 😀

    1. Unfortunately, people think that “optocouplers=safety”. Their absence—to most consumers—then becomes a factor which creates fear, they buy someone else’s board.

      1. True, sadly though what is and what isnt clearly stated ofcourse depends where you buy it and that it is for 5 volt and not really suitable for 3.3 is exactly the point of my post. I merely describe how to get it to work in spite of that obstacle.

        Having said that, the design of the entire board is total useless overkill. The use of optocouplers here serves no purpose as they are being fed from the same powerline.
        It would have been better had there been totally galvanically isolated optocouplers, that had their kathods tied to ground and the anode side as input. It then could easily work with any almost any triggr voltage and would not invert

    1. Yes indeed, but for 16 channels you would need 2. Other alternative is a cheap fet based level converter module, but it all adds to making the board suitable to use, while it could have been so much simpler.

  3. A MCP23017 and a bi-directional logic level converter make interfacing this board with a Raspberry Pi quite easy as well as 3.3v compatible. Both can be purchased as breakout boards. I have been using the MCP23017 with both the 8 and 16-channel versions of this relay board in a custom HVAC zoning controller for about a year with no problems. You can even mount the MCP23017 directly to the relay board by installing stacking headers with long pins on the MCP23017, then simply plug that onto the header on the relay board and add a dab of hot glue or similar to keep it from coming loose due to vibration, etc. The pins will be a bit out of order if you do it this way, but it works quite well and keeps things compact.

    1. That is certainly possible. There are more ways to skin a cat, but it means adding yet more hardware to simply trigger a relay 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: