Assistance required to control a relay

I have the following code on my ESP32
switch:

switch:
  - platform: gpio
    pin: 2
    id: relay_1
    name: "relay 1"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay_1
    restore_mode: RESTORE_DEFAULT_OFF

this code is working on the ESP in home assistant. I can switch the switch on an off in home assistant
and i can see the ESP responding as it should. However the relay does nothing. Both the green and red light are on the relay, and if I unplug GPIO2 then the green light goes off, and back on when I plug it back in. i’m obviously missing something somewhere. Eventually I want this to control a 24vDC solenoid, hence the pulse. The relay is a SONGLE SRD-05VDC-SL-C
Thanks for your help.

wiring? ? ?

try invert the pin

Is it supposed to be operated with 5V?
What voltage are you operating it with? Just the GPIOs ~3.3V?

100% of all my (over a dozen) relays designed for “5V” work without any hiccups on the 3.3V GPIO logic on my ESP’s. The VCC of the relay obviously get’s 5V and the relay pin get’s reliable triggered over around ~2.5V (which makes them perfectly fit to work with ESP’s) :raised_hands:

1 Like

I’ve got one doing the same but wasn’t sure whether it is a sure thing so thought it was worth flagging.

1 Like

If it is a relay without an optoconverter and it is switched against GND, try this.

switch:
  - platform: gpio
    name: "relay 1"
    id: relay_1
    pin: 
      number: 2
      inverted: true
      mode: 
        output: True
        open_drain: True
    restore_mode: ALWAYS_OFF

5V power supply

Power to esp32 is via 5v to usb.
then ESP32 GND to Relay GND
ESP32VIN to relay VCC
ESP GPIO2 to relay IN

I’m thinking it may be voltage to the relay not being 5V, I’ll get a stepdown today and power the esp32/Relay from the 24V power supply for the soleniod (using the stepdown).

I think that VIN is for supplying power to the esp32 board, not for supplying power to other devices, but your board’s docos will confirm.

edit: Although on further research I may be wrong ESP32 VIN pin as 5V output - ESP32 Forum

But your board diagram will be definitive.

As I say

OK, So I have confirmed 5v to board and relay. When I turn the switch on and off (in home assistant) the blue light on the esp32 board responds but nothing on the relay. If i unplug the pin2 the relay activates and the blue light goes on, when i plug it back in the relay activates and blue light goes off. I’m stumpted as to how i get the switch to activate the relay.

We have no idea what the blue light means on your esp32, because you haven’t told us which dev board you use.

esp-wroom-32 CP2101 NodeMCU , my bad.

And how was your mileage with the inverted pin config @kenha

we do know because, pin 2 is universally used for the onboard led. There may be some odd ball dev board where this isn’t the case but, gpio 2 is almost always the onboard led.

have you confirmed this is a 5v relay? These come in different voltages like 12v and 24v and they look 100% identical. post a picture of your relay and how do you have the jumpers configured? Do you have the jumper set to High or Low. You usually can’t toggle a 5v relay with it set to High on the jumper, you need to set it to Low and then invert your gpio.

I believe with these ones it’s in the product name (as per op) and printed on it. Was curious so had a browse.

IMG_20230918_082934

image

ya, I didn’t see that OP had typed out the model of the relay. These relays look identical and it’s not hard to overlook the voltage and accidentally grab a 12v. The other reason I said he should post a picture is those Songle relays are usually on a module and not just the relay like that picture. Which module and what additional circuitry is useful information.

1 Like

disconnect the 5v to the relay and instead use a 3.3v output on the board. If it has a H-L jumper, move the jumper to L. Invert the GPIO in your code. Often 5v is too much voltage and the relay wont be able to fully turn ON/OFF with only a 3.3v gpio. I have a bunch of relays that do this and i have to go back and swap the 5v with 3.3v then Viola!

- platform: gpio
    name: 'Barn Siren'
    id: siren  
    pin: 
      number: 1
      mode:        
        output: true       
    inverted: true