How to modify the Beefcake module to make it compatible to 3.3V logic
The Beefcake NPN transistor has a current limiting resistor R2 of value 1K. This resistor limits the base current at Arduino 5V logic high level. The base current within limit, after amplification (usually hFE > 100), is big enough to engerize the coil.
Calculation of Arduino 5V GPIO current into Beefcake relay module:
Arduino current i ~ (4V [Arduino High] – 1V [Vce(sat)]) / 1K [R]) = 3V / 1K = 3mA
However, the Rpi’s logic High signal is lower than Arduino, so the corresponding limited current is smaller and after amplification is not big enough to drive the coil.
Rpi current i ~ ((3V [Rpi High] – 1V) / 1K = 2mA
The modification is simple – just replace 1K R2 with a smaller resistor, say 510R.
Rpi current i (after modification) = (3V – 1V) / 501R = 4mA
I am making education guess based on circuit analysis and experimentation. I think my guess is 90% likely correct.
Though the small signal NPN transistor 2N3094 can be used for small load switching, it is not that reliable. For relay switching, it is safer to use power transistors such as SS8050, UDN2981, specially designed for inductive loads.
The OP wants a safe method which would not fry his Pi, so for reliability, a source driver such as UDN2981 is the way to go.
# Solution 2 – Using UDN2981 to drive the Beefcake relay module #
The comments point out that the OP’s Sparkfun Beefcake relay module is high level trigger, therefore the commonly used sink driver ULN2803 cannot be used. A driver similar to ULN2803, but current sourcing, rather than current sinking, should be used instead
I think UDN2981 is a suitable driver for the OP’s relay module.
I verified successfully UDN2981 driving a high level tirgger relay module similar to the Beefcak, and ULN2803 a low trigger one. Below is a summary.
UDN2981 controlling High trigger, NPN transistor input type relay modules
I first manually tested UDN2981, without connecting to Rpi, to blink 4 LEDs, to make sure the circuit is working OK.
Then I setup 4 NPN transistor input type relay modules (KY019), and connected the 4 relay module inputs to 4 UDN2981 channel outputs.
Then I connected 4 Rpi 3.3V GPIO pins directly to the 4 UDN2981 channel inputs. I used the following python function to toggle 4 relay modules at 25 cps.
The result was good. The 4 relay modules click and LEDs blink at 25cps as expected. The Rpi GPIO output signals stayed near 3.3V, and UDN output signals around 4.0V, implying that no input was overloaded.
UDN2981 controlling High trigger, opto-isolated relay modules
The OP also considered using opto isolated relay modules because they are safer. I have successfully used the same UDN2981 to control 4 High level trigger, opto isolated relay modules (MK055).
Actually UDN2981 can be used to control any kind of High trigger modules, no matter NPN transistor or opto isolated types.
However, for Low trigger modules, whether PNP transistor or opto isolated, source driver UDN2981 does not work, ULN2803 or other sink driver should be used.
ULN2803 controlling Low trigger PNP transistor input or opto isolated relay modules
I successfully verified ULN2083 sink driver can control 4 Low trigger opto isolated relay modules. I first tested manually blinking 4 LEDs, then used the same python function above to test the 4 modules. The results was also good.
ULN2803 and UDN2981 Pros and Cons
- ULN2803 and UDN2981 can be directly driven by TTL or CMOS logic signal with 3.3V or 5V supply voltages.
- Their rated 500mA outputs with clamp diodes are suitable to switch relays and stepping motors.
- ULN2803 and especially UDN2981 are not so common.
- They have 8 channels and therefore have a bigger 18 pin DIP package size. For less channels, more common 74HC03/04 or 74HCT125 with 14 pin DIP package are more common and easier to handle..
# Solution 3 – Using 74HC03 and 74HC04 to shift up RPi’s 3.3V GPIO signal #
Using UDN2981 to drive a relay module is a big over kill, because they are designed with built in fly back diodes to directly energize relay.
The UDN2981 is not common and not for beginners to experiment. For beginners, the very common and cheap logic gate ICs, 74HC03 Quad NAND gates, and HC04 Hex Inverters can do the same job as UDN2981, shifiting up 3.3V logic signals.
I have successfully verified HC03 and HC04 shifting up 3.3V logic to 5V and found it working for both transistor input and opto isolated high level trigger modules.
# References #
Arduino Voh 4.2V, Vol 0.9V
Rpi Voh 2.4V, Vol 0.7V
# A.3 Logical Level Converter using HCT125 #
So I tested another up converter, HCT125. I was happy to find that it works well. The HCT125 converted 5V0 signal did not drop when connected to the NPN transistor driven relay module.
End of Appendices
** * Long Answer To be deleted * **
This long answer is too long winded and messy. I am now trying to remove the irrelevant paragraphs and perhaps replace them by asking relevant question and answering myself.
How to check Photo Coupler / Opto Isolated Relay Module
- Get a jumper wire.
- Connect one end to signal/input pin of relay module.
- Hold the other end and touch the Vcc(+) and Gnd(-) pin and check the results below.
2.1 Transistor input type
For the popular bipolar NPN transistor input type, the sourcing driver signal (Rpi GPIO signal or RPi GPIO signal after 3.3V to 5V logical level conversion) goes to the base of the transistor through a series LED and biasing resistor.
There are other not so popular relay switching circuit as described in this relay switching tutorial
2.2 Photocoupler input type
The photocoupler input type relay has a phtocoupler as the input. The photocoupler drives another transistor which in turn drives the relay coil.
Appendix C – Logical level converter using TXS0102
Now I know that Rpi GPIO can directly drive the relay module, but there are two problems. First, the GPIO signal with a long connecting wire is noisy, therefore not that reliable. Second, the flywheel diode 1N4148 might not completely suppress the coil back EMF, and if unluckily the 1N4148 breaks down, or not properly connected (poor contact, dry soldering joint etc), the back EMF might damage the Rpi.
So I decided to use a logical level converter to shift up the Rpi GPIO signal from 3V3 to 5V. I first tried TXS102 converter and found it working well. Besides shifting up the GPIO siganl, the noise at the high level is also greatly reduced.
However, I found a big problem when feeding the converted 5V GPIO singal to the relay module. The relay still turned on and off as before, with the 3V3 signal, but when I used the scope to check out the waveform, I found the very surprisingly that 5V signal dropped by half, to 2.2V.
I suspected the reason was that TXS0102 can sink current much better than sourcing current to the relay module. To verify my guess, I fed the 5V signal to another relay module, a pull down photocoupler type, model MK01.
This time I found the 5V signal did not drop any noticeable amount.
So I quickly concluded that the NPN transistor type relay module is a bad choice. I would stop testing this kind of relays from now, and move on the photocoupler type of relays.
I also tested another photocoupler driver module MK101. This module has a jumper to select Higher trigger or Low trigger. I found that for Low trigger, the TSX0102 converted 5V signal level is not affected. But when Low trigger is selected, the converted 5V signal level dropped to around 2.5V, though the relay is still working.
Appendix E – Logical level converter using HC04
HCT125 is not so common. So I tried one more converter circuit, using HC03 quad open drain NAND gate and HC04 hex inverter. When I tested the HC04 output, I found it very noisy. I guessed one reason was that I was using dirrerent power supplies, one for rpi, another for the converter. Even I connected the ground points of the power supplies to make a common point, the noise did not go away. I then used one power supply for both rpi and converter, and the noise disappeared.
I tried the HC04 output signal for the relay module in Low trigger mode (which requires sinking current, but not in High trigger mode (which requires sourcing current), . So I am going to add the HC04 hex NOT gate which can source current to the relay module.
Appendix F – HC04 Level Converter Floating Input Problem
Last time when I first tried the HC03 based level converter, on a photocoupler relay module, I found that if I left the input floating, the module picked up the noise and the relay switched on and off crazily. I thought the frequency was perhaps 1kHz. I was not sure if it was some sort of positive feedback oscillation. But when I used the scope to check out, I surprisingly found it was 50Hz! I guess it is some sort of resonance. But I don’t know what is the difference between resonance and oscillation. Perhaps I should goggle again. Anyway, I think I need to add a pull up/down resistor somewhere.
Below to be shortened or deleted
# Appendices #
# A1. Opto isolated / Photo coupler relay module board and schematic #
Opto isolated relay module has a photo coupler which is 4 pin IC. The picture below shows a photoCoupler PC1 (with its 4 pins labelled 1, 2, 3, 4 in green) and a transistor Q1. The ICs are not always marked. In this picture, PC1 is EL354, and Q1 8050.