5v relay with ESPHome and an ESP8266

Hi, I’m having some problems getting a 5v relay to drive a 12v mag lock.

Starting with the easy bit, I have a 12v power supply with +ve going into COM on the relay, and then NO to +ve on the mag lock. Then -ve straight from the supply to -ve on the mag lock.

On the other side, I had vcc connected to 5v, gnd to gnd, and IN to a few different GPIO pins 4/5/12/13/14. In ESPHome I’ve defined a basic switch for those respective GPIO pins.

When the power is connected to the esp8266, the light on the relay goes green, and the lock closes (relay activated).

The problem is no matter whether the GPIO pin is switched on or off, the relay stays powered. I noticed there’s a very small amount of voltage 100-150mV coming from the GPIO pin. If I completely disconnect the wire from the GPIO pin (so voltage drops to zero) then the light on the relay goes out, and the lock opens.

The same behaviour exists on all GPIO pins, all supply a very small current.

Have I wired these up correctly?
Should the GPIO pins be giving out a small voltage even when not pulled high/low?
Is there a way to work around this?

1 Like

Can you post the yaml config?

You don’t pull outputs high or low. Only inputs.

The high and low level voltage output ranges are shown here:

logic-family-voltage-table

Sounds like you are trying to drive a 5V relay from a 3.3V logic level which may not be enough.

It’s a very basic switch, my thinking was this would supply enough voltage to drive the relay, and then when switched off it would no longer provide enough voltage for the relay.

switch:
  - platform: gpio
    name: "Test1 Board Status Light"
    pin: 2    #D4
    inverted: True
  - platform: gpio
    name: "gpio4 switch"
    pin: 4    #D2
  - platform: gpio
    name: "gpio5 switch"
    pin: 5   #D1 
  - platform: gpio
    name: "gpio14 switch"
    pin: 14  #D5    
  - platform: gpio
    name: "gpio12 switch"
    pin: 12  #D6  
  - platform: gpio
    name: "gpio13 switch"
    pin: 13  #D7        

Apologies not too familiar with the terminologies.

As I say when the switch is “on” it outputs ~3.3v which appears to be enough to drive the relay - relay LED is lit and I can see the mag lock close.

The issue is when the switch is “off” it still outputs 0.1-0.2v, within that Out Low voltage range your diagram shows. Which doesn’t appear to ‘stop’ the relay - the only way I’m able to do this is by pulling out the wire which obviously then has 0v which switches off the relay.

I’ll see if I can put together a diagram if that helps.

Put a resistor, 1000 Ohm = 1KOhm across the relay to offset the 0.1-0.2v in open state. That should work. Better yet, what kind of relay are you using? I looks like the relay might be “latching” meaning it needs 0 V to open.

Hello everyone.
Excuse my English, it is made with google translator.

Could you use an arduino relay?
It has an optocoupler and it works almost certainly.

Wow! If you have an advanced relay module with opto-coupler input: DC+ and DC- get 5V from the RPI. Then IN goes to the output pin of the RPI. “IN” via (LTV 817) turns on the LED that isolates the input.
Note: this does not really isolate the relay 100%. The jumper has a setting to fix that. Check the docs.