last day (17 days later) » 

8:30 AM
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:….
@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:
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():

# *** Set Gpio pin as output ***
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? 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:…. 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
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.

  last day (17 days later) »