Relays change state on boot and restart - how to fix?

Hi there,
I’m quite new to Home Assistant and home automation stuff.
I’ve installed Home Assistant on Raspberry Pi 3 and connected 8 low-level trigger 5v relays module, similar to this:
8-channel-5v-relay-module-for-arduino-500x500

I’m able to control relays using HA, but there is one problem.
When I power up my raspberry or restart HA relays are “flickering”.

I couldn’t find anything here, maybe because many users use ESP8266 via MQTT to control relays.

I’m wondering if it is possible to setup initial GPIO pin state, so my 8 relays won’t flicker.

On raspberry forum I found this thread, new firmware for raspberry has new command “gpio” that allows to setup gpios.
Is this possible to setup using hassio? (I’ve setup my Home Assistant using Getting Started page)

Select the GPIOs you use carefully. The “pull” columns in this diagram shows what state the GPIO wlill be on boot (before the port is initialised),

down = low
up = high.

Click to enlarge.

Also read Note 2. A couple of the pins have permanent pull up resistors.

thank You for reply. Now I’m even more confused than before :confused:
could You advice me which one are “safe” to use? As I wrote before I need 8 pins for 8 relays.
I have active low relays, so pins should be pull up so relays won’t flicker. Am I right on that?
GPIO7, GPIO8 are ok? Problem is that there aren’t many GPIO’s that are pull up.
Any alternatives?

If your relays are active low then yes you want the pull up GPIOs. Of which there are only 7:

GPIO2, 3, 4, 5, 6, 7,8.

So one of your relays may flicker on start up becuase you have to use a pull down GPIO.

You could use logic inverters (not gates, 1 in = 0 out, and vice versa) to drive the relays, in which case you would use the the pull down outputs of which there are many more.

Or just use the logic inverter circuit for the one relay you need to use a pull down GPIO for.

logic-log47

Another option:

https://www.aliexpress.com/item/32828293291.html

Tie the 1A and 1B inputs together and the 1Y output will be an inverted logic of 1A and 1B.

Or just look for a relay board with active high inputs.

I just ordered active high relay modules. Sadly I didn’t know that before I ordered modules I already have.

@tom_l what about gpio config command? (https://www.raspberrypi.org/forums/viewtopic.php?f=117&t=208748) can I use it to reconfigure gpio’s? that would allow me to use those modules I currently have.

You might be able to adapt this method of altering the GPIO defaults:

I’ve enable onewiere using this guide: https://www.home-assistant.io/components/onewire/ I SSH to my pi and edited boot\config.txt, I’ll try this with GPIO’s, but I need to be sure I’m using correct ones.
Which one’s can be used?
I already use GPIO4 for onewire sensors, so I have 2,3,5,6,7,8 and I must add two more.
Can I use which GPIOS I want? Are there any pins that I shouldn’t reconfigure?

Yep. Any of them labelled GPIO.

The only pins you should not use are ID_SD or ID_SC (GPIO1 and GPIO0).

I’m going to try that out today.
Hopefully I won’t destory my PI (again :unamused:)
If everything works well I’ll try to add info to documentation, i think this is a really great features and not everyone knows about it.

1 Like

So if I want to setup GPIO12 and GPIO16 at input pull up I shoud use this command:

gpio=12,16=op,dh

Is that correct?

It looks correct from that document you linked to.

1 Like

Also newbie here and i found this video from @TheHookUp very informative to get me started on figuring our which GPIOs to use.

Whilst that is a very good video we are talking about raspberry pi GPIOs.

I use a script in init.d that sets the state of the gpio on boot to prevent this. You can adapt it from https://www.npmjs.com/package/homebridge-rasppi-gpio-garagedoor

It is a homebridge plugin page but the gpio settings configuration part (number 2 in the instructions on the link) is the same for anything.

Fair enough, thanks. I missed that in the OP :slight_smile:

1 Like

Hi,

I have used a RPI with those relay board on a few occasions. Most of the pins are suitable for controlling your relay board. The relays board most likely requires an active low to trigger, but that is easy to set in home assistant config and RPI pins are all Bi directional. And outputs don’t need pull ups or pull downs.
It would be done like this in configuration.yaml:

switch:
  - platform: rpi_gpio
    invert_logic: true
    ports: # Port numbers are the GPIO pin numbers, these pins number are all ok to use BTW
      5: Light One
      6: Light Two
      13: Light Three
      19: Light Four
      26: Light Five
      21: Light Six
      20: Light Seven
      16: Light Eight

When the RPI restarts the relays will turn on and off once, I couldn’t figure out a simple way to stop it, not in config anyway.
If your relays are chattering/flickering, even if they aren’t, (as they are designed for 5v) I recommend removing the LEDs on the relay board and bridging the gap with solder, as the RPI GPIO pins are 3.3v and the forward voltage of the LEDs is about 1.2v which leaves only barely enough voltage to power the optocoupler, and sometimes it can’t power it which causes chattering or not working at all randomly and as the optocoupler ages it gets worse. Remove the LEDs it will work ok.
And if you power the relays with the same power supply as the RPI make sure you have a 2.5A 5v or bigger. As the 8 x relays will use ~700mA together. It would be better to have a second power supply for the relays this would connect on the 3 pins to the right of the picture, remove jumper and connect a second 5v Supply to the left 2 pins of the three GND and VCC, you will still need VCC and GND from RPI as well at the other 10 pins.

And one important note, as far as I know till recently don’t use GPIO pins on the RPI with HASSIO as input(binary_sensors) it doesn’t work, it’s way to hit and miss, I think it has something to do with interrupts on the GPIO not being set up right, I don’t really know why but I know it’s not reliable.
The best way I have found is using ESPhome on HASSIO, an ESP32 or ESP8266 and an MCP23017 16 port expander and you can connect 16 relays like this one or input and the expander is 5v compatible and very easy to setup, pretty affordable.
Here one I used at I job - works really well

That is exactly what misiu is attempting to fix by adding the new Pi gpio command to the boot config (and using the correct GPIOs). As outlined in my post, most (except for seven of them) are high at boot.

Ok, now you say that it makes sense, what you had posted before. It did make me think about something though. I went and tried it out connected and active low to relay board to a RPI GPIO pin 26 and for some reason it didn’t turn off and on when it rebooted. I had a look around and I found that on boot the GPIOs are all in input mode, some comments based on the datasheet that pins 0-8 up pullup and the rest pull-down, while others disagreed with any pullup or down being applied that they and float of sort I assume, and randomly trigger the relays. Perhaps you could try an external pullup resistor from each output pin to 3.3v maybe from 20 - 50Kohm might stop it happening, if that worked it would be fair straight forward. I might try it tomorrow I would be interested to know the answer.

Indeed they are inputs at boot - you don’t use pull resistors on outputs (unless they are open drain). And I’ve always found the table above accurate for which are pull-up and which are pull-down at boot.

The internal resistors the pi uses are 50K minimum so to get less than 0.8v (V-in low) any value resistor below 16K to GND should do the trick.

There is also an option to modify relay module to be high level trigger. I just found this video: https://www.youtube.com/watch?v=-wiygjMviFo I didn’t tried it, but there is always an option.
The best option is to buy relay modules that have jumper to select between low and high level triggers