Uncategorized

Relay workaround notes

Asked 
Active today
Viewed 47 times
0

I 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 powered on, and obviously I can’t control it anymore.

After restarting the pi, the gpio readall outputs:

+-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
|   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  | <<<< Bulb Relay
|     |     |      0v |      |   |  9 || 10 | 0 | ALT0 | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
|   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
|   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
|   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
|  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
|  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
|  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
|     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+

So, even after the pi turns back on, the bulb remains turned on anyways until my script runs and when GPIO.setup(14, GPIO.OUT) is executed, the bulb turns off.

I would like to turn off the bulb if the pi goes down (it shuts down/disconnects from the internet – but that is another case I will treat in my script etc).

Should I use other pins for relays? Should I inverse the relay usage (LOW for ON and HIGH for OFF)?

I cannot figure this out and it is a little bit because sometimes the pi disconnects and I have to manually stop the electricity of the bulb.


By using BCM 8 (OUT, 1 by default, after restart), and using LOW output to turn on the relay, the bulb is turned off after restart, but obviously the issue is that as long the pi is completely disconnected from electricity, the relay close the circuit of the bulb.

In short I have the following cases/problems:

BCM USED   DEFAULT STATE  RELAY ON   PROBLEM
14         ALT0 1         HIGH       Bulb remains turned on after restart,
                                     until I set the STATE to OUT 0
8          OUT 1          LOW        Bulb turns on when pi is disconnected

Expected scenario: always turn off the bulb when the pi disconnects, turn on the bulb only from the script

share  edit  follow  close  flag
  • 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, … – tlfong01 22 hours ago   
  • You miay like to look at the real thing’s picture of my other answer below: raspberrypi.stackexchange.com/questions/113268/…. – tlfong01 22 hours ago    
  • 1
    @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. – Ionică Bizău 22 hours ago
  • 1
    @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. – Ionică Bizău 22 hours ago
  • 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. – tlfong01 22 hours ago   
  • 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. – tlfong01 22 hours ago   
  • 1
    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 – Jaromanda X 22 hours ago
  • 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. – tlfong01 21 hours ago   
  • 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 21 hours ago    
  • 1
    @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? – Ionică Bizău 17 hours ago
  • 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. – tlfong01 17 hours ago   
  • 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 17 hours ago    
  • 1
    @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. – Ionică Bizău 16 hours ago
  • 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. – tlfong01 16 hours ago   
  • For this High Trig KY019, if Rpi is disconnected, no source of current flows/drives SS8050, so switch is off. All is well. – tlfong01 16 hours ago   
  • 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 16 hours ago    
  • 1
    @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. – Ionică Bizău 16 hours ago
  • 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. – tlfong01 16 hours ago    
  • 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. – tlfong01 1 hour ago    
  • 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. – tlfong01 29 mins ago   
  • 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 16 secs ago    Edit   

2 Answers

1

Expected scenario: always turn off the bulb when the pi disconnects, turn on the bulb only from the script

NOTE: Please know this answer disregards all comments made to the OP’s question.


The issue you’re facing cannot be solved with software alone. You will need to add some hardware if you wish to maintain positive control of your relay at all times. As made clear in @Milliway’s answer, GPIO states are indeterminate when power is removed from the RPi – that is to say when power is removed from the RPi, the state (HIGH/LOW/OFF) of the GPIO pins is unknown/undefined. It follows then that you cannot maintain positive control of your relay if you allow GPIO pins with undefined states to control it. Once you understand that, you are on your way to a workable solution.

Based on your “Expected scenario”, there is a fairly simple hardware solution outlined in the schematic below. Simply described:

  • When the RPi is not powered up, this hardware will force the relay in the schematic to be in its Normally Open state.
  • When the RPi is powered up, the GPIO pin will take over control of the relay’s state.

In hardware-logic-speak this is known simply as a AND gate – its “logic equation” in this example is as follows:

IF (RPi IS POWEREDAND (GPIO IS HIGHTHEN (RELAY IS ENERGIZED)

Or, re-phrased to mimic a software idiom:

IF (RPi IS POWEREDAND (GPIO IS HIGHTHEN (RELAY IS ENERGIZED)
ELSE (RELAY IS NOT ENERGIZED)

Notes re Schematic:

  1. Due to the absence of any hardware specifications in the original question, the schematic shown here omits certain details required for implementation. If you need implementation details, please edit your question to include specifications for your relay (or a link to its spec sheet).
  2. R1 & C1 work to perform a power-on-initialization of one of the inputs to the AND gate. When power is applied to the RPi, the voltage across C1 will increase from 0 (GND potential) to 3.3V at a rate determined by the R-C time constant (tc = R1 * C1), eventually reaching the supply voltage (3.3V in this case).

    Consequently, choosing the values of R1 and C1 allow you to determine how long the output of the AND gate is forced low – and thereby inhibiting the GPIO output from controlling the realy.

  3. Also know that for situations requiring a very long initialization time, there are alternative methods and designs to accomplish this. Likewise, other features such as fail-safes, hardware interlocks, etc. may be accommodated.
  4. The warning we give to everyone who dabbles in circuits containing potentially lethal voltages applies for this project. A careless moment can change your life – so develop personal habits and safeguards, and please be careful.

schematic

simulate this circuit – Schematic created using CircuitLab

share  edit  follow  flag

4

You do not understand the way GPIO work.

You can control GPIO pins, setting High or Low, and the pins retain their state, even if the Pi is shutdown.

When you remove the Pi power the pins become undefined.

On re-applying power the GPIO pins are configured as INPUTS, with either a weak pullup or pulldown, which depends on the pin.

The pins ONLY become active outputs if configured by the user.

A well designed peripheral will not be activated by the weak pullup or pulldown.

The conventional solution, is to provide a sensible pullup or pulldown (4.7kΩ or less) to force the state, until the pin is configured as an Output.

share  edit  follow  flag
  • What do I need to provide that pullup/pulldown solution? I didn’t know that the pin state is preserved after shutdown. Maybe another solution is to cleanup the pins right before the shutdown. – Ionică Bizău 21 hours ago
  • If you want the state to change on shutdown it is your responsibility to implement this. Many circuits implement pull-ups to ensure a safe state. If they do not you can use a resistor, but it is not possible to specify without knowledge of the circuit. – Milliways 20 hours ago
  • Can you post an example I should follow to implement this? I do have a resistor I used to turn on an LED. – Ionică Bizău 17 hours ago
  • @IonicăBizău without details of the circuitry you are using it would be irresponsible to make circuit recommendations, – Milliways 6 hours ago
  • @IonicăBizău: I mostly agree with everything Milliways has said. However, I’ve made some assumptions, and have some ideas for you here. – Seamus 6 hours ago

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 )

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: