last day (15 days later) » 

13:19
0
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.
use a DIFFERENT GPIO PIN whose default state is LOW rather than HIGH - e.g. BCM 18 - two pins down - if you use LOW for on for this pin, then it's probable that when you disconnect the power from the pi, the light will go on ... you're better off using HIGH = ON, and use a PIN other than any of those you see as V = 1 in your chart
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.
13:19
@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).
@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.
13:19
@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.
You appear to be another victim of the shoddy products for sale on the web. The relay is UNSUITABLE for the Pi. They can be only be used with additional circuitry, or modifying the module. See raspberrypi.stackexchange.com/a/100014/8697 The particular model you linked would not meet the standards for mains isolation.
@Milliways What should I use instead of a relay to get this done (turn on a light bulb programatically, but making sure it stays turned off if my program does not run)?
@IonicăBizău there is nothing wrong with using a relay - indeed this is one of the recommended methods. The problem is the poorly designed relay module which is unsuitable for the Pi - there are modules designed to be controlled by 3.3V logic levels, although they are swamped by the thousands of dodgy modules on ebay.

last day (15 days later) »