1

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


The circuit is very simple: I have a one channel relay that has a switch that can reverse the relay action. This is another question I have asked to see how to use the relay without the pi: https://electronics.stackexchange.com/q/301932/85944

 +---<--5V Transformer--------------------220V SOCKET
 |                                           |   |
Pi ---- [5v] ----- [(DC+)         COM]-------/   \------[ LIGHT ]
   ---- [0v] ----- [(DC-)  RELAY   NC]------------------[ BULB  ]
   ---- [BCM14] -- [(IN )          NO]
  • Comments are not for extended discussion; this conversation has been moved to chat. – goldilocks Jun 12 at 13:19
  • @Ionică Bizău, Your wiring is plain WRONG! [Bulb] should go to [NO], Not [NC]! This is the root cause of all your troubles: ) – tlfong01 Jun 13 at 1:21
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.

| improve this answer | |
  • 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 Jun 11 at 9:11
  • 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 Jun 11 at 9:57
  • 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 Jun 11 at 13:05
  • @IonicăBizău without details of the circuitry you are using it would be irresponsible to make circuit recommendations, – Milliways Jun 12 at 0:06
  • @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 Jun 12 at 0:45
2

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 de-energized 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 POWERED) AND (GPIO IS HIGH) THEN (RELAY IS ENERGIZED)

Or, re-phrased to mimic a software idiom:

IF (RPi IS POWERED) AND (GPIO IS HIGH) THEN (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 incorporate safeguards in your work habits, and please be careful.


schematic

simulate this circuit – Schematic created using CircuitLab

EDIT: Additional details re. OP's edited question:

The relay vendor (eBay) did not provide a spec sheet. However the eBay seller's page has information that allows the design to be furthered.

Unfortunately, the eBay seller's information does not include some vital information:

  1. the supply current requirements to power the relay module.

  2. the relay configuration (i.e. SPDT, SPST, DPDT, etc)

We'll assume that the RPi's onboard 5V regulator will source enough current to power the relay module: REF1, REF2. We'll rely on this answer for the relay configuration.

OPINION: One should not have to rely upon these assumptions, especially given that this relay is being used to handle potentially lethal voltages. I agree with @TomCarpenter's comment: "The most important rule to follow is no datasheet = no sale"

All of that said - and assuming all assumptions hold up - the following schematic is capable of compensating for the indeterminate state of the GPIO pin during startup and power down. Here's a summary of its operation:

  1. When the RPi is powered down, its +5V supply will go to zero. As we are powering the relay module from the RPi's +5V supply, this will remove power from the relay module. When power is removed from the relay module, the relay contacts will be in their "de-energized" (aka "Normal") position.

  2. When the RPi is powered down, the voltage across C1 will begin to drop as it discharges through the resistor R1. This will take one of the inputs to the AND gate low, thereby making its output - and the relay's control input- zero/LOW. And so, even if the relay is powered from a source other than the RPi, this will cause the relay contacts to move to the "de-energized" position.

  3. When the RPi is powered up, the voltage across C1 will be zero, forcing the output of the AND gate - and the relay control input - to also be zero.

schematic

simulate this circuit

IN CLOSING:

One final caution in addition to all other warning here about lethal voltages: Before applying mains power (220VAC), test this circuit with safe voltages.

| improve this answer | |
  • Thank you very much for this answer. However, if you could recommend me what else I have to buy to complete the circuit that would be great. I added more details in the edited question. – Ionică Bizău Jun 12 at 7:12
  • @IonicăBizău: The rules here don't allow for "shopping" questions. Unfortunately, the relay you've chosen does not come with a spec sheet, but there is some info listed at the bottom of the page. One key item is this: It "apparently" has the UL Recognized Component Mark, but that's not mentioned on the seller's page. That is not a good thing **. He **should be able to supply you with the paperwork that proves the part is what is claimed. But I'll leave that to you to sort. – Seamus Jun 12 at 8:53
  • @IonicăBizău: I'll add another schematic to my answer when I've got some time - probably this weekend. As for the shopping, that's still up to you. I'll give you part numbers on the schematic - you might find them at a distributor - Mouser for example – Seamus Jun 12 at 8:58
  • @IonicăBizău: I've added the requested details. Let me know if you have questions. – Seamus Jun 14 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.