Basic GPIO Output

Hi I seem to be having issues with a rather basic GPIO Output.

I am trying to trigger a 5v relay with a ESP32 GPIO.

My wiring is as follow.

ESP | Relay
5v | 5v
GPIO26 | GND

For some reason I cannot get the relay to switch. Using both a switch and output component in ESP home.

output:
  - platform: gpio
    pin: GPIO26
    id: 'generic_out'
switch:
  - platform: output
    name: "Generic Output"
    output: 'generic_out'

or

switch:
  - platform: gpio
    pin:
      number: 26
      inverted: false
      mode: output
    id: heaterpower
    internal: true

The ESP can only output 3.3V. A lot of relay boards that say 5V will also trigger on this, but definitely not all. You can verify the ESP is working properly by using a multimeter to confirm the GPIO is going high as well as try connecting the relay to the 3.3V pin of the ESP to see if that triggers it.

What relay are you using?
if you are using a module you should connect:
esp / module
5v - 5v
gnd / gnd
gpio / IN

It depends on which relay you use, if it is controlled via an optoconverter or if the switching is activated against GND.
Post the relay you are using

I’ve pulled the relay off a esp01S relay module.

I can get it to trigger by applying 5v to it,

So my logic here was to supply it with constant 5v,

Then just switch the GND to it with a GPIO

How do you “switch the ground” with a GPIO?

Pull the gpio low with the output component?

Wont that cause 5V to travel thru the ESP?

Then resolder it back to that module. Do you think they made the circuit next to yellow connector just for fun? Or because it’s needed to drive 5V relay safely with 3.3V microcontroller…

The relay has a coil, obviously. If I apply 5v and ground to the coil, the relay energizes.

I planned to supply the coil with 5V constantly, and then switch GND to the relay using a GPIO.

I could have sworn I had done this in the past.

That way, when ground is applied to the realy, the coil receives 5v.

1 Like

And so does Esp Gpio.

Where you think the current flows?
Relay coil is nominally drawing about 100mA. Esp Gpio can sink safely may be ~20mA.
So you are abusing your Gpio sinking too much current, but not only, you are sinking 5V. And esp Gpios are not 5V tolerant.
On the other hand, when you need to turn off your relay you write Esp gpio high. At this point relay coil has a voltage 1.7V (5-3.3V) and unknown behavior, may bee it’s not turning off, may be it’s flickering between…
Also, relay module has flyback diode to protect circuit from spike when coil is de-energized. Bare relay doesn’t.

2 Likes

You should not be using 5 volt relays in an ESP32 device. Only use relays compatible with 3.3 volts. In addition to that, you need to consider using a relay module. Otherwise you have to wire a flyback dial in the opposite direction of the current so that the ESP isn’t fried when the relay shuts off.

Reality is that most (all the dozens I own) relay (modules) designed for 5V do work perfectly fine with 3.3V too. :tada:

Why is that? Because the threshold voltage to toggle the relay (distinguish between high and low state) is usually around 2 - 2.5V. And that is perfectly fine to be toggled by 3.3V GPIO’s. :muscle:

  • So the GPIO state is low: ~0V → Relay is OFF (because lower then 2 - 2.5V)
  • Now when GPIO is high: ~3.3V → Relay will be ON (because it is higher then 2 - 2.5V)

So in general you can assume 5V relay do work perfectly fine on 3.3V GPIO’s :star2:

:100:

Bare relay? At around 100mA it’s definitely not fine!

1 Like

Yea, I mean I have it working on multiple setups with these. But there is a lot of electronics on this board so can’t say it’s a bare relay.

This board has an external power in of 5v then separate inputs for the gpio inputs.

I gave up trying on a plain GPIO and just used on of these boards.

1 Like

I always use modules. They don’t even cost more than bare relays :wink:

image

Last time I stock I payed: :money_with_wings:

  • $0.88 for :two:-channel relays (was a pack of five)
  • $1.32 for the :four:-channel type

Yes, but OP was using (erroneously) bare relay. And for that reason your post is quite confusing.

I think this is more of a lack of understanding regarding how GPIOs work, because in theory if I was able to switch a GPIO to GND, then what I was trying should have worked.

No really. You can “switch” Esp gpio to gnd to sink a small current (~20ma). But not to sink 100mA and not to sink 5V in any case.
If you look at the module from where you pulled the relay out, you will see transistor, optocoupler, flyback diode, some resistors etc.
Believe me, those chinese module manufactures didn’t put even one single resistor on that module if it wasn’t necessary.