Issue with RPi GPIOs

Hey guys,

I am running a hass.io on my rpi3b+.

Aside from all the wireless/mqtt switches that are in my system, i also have a total of around 20 switches hardwired to the pi with 5v relay boards. (3x4relay and 1x8relay boards)

For ease of use and some sort of cable management i am using a gpio cable connected to a breadboard where all my cables connect. I am also supplying external 5v power to the breadboard through a branded phone charger.

I know my cable management is not good but im working on it.

Here is my problem:

I have noticed that when i try to turn on all or most of the switches, some specific ones fail to turn on.

example: If i turn on my garden floodlights when everything else is off, they work ok. if i try to turn them on when several other lights are on, they wont work. Same with two (of my 8) living room lights.

Here is a map of my gpio wiring in case someone has some idea. The blue/red/green tinted names are the relays, and the ones that fail the most are GPIO23/20/21 and occasionally GPIO27 and 17 (on the bcm numbering)

What is this behavior? some sort of power shortage?

The strange thing is that it’s not happening on any switch. Some switches always work, no matter what else is on, and those specific 4-5 gpios seem to always fail when others are on.

I mean i tried switching them all on one by one with different order, and it’s not the last ones that fail, its always those specific ones.

Also, i have found that if i turn them on on a specific order, i can manage to light everything up.

I did already seek advice at the rpi corner of stack exchange where i was advised to add the aforementioned 5v power on my relays, but that didnt make any difference. They did also advise me to get rid of the breadboard (see pic) but i have not found a good alternative way to wire them yet.

I also tried a different rpi, with the same results.

Any ideas or tips to troubleshoot would be greatly appreciated!

(also please let me know if another subreddit would be a more appropriate to post such issue)

Thank you in advance guys

PS: part of my switch.yaml:

  - platform: rpi_gpio
    ports:
      4: saloni_1
      3: saloni_2
...
      8: radiator_upstairs
      13: air_condition_upstairs


    invert_logic: true
      

Did you see my post on my custom GPIO breakout board: https://community.home-assistant.io/t/custom-raspi-gpio-interfcae-pcb-project/88269?u=tom_l :slight_smile:

I notice you have a PIR attached to PIN 28. It would be best to move this. PINS 27 and 28 can be used for internal functions on the Pi3.

The power available from the GPIO bus is limited to a maximum of 16mA per pin with the total current from all pins not exceeding 51mA.

How much current do your 5V relay boards require per input?

Also note that the output of the Pi is 3.3V not 5V. This coupled with the limited power available may be causing your relay boards to not switch reliably.

What’s the current rating of the power supply? I’d advise against using mobile phone chargers, even branded. Voltage regulation can be flaky on these even if they are higher current rated.

Have you tried monitoring the voltage when you fire your relays?

I’ve got a RPi running some LEDs for TV ambient lighting (Hyperion). I’ve got that powered by a 5v/10a supply and it’s absolutely rock steady.

Wow! now. that’s something you should commercialize.

My PIR is on pin 1, do you mean that?
I ve been actually meaning to move the pin 14 too, as they flash on every rpi boot :slight_smile:

Will look into that.
My relays are these (or similar) 4xBoard and 8xBoard and they have been working fine for a long time with 5v VCC and 3.3v signal input.

The relays work with 5v power and 3.3v signal, iirc (see links above)

I’m using the official power adapter for the rpi.

And an old iphone adapter for the 5v on the breadboard rated 5v1A

Should i try a more recent samsung smart charger rated 5v/2A and 9v/1.67A?

Fire all the relays and use a multimeter between 5v and gnd on the breadboard? right?

edits: got multiple replies in one post to keep it tidy :slight_smile:

That’s the Broadcom numbering but yes. I was using the physical pin numbering.
Using Broadcom PIN numbers 0 and 1, otherwise known as physical pins 27 & 28 could cause problems. Probably unrelated in this case but something to be aware of.

Use your multimeter to check the voltage between ground and a Pi pin that should be on but isn’t switching the associated relay. Let us know what you find.

Got it, totally forgot about the physical numbering… :slight_smile:
Will make sure to avoid those plus physical 8.

Will do that and report back. Am i checking for Amperage or Voltage (will check both but just for general knowledge)

Voltage.

Thanks for the link to the boards. One thing I missed was that they use inverted logic (0v = relay on).

So you are looking to make sure the Pi pin is a low voltage (close to 0v) when the relay should be on.

I’m beginning to suspect @andyjacobs is on the right track with the suggestion that not enough power is being supplied to the relay boards. You should also check the 5V supplied to the relay board when a lot of relays are on. Make sure it is not sagging a lot below 5V.

My reason is that the relays would all actually be on if if the Pi’s 3.3v was not enough to keep them off - now that I know it is inverted logic.

I didn’t explain that well. Put your meter across the 5V from the phone charger plug pack and try to turn all the relays on one by one, watch what happens to the 5V. How low does it go?

Hey, i still have not been able to measure as a buddy borrowed my multi-meter but ill be on it soon.

With regards to the inverted logic thingy:
The relays have NO and NC positions.
I have connected my wires on the NO so that when there’s no power to the RPi, everything is off.
When the RPi starts up (and before hassio loads) everything remains off.
When hassio loads (and the invert_logic: true is taken into account) everything remains off.
When i say everything off, i mean lights and relays, their indication led is off.
Now when i turn on the switches on hassio, indication leds and lights go on.

I dont know if my thoughts are understandable, but it seems to me like this is not true

if it were true, wouldn’t the lights be ON when the rpi was disconnected?

Not if the relay board inputs have pull-up resistors. So they float high with no connection.

The instructions are poorly translated but this is the bit that to me means 0v = relay on:

Edit: I had a closer look at the PCB in the photo. It’s not pull-up resistors. They’re using optocouplers on the inputs. A ground on one of the inputs allows current to flow through the optocoupler internal LED (the other side is connected through a current limiting resistor to 5V). No connection or High level = no current flow. So no internal LED light = the output phototransistor of the optocoupler and thus relay stay off.

This also means that you probably need to sink between 5-10 mA into a GPIO pin per relay input (enough to turn the optocoupler’s internal LED on and the LED in series with it showing the relay state). So you should be able to power 5 to 10 relays from the Pi’s GPIO - assuming that the Pi GPIO bus can sink as much as it can supply (max = 51mA). Which from what I can gather is actually a valid assumption (unlike most other line drivers which can sink more than they can supply). The Pi GPIO drivers are a stupid design.

If you tell me what is written on these two components (boxed in red) I can better estimate the actual sink current required per relay input:

Also what colour are the LEDs on the edge of the board that show if a relay is on?

Here are two accessible ones that i could grab a picture of, The first is of my single 8xboard and the second is of one of my 4xboards. I think the remaining 3 4xboards are identical to the second one.

All of them are red leds.

Lastly,

I tried to get a measurement of the 5v/gnd rails while gradually turning the relays on.
The voltage drops to ~3.5v very soon, like after powering 4-5 relays and no more relays will turn on. (normally arund 8-10 relays turn on before i have issues)
I am guessing that this is because of hooking up my crappy multimeter inside the circuit?

PS. thanks for sticking with me man :slight_smile:

Ok that’s pretty conclusive. The relay boards need 2mA per GPIO so you should be able to interface to up to 25 relays.

Your main 5V power supply is sagging under load. You need a better one. The relay coils are rated at 0.45W so about 90mA per relay at 5V. For 12 relays you need a power supply rated at just over 1 Amp. Go for a 5V 2A power supply to make certain you have enough power and so that you are not operating it at 100% load all the time.

1 Like

Thanks for clearing this up once and for all :slight_smile:
I did a work around for the time being, powered half with the external power supply and the other half with the 5v of the rpi.
Everything seems to be working fine now.

Could you point me to some grade A 5v power supply so that i know what I’m looking for (I’m guessing mobile chargers are out of the game here). Preferably something small and plugable to an outlet.

The official raspberry pi power su-ply would be suitable.

Get yourself one of these:

https://www.amazon.co.uk/gp/product/B01D8FLU98/ref=as_li_tl?ie=UTF8&tag=26100f-21&camp=1634&creative=6738&linkCode=as2&creativeASIN=B01D8FLU98&linkId=669deb28358c420bfcf4967fb4073a58

You won’t go far wrong with that

Looks good,
I have temporarily solved my issue by powering half the relays with my pi and the rest with my phone charger.
It all seems to be working good at the moment but I’ll keep the specs and upgrade to something Iike this when I upgrade the whole thing when I’m.back home.
Thanks!