25: PROBLEMS WITH RELAY MODULES
Many people who tried to use a relay module with a raspberry PI has had difficulties getting it working, often finding what happened counterintuitive. like always going on, even when programming it as off, then doubting their software instead of the module. The main reason is that these modules are designed to work with a control system (like an Arduino) that uses 5V signalling, and the designers of these modules have not considered what would happen when one would try to use the module with 3.3V signalling, nor do the sellers of these modules care.
Lets look at a typical schematic of such a module:
this module contains an opto-isolator, (U1) which is great as it prevents any dangerous voltages that may occur when a current that is running through a coil (the relay coil) is forced off suddenly. This will create a very large voltage (called the EMF voltage, which can be hundreds of volts) in opposite direction of the current flow. This voltage is normally dissipated (shorted) into a "snubber", "flyback" or "back EMF" diode, but if this diode fails, the transistor might be destroyed, and the high voltage might end up at the base of the transistor (on R2) which can destroy the controller without the opto-isolator in place.
In the case of the above schematic the relay control circuit is wired to be "active low", that is a low voltage (compared to the relay control supply voltage, on pin 2 of the left connector block) will light up the LED in U1, which will send current trough R2, this will make the photo transistor in U1 conductive, which will open the transistor Q1, thus powering the relay coil.
However there are problems with this design;
normally the opto-isolator is powered with 5V on pin 2 (and GND to pin 1), and the IN0 input is expected to either go to 0V (GND) or 5V.
When grounded a current will flow through both LED's and the relay coil will be powered, and when 5V is put on the IN0 input there is no voltage over the R1-LED_of_U1-green LED string so both LEDs are off.
But if you put 3V3 on the IN0 pin, this isn't true! there will still be 5V-3.3V = 1,7Volt across both LED's, so a small current might still flow, maybe just enough to dimly light the LED in U1, lighting the photo transistor so that a small current trickles to R2, and the relay coil might get just enough energy to keep drawing in the relay armature, (metal plate that is attracted by the magnetic field) and so the relay may still keep its energized condition. A relay needs much less energy to magnetize the relay so much that the armature keeps stuck, than the energy needed to pull in the armature.
one could program the IN0 GPIO so that it becomes high-ohmic, and that would turn the relay off, but the downfall of that approach is that there might appear a voltage on the GPIO larger than 4V, and this might cause the dreaded "latch up" phenomenon as the GPIO's of a PI are NOT 5V TOLERANT! See https://en.wikipedia.org/wiki/Latch-up for an explanation of what might happen. Just maybe the two diodes (LED's) might prevent latchup in practice, so it might work, but your mileage might vary!
The obvious solution to this problem would be to lower then voltage on pin 2 of the left connector block to 3.3V (but still leave 5V or perhaps 12V on pin 3 for the relay) but then you would notice another problem! To spare the need (price) for an extra resistor for the green LED, the designer of these modules has simply put the two LED's (the green signaling LED and the LED in the opto-isolator) in series, but that means that you need more than the sum of the two forward voltages of the two diodes (a LED is a diode) to get any amount of current running through them, that is to say (with the typical forward voltages of LED's, with a color from red to green, of about 2 Volt) we will need more than 2 x 2 = 4Volt, so 3.3Volt is not enough to light up the two LED's!
This renders these relay modules very hard to use with a 3.3V based system like a raspberry PI.
This problem is exacerbated by the fact that the seller of these devices (especially on e-bay) almost never let you see the schematic of the relay modules you can buy.
One solution would be to keep pin-2 on 3.3V but short out the indicator LED, but that means soldering ( a skill that anyone calling himself an electronic engineer cannot go without by the way, and its not as hard as it looks, just mind not to touch any of the metal of the soldering iron during use).
The best solution would be to keep the pin-2 voltage at 5V, and use another transistor (and base resistor) to turn on and off the LED current.
or simply buy a better suitable relay board, something simple without an opto-isolator like this will work fine:
you can also use this design with a 12V relay without problems.
Test without software control, before adding software control!
To test if a relay board works with 3.3V signaling, simply connect the control input directly (manually) to either 3V3 (3.3Volt) or GND, and see if it works as expected, after that you can worry about controlling it with software. Also, you can use a LED to test your software before connecting the relay board, if you have doubts about your software.