Uncategorized

JDVcc Relay Notes

Q: How to turn off relay when RPi shuts down

Ionică BizăuI have connected a relay to BCM 14 and by setting its output to HIGH I can turn it on, making a 220V bulb to turn on. I use for that the following python instructions: GPIO.setmode(GPIO.BCM) GPIO.setup(14, GPIO.OUT) GPIO.output(14, GPIO.HIGH) However, if I power off the pi, the bulb remains …

Hi @Ionică Bizău, your problem is interesting. There are a couple of possibilities, most likely one is explained below: (1) The wiring is simply wrong. The relay module has two parts (a) the control part which inputs a control signal from a Rpi GPIO pin. (b) The relay switch part which is the little blue thing with a switch inside which switches on off current. This current switch has three terminals (i) COM (Common), (ii) NO (Normally Open, not connected to COM), (iii) NC (Normally Closed, connected to COM). / to continue, …

You miay like to look at the real thing’s picture of my other answer below: raspberrypi.stackexchange.com/questions/113268/….

@tlfong01 I think the issue is that even after I shut down the pi, the relay still gets current from the pins. I tried using BCM 8, which has OUT 1 by default and use LOW to turn on the bulb, and it works in the sense that after the pi is restarted the bulb turns off, but after turning off the pi, the relay is still in the same position.

@tlfong01 That is an amazing answer, but my case is a little bit different. Is it possible to change the default state of the pins? I think an OUT 0 would help, meaning, that mode is set by default to OUT, but current is LOW.

Let us first make sure that you have wired the light bulb and relay correctly, before we move on to the Rpi GPIO. The correct wiring should be like this: (1) Mains Live goes to COM, (2) NO goes the one contact of light bulb, (3) Another contact of bulb goes to Neutral. Now let us consider following cases: (a) No 5V power to relay module case: relay switch is open, COM is not connected to NO (This is the spec of the relay module), therefore electrical current cannot go from COM to NO to bulb.

OK, if now you understand and agree on my correct method of wiring mains to relay switch and light bulb, we can move on to the Rpi GPIO stuff.
First we need to make sure what type of relays is yours. You might like to give us a link to your relay. We need to know this, because there are two basic types of relays, (1) LOW level triggered, (2) High level triggered. For a Low level triggered module, if Rpi GPIO pin signal is Low, then module is triggered, switch is on (COM connected to NO), and if GPIO pin is High, then switch is off (COM not connected to NO). Vice versa for High trig module.

Now let us consider the Rpi’s GPIO’s default condition. Almost always, (1) Whenever you power up an Rpi, all GPIO pins are set to INPUT mode, (2) Even after powering up, say you have set any GPIO pin to OUTPUT mode, so to control things, and as soon as you CLEAN UP GPIO. ALL GPIO pins no matter previously set to output or input mode, will be AUTOMATICALLY set back to the initial default mode, ie INPUT mode.

Now the fun part: if a GPIO pin is in Input mode (for whatever reason/cause), then the relay module just don’t care and “blindly” sees it as an “EQUIVALENT OUTPUT MODE GPIO PIN SET to HIGH LEVEL OUTPUT”. This nonsense is one of the many sorrows of Rpi newbies weeping in the dark. Anyway, time for me to jog and supper. See you late this evening or tomorrow.

@tlfong01 Sorry, from the last comment I understand that IN and OUT 1 are going to turn on the bulb. So, is it possible to configure an OUT 0 by default?

Ah, there seems some misunderstanding somewhere. Let us start from the sentence in you question, before the chart: “After restarting the Rpi, the command gpio readall shows the following chart… Now suppose we use BCM4, GPIO7 pin’s signal to control the relay module. At power up, this GPIO7 pin is in input mode, and voltage level is High. Now if your relay module is low trig, as in my answer, the relay switch is off, and light bulb is off. If you understand and agree what I have said so far, you can carry on my story, or you can comment or make counter suggestions.

Or if you like, you can use BCM8, CE0 to control the relay. Now at power up, CE0 is output mode, voltage High. So at power up, both relay and bulb are off. I know you might find it not reasonable or feel uncomfortable to let High signal to turn off something, and Low signal to turn on something. But that is what the EE guys like: Low to activate or trigger something. For the EE guys, turning on something using a High signal is bad manners (actually electrically inefficient, because of the nature of the optimally designed circuit).
8:30 AM

