Short Answer V0.1
The OP’s relay is optoisolated, low level triggered designed for 5V Arduino. This type of relay switches on if input signal is less than 1V, and switches off when input is greater than 4V.
Arduino has no problem because its high low signal meets the spec. However, 3V3 Rpi’s low signal meets spec, but high signal is only around 3V, therefore cannot switch off the relay. In other words the relay is always on.
There are 5 methods to get around, and a schematic is helpful to explain.
/ to continue, …
The very long answer below is to be shortened later
I am 90% sure that your module cannot be directly controlled by Rpi. I will first point out the problems, then suggest ways to get around.
I will search my junk box to find a relay similar, and do some experiments to verify my guesses are correct.
Luckily I found a similarly looking guy. First thing first is to check out if it is (1) low logic level activate/triggerable, and (2）5V Logic compatible.
Me no English, so I selfie!
Now that I know the relay module switches on when In1 is Ground, and off when In1 is 5V, I move on to test (a) how high, say, 3V or 4V can still turn it off, and (b) how low, say, if 1V or 2V can still turn it off.
There you go, selfies again.
Earlier I though my module similar to the OP is not Rpi compatible either. But I surprising found that its trigger points are approximately 1.8V- and 2.4V+. Since Rpi’s low level is < 1V, and high level > 2.8V, therefore Rpi has no problem switching it on/off. (Arduino high/low are approximately < 1V and > 4V, so also has no problem.)
So now I still need an example similar to the one OP has, to explain why Rpi’s low signal is OK to switch it on, but Rpi’s high signal is not high enough to to switch it off (without fiddling with the JD-Vcc jumper, which most newbies never heard of!). I searched my junk box again, and did similar engineering experimentation with the results below.
Now the time has come for me to answer the OP’s question about using the JD-Vcc way of supplying power to the relay, and an example of Rpi Python code to control it.
I am using a relay recommended by Forbes. You might like to read the following Forbes article before I explain. But the picture first.
Everything You Need To Set Up Raspberry Pi Home Automation – Don Reisinger, Forbes(recommending an optoisolated, low level trigger relay) https://www.forbes.com/sites/forbes-finds/2018/07/12/everything-you-need-to-set-up-raspberry-pi-home-automation/#7c65bdb04cdb
Will this code harm rpi, and how does JD-Vcc work?
GPIO.setmode(GPIO.BCM) … code used to trigger relay using raspberry pi, connected 5v source to jd-vcc and 3.3v to vcc of header. Is there any harm in using the above …
connected 3.3V of relay to 3.3V of the RPi. JD-VCC to 5V of the power supply. ground of relay to ground of power supply. Now it’s working. The question now is, why isn’t the relay working when I connect the VCC of the relay to 3.3V of the power supply. …
To answer the above questions, we need first to see how the photocoupler works. Here is the picture.
Now that we know the specification of the photocoupler, we can do some quick and dirty calculations, to make sure that the relay will not harm you rpi GPIO.
Not using Rpi’s 40 pin connector’s 5V, 3V3 power pins to power relay
Just now I read your question again and I gave it an up vote, because of the more than ten user questions I read so far about this FAQ topic, yours has the most detailed description of the frequent newbie problem of Rpi not being able to turn off a low level trigger relay which Arduino has no problem, because the relay was designed for Ardunio, perhaps before Rpi was born.
I particularly appreciate your specifically pointing out that you are using the external power supply unit, even with a web link. This is a very important guideline for newbies often connecting the wrong wires by mistake. Actually I have been using Rpi for 5 years, and I NEVER USED the RPI’s 40pin connector power pins to power any external device.
Low Trig Opto EL817C 5V Relay Rpi GPIO Input Current Requirement Calculation
Working backwards, Ic needs only 1mA. Working forward, If 5mA would generate 10mA Ic, fully saturate photo transistor. Conclusion – Rpi GPIO Low need only 5mA to fully saturated photo transistor.
The question is Rpi GPIO High too High to cut off photo transistor？
Now I am going to measure the current flowing into low level signal (power ground, or Rpi low). The objective is to check if current flowing into Rpi GPIO signal pin is too large to damage the Rpi. I am going to check two types of low level relays, (1) without optoisolation, (2) with optoisolation, similar to the OP’s relay. The results shows that for this relay, signal current is between 0mA~4mA, safe enough for Rpi, but not compatible to Rpi, because Rpi High is not high enough to switch on this relay.
The second relay tested is low trigger PNP type relay, as shown on the left of the schematic below. One way to get around the Rpi high level signal is not high enough problem is to increase the series resistance, so that less current flows into the LED and therefore cannot drive the photo transistor into saturation.
Next to measure the current for OP’s opto isolation type low trigger relay.
Now I am measuring the currents of the optoisolated, low level trigger relay that looks similar to the OP’s dual relay module. However, the OP’s module does not work with Rpi, unless Vcc = 3V3, and JDVcc = 5V. For this relay just tested, JdVcc = Vcc = 5V works for both Arduino and Rpi.
If If is only -2mA, I need to check the CTR at this point and see if Ic and Vce(sat) etc. So I checked out the CTR pic and found CTR at 2mA is 70%. So Ic is 2mA * 70% = 1.4mA, should be safe enough, …
To avoid frying Rpi, first test manually, by hand, using a jumper wire to select hardware signals, from breadboard PSU module, 5V, 0V, 3v3 power rail, and 555 square wave etc, to test relay, BEFOREusing Rpi GPIO by python program.
Summary of three Relays Tested So Far
Now I am going to write a Rpi Python program to toggle the third relay module. First, a summary of the three relay modules so far tested.
(1) 5V dual channel, with optocoupler, low level trigger. This relay is identical to the OP’s relay, except one thing:- OP’s relay is NOT Rpi compatible. This one is. Because I cannot repeat OP’s failures using this module, so I found the second relay to try.
(2) This relay module has not optocoupler, but only one PNP BJT directly driving the Songle relay switch. Since this is not like OP’s module with optoisolation. So I tried another one.
(3) This is similar to OP’s modules, but only single channel, and without JDVcc jumper for separate control logic and relay switch power supplies. I found this NOT Rpi compatible, so is ideal to test out for the OP’s question.
The Rpi Python Program to use logical level shifted up GPIO signal to drive relay.
OP has at least three ways to get around, (1) Using JDVcc and Vcc PSU, (2) Setting GPIO pin to input, in order to switch off the relay, (3) using a logical level shifter to convert Rpi GPIO 3V level to 5V.
I think the first two solutions are not safe for newbies. For newbies, I recommend to shift Rpi 3V signal to 5V. The program written below is for this solution.
Now I have debugged a little python program to toggle the relay which in turn drives a 12VDC motor and a 12VDC solenoid at the same time.
Next step is to check the level of back EMF, EMI which transmit back to the Rpi side and successfully isolated by the optocoupler, …
Back EMF Check of Four Inductive Loads (Motor, Solenoid, Water Pump, Solenoid Valve)
Optoisolated Back EMF Voltage
Input signal seems almost completely optoisolated. Next is to compare relay WITHOUT optoisolater.