Newbie help with Raspberry PI gpio switch

Hoping someone can help me get started with gpio switching.

I’ve got a 2 model B v1.1 board with the 40 pin connector. I’m trying to drive a simple arduino 5V relay module: enter link description here

I’ve physically tested the relay module separate to the Pi and connecting 5V to Vcc and Gnd to Gnd illuminates the green led only, and the relay remains unswitched in the NO position. Shorting In1 to 5V does nothing, but shorting In1 to Gnd illuminates the red led and the relay switches to C position. Technically the relay is a SPDT, but you get the idea. I think this is called “Active Low”

I’ve connected (relay module <-> Pi) Vcc <-> Pin 4 (5V), Gnd <-> Pin 6 (Gnd), In1 <-> Pin 12 (gpio 18)

The relevant section of my configuration.yaml is:

#Raspberry pi PIR sensors
binary_sensor:
  platform: rpi_gpio
  ports:
    11: PIR Kitchen
    12: PIR Front Door
    13: PIR Movie Room
    15: PIR Baby
  pull_mode: "UP"
  bouncetime: 50
  invert_logic: false

# Raspberry pi Relays
switch:
  platform: rpi_gpio
  ports:
    18: Front Lights
    38: Door Lights
    40: Alarm Siren
  invert_logic: false

Right now I’ve only got anything connected as above, but just in case my future setup is causing grief it’s in the config.

Behaviour is as follows:

  • HA service stopped: Green LED only, relay NO.
  • HA service started: After a few seconds, red LED lights, relay C. Front Lights switch in HA is off.
  • Click on Front Lights switch, activity light on Pi illuminates briefly, no change on relay.
  • Click off Front Lights switch, activity light on Pi illuminates briefly, no change on relay.
  • HA service stopped: Red turns off, green LED only, relay NO.

So, clearly HA is doing something with the gpio, but I can’t seem to control it in any way.

I’m sure it’s some sort of rookie mistake, but I’m stumped.

Any help would be appreciated.

Regards,

Mark

Hi!

These 5V arduino relays usually requires a logic level voltage close to 5V to switch. The RPi GPIOs use 3.3V logic. Check the datasheet of the relay board, and check the voltage on the relevant GPIO pin when switching states in home assistant.

Looks like you are using the physical pin numbers for the 40 pins, but I believe you should be using the Broadcom identifier numbers. So, where you have:

18: Front Lights 38: Door Lights 40: Alarm Siren

…you should have:

24: Front Lights 20: Door Lights 21: Alarm Siren

Because pins 18, 38 and 40 are known as GPIO24, GPIO20 and GPIO21 respectively.

I know this because I just went though this with my near identical setup.

1 Like

Thanks for the responses guys,

So it’s entirely possible that my relay module is not suitable and more likely that my understanding of how the gpio is supposed to work is wrong.

tonydew, you were initially right about the pins vs gpio, but I did figure that one out, and though I haven’t corrected the rest of the switches, I did correct the “Front Light” one which is my tester. I’m using pins 4 and 6 for Vcc and Gnd, and pin 12 (GPIO 18), so I think this at least is correct.

For the sake of info, I’ve made a table with the voltages across the three pins:

V x pins, HA off, Button Off, Button On
4 & 6, 5.08, 5.08, 5.08
4 & 12, 3.6, 5.08, 1.8
6 & 12, 0.64, 0, 3.29

Clearly HA is doing something, but my understanding of how it’s supposed to trigger the relay is wrong.

From the table, I can see what you are saying martinhjelmare about the 3.3v logic, which is probably not enough to trigger the relay, but I can’t understand why the button off (with HA on) gives 5v across 4 & 12, which is why the relay is on in this condition as well.

Also why is there a voltage with HA off, and how can I make sure that none of my relay’s trigger if the HA service stops for some reason?

I have to admit, I’m a bit confused about the logic behind the gpio setup. I’m more used to the old circuit closed energizes the magnet in the relay to trigger it, circuit open, relay returns to it’s default un-powered condition. This is clearly wrong because I have done a continuity test between all three pins in all states and there is none.

So… is there any way to get these relay modules to work (I have a few) or do I need new ones?

I also have a few 12v relay module for arduino which is marked as high level trigger Relay module link. I was thinking that these might be better as my relay is about 10M from the Pi and I’m using Cat 5 cable between them. The voltage drop across 10M would probably cause problems anyway.

The question is, how are these connected to the Pi? I assume that you would use a seperate 12V power source for the relay, and pin 12 for the In on the relay, but what about Gnd? Do I need to connect pin 6 (Gnd on Pi) to the Gnd on the 12V power supply, or is the circuit completed somewhere else. I’m reluctant to just give it a try as I’ve read the Pi’s are not particularly well isolated and many have died from a bit of electrical tinkering!

Thanks so much for your help. Once I get my head around it I should be right.

From your first post I gathered that the red light comes on when logic is LOW, ie 0V. That’s why it’s on when the HA switch is off. I think you can invert this UI behavior by changing invert logic to true in the config.

Also since the relay is a SPDT, you can choose how you connect it to the device, normally open or normally closed.

Regarding your measurements, I think you were feeding 5V to pin 12, that’s why it read 5V when switch was off. Also, I think you can damage the circuitry if feeding pins the wrong voltage, so be careful. You should connect one side to ground when measuring a GPIO.

To clarify, the relay requires 5v continuous on Vcc and Gnd before it will detect voltage on the In1, which triggers the switching. Connecting 5v to Vcc and Gnd will only turn on the red LED, which indicates the relay module is powered, but won’t do any switching.

The measurements were done without the relay connected, and straight off the pins with a voltmeter, so I don’t think there’s any way I was feeding 5V to pin12. I was measuring what the Pi outputs in the different conditions.

I’m not sure exactly what is required at In1 in order to trigger the relay. I will need to test this (not using the Pi) to see what is the minimum voltage required to trigger the relay. I suspect it will be higher than 3.3v.

However…

I did some testing of the 12V relay module, which acts in a similar fashion. Ie 12v to Vcc and Gnd to power the module (green LED this time) and anything above about 0.8V on In1 to trigger the relay.

So by using an outside 12v power supply, with the +ve from the power supply connected to Vcc on the relay module and -ve to Gnd on the relay module and looped back to Gnd (pin 6) on the Pi, and pin 12 (gpio 18) connected to In1, HA can successfully control the relay as expected. Yay!

According to what little I could find out about the relay board, it is isolated from the Pi by way of a transistor, a few resistors and a diode, so it should be safe (hopefully!)

Sorry, I misunderstood your table. You were doing nodal analysis, I see that now.
The voltages in the table are the voltage difference between the points you measured. First row is diff between 5V and GND, which is always 5V, Second row is diff between 5V and pin 12 and depends on the state of pin 12. When pin is LOW or 0V, ie button off, diff is 5V. When pin is HIGH or 3.3V, diff is 1.8V. When HA is off, the pin is probably floating. The third row shows the diff between GND and pin 12, depends on the state, and is either 0V or 3.3 V or floating.

Yes, I know that the relay requires a separate powersource (5v and GND). What’s the difference between green and red light on the relay? From your description in the first post it seems the relay closes the circuit, switches to C, when powered and In1 is LOW, ie when HA is started and switch is off. So to switch the relay to NO, you need to change In1 to HIGH. But the relay will probably need a HIGH closer to 5V than 3.3.

Good that you got the 12V relay working,

Ok, finally figured out the 5V relay module. 5v continuous to Vcc and Gnd to power the module and illuminates the green LED. and a simple short of In1 to Gnd to trigger the relay and illuminate the red LED. From the datasheet, the relay itself has a nominal current of 71mA.

So it looks like the simple fix is a transistor, with a resistor and diode for protection:

Unless there is some easier solution?