@tlfong01 Well, that is my question about: if I use BCM8, unless the pi is running and output is HIGH, the bulb will be on. The problem here is that if the pi is disconnected, the bulb will turn on. If I use BCM4, the bulb will turn on when pi is restarted and will remain on until I set the mode to OUT.

But in case you insist the “natural” way to use a High signal to turn on the relay and light bulb, and an Low signal to turn off relay and bulb. Then you can use a High trig relay KY019, shown in Appendix B of my answer. Now let us use GPIO7 as the control pin. At powering up, GPIO7 is input mode, voltage is High. But this High is a fake High, or “input mode High”, and is not an output logical signal to drive current into the base of the PNP BJT SS8050 to turn on the Songle switch. So again, all is well during start up.

For this High Trig KY019, if Rpi is disconnected, no source of current flows/drives SS8050, so switch is off. All is well.

In case you have not yet lost completely, let us go back to the the first relay, the Low Trig Opto JDVcc guy. Now if Rpi is disconnected, the relay’s IN terminal is dangling, or floating in the air. In other words, now the floating IN terminal can be in random, High state or Low state. Case 1: If High, then of course relay is off. Case 2: If Low, no problem either, because this is a “fake” Low, no current can be /sink pull out from optoisolator’s D1 LED. In other words, no problem both cases and you can sleep well. Ah, bed time! Call it a day. See you tomorrow. Cheers..

@tlfong01 Thank you very much for this information. What I still don’t understand is what I am trying to do is possible or I need more hardware to get this done.

But I have no idea at all what actually is your hardware. I vaguely remember that I did ask for a link of your relay. But have you given me already? I am hopping among a couple of forums, entertaining different questions at the same time, so I might have missed or forgot something critical. My apologies. Anyway, see you tomorrow.

It is OK if you bought your toy from eBay and could no longer find the link. I can go through various types one by one, if you find your toy matches a certain type, then you know how to set up or do any modification if necessary. Now next type is the High/Low Trig Select Jumper Type, as shown in Appendix C. If you find the H/L jumper in your module, congratulation! No hardware change is necessary, just select the High option to make your toy High Level Triggered, and Day is Done: youtube.com/watch?v=Y2jxjv0HkwM.

Now let me introduce the first type of relay which needs hardware modification to make it Rpi compatible. Please read Appendix D for the details.

Appendix D has a complimentary workaround for the Rpi High is not high enough problem, for a Low triggered buzzer/relay circuit. The workaround this time is to add an resistor in series (last time is in parallel), to increase resistance, therefore smaller current to deactivate/untrigger the relay/buzzer. To summarize, the general trick is to add a 4k7 resistor in series or in parallel, to the input resistor. I know it is complicated. So if you can give the link or show me a photo of your toy, I can specifically explain how to solve your problem. No hurry at all. Cheers.

@tlfong01 I added details about the circuit and relay.

Thank you for the link. Please see Appendix C and D for newbie’s guidance. Good Luck. Cheers.
8:30 AM

Sorry, I still fail to undertstand how the rezistor will help in this circuit.

Because the relay is either ON or OFF.
8:46 AM

No problem. Let me suggest some tests to verify my guesses a are correct or not, then we can dig dipper.
My answer suggest the OP to first do a manual test, then the python test.
For you, I suggest to jump straight to the python test. You can use the python test to toggle your relay, first in High trig mode. If you find High trig mode OK, we can move on to the fun part, the Low trig mode.

But if even the python code is not working for the High trig mode, then we can fall back to the manual mode.

Now le me read the python program to refresh my memory.

# relay toggle test 2019jan10 tlfong01 2019jan109hkt1625 ***
# Rpi3B+ stretch (Linux 4.14.34-v7+ arm) IDLE python 3.5.3 

# Program Description
# 1. Setup Rpi.GPIO BCM mode
# 2. Set GPIO11 as relayPin to control 5V relay KY019
# 3. Toggle 100 times relayPin at 1Hz

