Uncategorized

Relay discussion notes

Asked 
Active today
Viewed 22 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 5 hours ago   
  • You miay like to look at the real thing’s picture of my other answer below: raspberrypi.stackexchange.com/questions/113268/…. – tlfong01 5 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 5 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 5 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 5 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 5 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 5 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 5 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 5 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 1 hour 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 41 mins 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 32 mins 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 17 mins 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 17 mins 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 14 mins 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 4 mins ago    Edit   

2

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 4 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 4 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 54 mins 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 )

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: