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:- You could replace the LM2957 with a 3V3 type, but that is quite an undertaking.
- 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.
- 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)
- 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 GPIO.setmode(GPIO.BCM) 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) time.sleep(0.001) GPIO.output(pin, GPIO.HIGH) time.sleep(0.001) GPIO.cleanup
-
- 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)