import RPi.GPIO as GPIO
from   time import sleep

# *** GPIO Functions ***

def setGpioMode():
    GPIO.setwarnings(False) 
    GPIO.setmode(GPIO.BCM)
    return

# *** Set Gpio pin as output ***

def setGpioPinOutput(gpioPin):
    GPIO.setup(gpioPin, GPIO.OUT)
    return

# *** Set Gpoio pin High/Low ***

def setGpioPinHigh(gpioPin):  
    GPIO.output(gpioPin, GPIO.HIGH)
    return

def setGpioPinLow(gpioPin): 
    GPIO.output(gpioPin, GPIO.LOW)
    return

# *** Toggle Gpio pin ***

def toggleGpioPin(gpioPin, highSecond, lowSecond, count): 
    for i in range(count):
        setGpioPinHigh(gpioPin)
        sleep(highSecond)
        setGpioPinLow(gpioPin)
        sleep(lowSecond)
    return

# *** Main ***

print('Begin togglePin(), ...')

relayPin   = 11
highSecond = 1
lowSecond  = 1
count      = 100

setGpioMode()
setGpioPinOutput(relayPin)

toggleGpioPin(relayPin, highSecond, lowSecond, count)

print('End   togglePin().')

# *** End ***

(see full text)

Should I configure the hardware to turn on the light when it is HIGH?

Ag, let me see. The toggle relay program is almost copy, paste, and run, except you need to change the following statement: relayPin = 11. You can use any GPIO pin you like.

I understand that, but I don’t understand what we expect.

That will simply toggle the pin.

For the hardware, I mean relay side, you only need to select H/L jumper to H to test the High trig mode, and that is all, no need to put any load on the switch. I mean, disconnect any mains wire from the switch.

Well, that works

What I wrote in the question is that the issue is that if I use HIGH to turn the bulb on, it will remain on after shutdown, and will turn off only when my script sets it back to LOW.
8:59 AM

Ah yes, you are are right. The toggle program just stupidly repeats GPIO pin High and Low 100 times (Of course you can set it to 10 times, if you like). I/we expect the following: if the High/Low signal turn on/off the Songle relay switch, then you should hear click, click, click or tic tok 100 times.

If I use LOW, if the pi gets disconnected, the bulb turns on

Oh my goodness, you are indeed an impatient guy. Is your in law pressing you hard? Let us go slowly, one step at a time. I usually say the following: “Eat the big elephant bite by bite, say, in 10 bites. BTW, I am impatient talking to impatient guys. Let me take a jogging/supper break and let you play with your toy, any way you like. See you late this evening or tomorrow. Good luck. Cheers.

Ah, I notice that @Seamus have arrived. Let us welcome him. Perhaps he is hopefully patient enough to talk to the impatient guy. So I will leave him to advise you slowly walking along the long and winding road. Or are you walking the last mile?

youtube.com/watch?v=fR4HjTH_fTM. Happy youtubing. Cheers.
9:37 AM

Just passing through – I got a notification & thought I’d drop in. Also, a question: “How much supply current does the relay module need when operated from a 5V supply?”

Seems I’m talking to myself now 🙂 ANyway – that information is needed to complete the design. If you don’t know what it is, can you make a measurement? The relay should be energized when the measurement is taken.

@Seamus Please see Appendix A, B of my answer referred to the OP above.

I don’t see that…

My answer referred to OP: raspberrypi.stackexchange.com/questions/113268/…. Actually Appendix A, B, C, D are all relevant. PS – Appendices are at the very end of the answer. Ah, I need to go. Chat later.
2 hours later…
12:10 PM

I am really sorry if I hurt you, I did not want to do that. What I wanted to point out is that I already have a script setting the output of the relay. The issue is not in the actual script logic but in the way I should use the pi and the other hardware stuff. I feel I miss some hardware to get this done, but I am not sure.
1:00 PM

user image
1:28 PM

@tlfong01 Sorry, but there is no neutral output. The outputs are for open/closed and common.
1:57 PM

