5V Relay and ESPHome

I’m trying to follow a tutorial online for using a 5V Relay and the D1 mini to power a fireplace on. I can’t figure out why they’re using tons of code and MQTT when you can accomplish the same in ESPHome with minimal code.

That gets to my question here, why won’t my 5V relay flip on and of using this code found on the ESPHome website?
switch:

  - platform: gpio
    name: "fireplace_relay"
    pin: GPIO5

Is this all that’s need to get a relay to flip on and off, or is there additional lines needed?

Here is the tutorial with parts and wiring. Wifi Fireplace w/Wemos D1Mini/ESP8266 | Automated Home Party

Because it is 5 volt, the ESP is 3.3 volt.

You can probably use a opto coupler to get around this limitation.

Have the D1 trigger the opto coupler then pass 5 volt through the opto coupler to the relay.

Yep - you’re right - that’s about all you’d need.
Just be sure you get that relay, because it’s actually more than just a relay.
It’s got a driver transistor inside, among other things, that enable it to accept a logic-level input signal.

Powering an ordinary relay coil using a GPIO pin is likely to ruin the GPIO pin, if it works at all, because the GPIOs aren’t designed to supply the kind of current a relay coil needs (nor the backlash current when power is cut).

So, even though a plain old relay might be found for many fewer dollars, it’s crucial that you use one that has a logic-level input, like the one shown in that page you linked to.

As mentioned here, you need such a relayrele1

If you have an opto coupler then that will do fine, no need to buy another relay.

I’m using the 5V pin on the ESP d1 mini, not the 3.3V

Yes, that is the relay I’m using. It powers on (light on), but with my code above it doesn’t toggle on/off when I flip the switch in HA. I thought additional code might be needed but guess I have a different issue, perhaps the signal from the D1 pin isn’t sending the signal

This is my configuration that works well

switch:
  - platform: gpio
    pin: D1
    inverted: true
    name: "Relé 1"

5V power supply

Tried this, didn’t work. I put a multimeter on the D1 Mini, the 5v pin is getting about 4.57volts, and the D1 pin is getting about 2.2v and is toggling on and off as I toggle the switch, but the relay isn’t doing anything. Perhaps the relay is defective, I’ll order another one.

Before ordering another relay, I’d check that your power supply is up to the task.
Those voltages appear to be far below what they should be.
It is possible that your supply is too weak to support the load of both the ESP and the relay (when it’s activated). Try a more beefy power supply before replacing anything else.

This typically doesn’t cause any problems because most of this type of relays have specifications to something like this on the trigger pin:

0.0V - 2.5V relay will be off
2.5V - 5.0V relay will be on

So no problems expected if your esp “works” normal (delivering a 3.3V high signal on a gpio pin):

But that on the other hand doesn’t look good at all and looks like the cause of the problem.
What does your 3.3V pin deliver on your esp?

Did you try changing the power supply/wiring? Might also that your onboard ldo is failing (or is overloaded)?

Ugh I had the signal and 5v wire reversed on my relay. Works great now!

Hi,
I have the exact same relay and using the D1 Mini. If relay is connected to 5V the D1 output pin will always trigger relay. If I connect relay to 3.3V VCC, then it will work as normally with the exact same code as you use here. But when connecting VCC to 5V it will just trigger the relay all the time.
If I change the code to be pin D3 and put the wire there, then it will stay low and not trigger. But as soon as the code uploads it will be forced high, and inverting the pin als does nothing. Why is this?

I found a solution:
OK, now for that particular relay module, you need to connect its Vcc to 5 V (not 3.3), its “GND” to your WeMOS ground and its “in” to the anode of a LED whose cathode goes to the WeMOS output pin. Without the series LED the relay will not turn off .

It is not necessary to use an LED.
Try using this code

switch:
  - platform: gpio
    name: "Rele1"
    pin: 
      number: D5
      inverted: true
      mode: 
        output: True
        open_drain: True
    restore_mode: ALWAYS_OFF
4 Likes

Eficaz y simple!!. It’s a great solution.
Ahora otorgamos más de 4V. (4.37V aprox.)
It must be taken into account that it is only a solution to power certain devices: relays, motors, LEDs. But not switches since the board will not be able to absorb them.
info: Configuration Types — ESPHome

I’ve the same relais and i did not found a solution to turn off it. it stays always on.
I use esp32 wroom
Thanks

I just got one of these relays and had the same issue and found this thread. This config worked for me on a ESP32-WROOM-32U:

switch:
  - platform: gpio
    pin: 
      number: ${relay}
      inverted: true
      mode: 
        output: True
        open_drain: True
    name: Relay
    id: relay
    restore_mode: ALWAYS_OFF

So this was bugging me because I could see the green LED flickering and I could actually hear some noise coming from the relay when it was turned off. After some research I found this article.

Basically the relay power voltage and control voltage should be the same. So powering with 5V and sending a 3.3V control signal as the ESP32s do on GPIO is causing the issue where the relay doesn’t turn off. There are a couple of fixes according to the article but the simplist is to power the relay with the 3.3V pin instead of 5V. This configuration does draw a little more amps, but when powering this way you no longer need the “open_drain” option.

My noise and flickering are now gone and the relay works as expected.