@IonicăBizău I think you don’t understand my question because you don’t understand the operation of the relay switch with the three terminals COM, NO, and NC.
Newbies often got confused and wrongly use COM and NC. Actually you should use COM and NO, then your problem would disappear immediately.
That is why I need to confirm this wiring in the very beginning, before moving on to know your type of relay.

Now I still hope you can confirm, it would be nice with with a photo, how you connect COM and NO. Repeat – NC should NOT be used at all. No hurry to reply. See you tomorrow. Good night and cheers.

Ah, I forgot that you don’t understand the mains wiring. For 220/110VAC, there are three wires: Live in Brown, Neutral in Blue, and Earth in Green.
But in case you are not using AC, but DC 12V/24V, then never mind what I said above. Cheers.

====================================================

Asked 
Active today
Viewed 72 times
1

Just starting to explore python, Raspberry and a relay. I want to control my 2-channel relay with a button. This is my test code:

import RPi.GPIO as GPIO
import time
import requests
from datetime import datetime
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # button
GPIO.setup(22, GPIO.OUT)                    # IN1 on relay
GPIO.setup(23, GPIO.OUT)                # IN2 on relay

try:
    while True:
        input_state = GPIO.input(18)
        if input_state == False:
            print('Button push')
            GPIO.output(22, 1)
            GPIO.output(23, 1)
            sleep(0.5)
            GPIO.output(22, 0)
            GPIO.output(23, 0)
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt  
    GPIO.cleanup()

Now when I run the code, the relay is switched on and only powered off when I hit ctrl-c. The push of the button has no effect. Just the GPIO.setup seems to trigger the relay. I power the relay with 5V from the Raspberry (2B).

Probably some newbie coding failure. Any help is appreciated!

Regards

edit: This is the relay I’m trying to use. I bought some years ago, I think from eBay (not Amazon).

This is the circuit: Raspberry 2b and a relay

This is a picture of the relay: Relay

share  edit  follow  close  flag
 New contributor

2 Answers

1

Question

The OP’s has a relay module, but his python program is not working. How to fix it?


jdvcc relay


relay toggle


Answer

1. First thing first. Let us confirm that the following is the OP’s relay module.


relay 1

relay 2


2. Let us look at the relay’s schematic and how to do the wiring.

relay 3

relay 4


3. 3V3 Logical Level Signal Manual Testing (Without using Rpi)

a. Put Rpi aside. Do not use the Rpi’s 5V and 3V3 rail.

b. Remove JD-Vcc jumper.

c. Connect JD-Vcc terminal to 5V power (External power, not Rpi’s 5V rail)

d. Connect Vcc terminal to 3V3 power (External power, not Rpi’s 3V3 rail, can use 3V battery)

e. Use a jumper wire and connect one end to IN1 terminal.

f. By hand, connect/touch the other jumper end to ground to see if the LED and relay switch turn on/click.

g. Touch jumper wire to 3V/3V3 and see if LED and relay turn off,

h. Use a multimeter to measure the driver current, between IN1 and ground. The current should be less than 5mA.


4. 5V0 Logical Level Signal Manual Testing (Without using Rpi)

If you are using a logical level converter (for example NPN BJT 2N2222) to shift up Rpi GPIO pin’s 3V3 logical signal to 5V, then you don’t need to remove the JDVcc jumper, and no need to power the input circuit with 3V3 power, as shown below. The testing procedure is same as above, except you are now using 5V power and also 5V logic level signal to test the module.

relay test 5V


5. Python program to toggle relay

# relay toggle test 2019jan10 tlfong01 2019jan109hkt1625 ***
# Rpi3B+ stretch (Linux 4.14.34-v7+ arm) IDLE python 3.5.3 

# Program Description
# 1. Setup Rpi.GPIO BCM mode
# 2. Set GPIO11 as relayPin to control 5V relay KY019
# 3. Toggle 100 times relayPin at 1Hz

import RPi.GPIO as GPIO
from   time import sleep

# *** GPIO Functions ***

def setGpioMode():
    GPIO.setwarnings(False) 
    GPIO.setmode(GPIO.BCM)
    return

# *** Set Gpio pin as output ***

def setGpioPinOutput(gpioPin):
    GPIO.setup(gpioPin, GPIO.OUT)
    return

# *** Set Gpoio pin High/Low ***

def setGpioPinHigh(gpioPin):  
    GPIO.output(gpioPin, GPIO.HIGH)
    return

def setGpioPinLow(gpioPin): 
    GPIO.output(gpioPin, GPIO.LOW)
    return

# *** Toggle Gpio pin ***

def toggleGpioPin(gpioPin, highSecond, lowSecond, count): 
    for i in range(count):
        setGpioPinHigh(gpioPin)
        sleep(highSecond)
        setGpioPinLow(gpioPin)
        sleep(lowSecond)
    return

# *** Main ***

print('Begin togglePin(), ...')

relayPin   = 11
highSecond = 1
lowSecond  = 1
count      = 100

setGpioMode()
setGpioPinOutput(relayPin)

toggleGpioPin(relayPin, highSecond, lowSecond, count)

print('End   togglePin().')

# *** End ***

/ to continue, …


Relay Newbie FAQ

Q1. Why relay is always turned on?

Q2. Why relay is turned off only when there is the Ctrl-C interrupt?

Q3. Is there another way to turn off the relay beside the Ctrl-C interrupt?

Q3. What does the JDVcc jumper do?

Q4. Is the JDVcc jumper only for Rpi?

Q5. Is there any work around if no JDVcc jumper is available?

Q6. Is it safe to power Vcc and JDVcc with 5V, with the JDVcc jumper on?

Q7. Is the optical isolator really necessary?

Q8. What is the meaning of Total optical isolation?

Q8. Is it safe to let the relay module to pass 10A?

Q9. Is it safe to let the relay module to switch 250VAC?

Q10. How many types of relay modules are there?

Q11. Is there any relay selection guide for the relay newbies?

Q12. Is it OK to get a fake relay module from eBay?

/ to continue, …


References

(1) Amazon 5V Two 2 Channel Relay Module with optocoupler Compatible with Arduino PIC AVR DSP ARM by Atomic Market

/ to continue, …


Appendices

Appendix A – Songle 5V Relay Switch Characteristics


songle relay 1


relay hysteresis

Appendix B – Simple High Level Triggered Relay Module without JDVcc jumper and optical isolation


high trig relay


Appendix C – Relay with High / Low Level Triggered Select Jumper


High Low Trig Relay


eBay H/L relay example

The above eBay relay is an example of High/Low level trigger select relay.
The relay can be used for Rpi as High or Low trigger as summarized below:

(1) High trigger

No modification is necessary. Just jumper H and there you go.

(2) Low trigger

There is 50% chance that Low trigger won’t work, because Rpi’s High signal (about 3V) is not High enough (need 4.2V) to untrigger the relay. The workaround is simple: just add a series 4k7 to the IN terminal (See Appendix D’s discussion section). Rpi’s GPIO signal now goes to the other end of 4k7 not connected to IN.

Warning: eBay can be big fun: No guarantee no nothing would melt down or blow up. You burn Me no burn. Good luck.


Appendix D – How to modify an Arduino High Trigger relay module to make it Rpi compatible

modify arduino

Discussion – In the above High trigger application, Rpi High signal is not high enough to pass big enough current to drive the LED, and the workaround is to decrease the series resistance by adding another resistance in parallel.

In an complimentary case where a relay or buzzer is low triggered, the Rpi’s High is not high enough to deactivate/untrigger the LED or switching transister. Then the workaround is to add a series resistor to the input terminal, so that the even Rpi High is not high enough, input current is smaller now and so can deactivate/untrigger the LED/switching transistor. See the following Q&A for an example of active buzzer.

Active Piezo Buzzer Makes Sound On Both Rpi GPIO Low and High Level Signal


/ to continue, …


Figures

/ to continue, …


share  edit  delete  flag
1

You are another victim of the shoddy products for sale on the web.

This is not a coding problem – the relay is UNSUITABLE for the Pi.

They can be only be used with additional circuitry, or modifying the module.

See https://raspberrypi.stackexchange.com/a/100014/8697

The particular model you linked is even worse than most! It would not meet the standards for mains isolation.

share  edit  follow  flag
.END

Categories: Uncategorized

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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: