Driving a 5V relay board with ESP32 : not working

Hello everyone,
it’s my first post here !
This issue is driving me crazy, since I’ve re-read many posts about how to drive a 5V relay board with my ESP32, and it looks like I’m doing things the right way (theoretically)…but the setup is still not working ! and is producing a really strange behaviour.

I’m powering up my ESP32 through its 5V and ground pins, thanks to an external 5V power supply delivering 350 mA (an old Nokia transformer).
The 5V relay board is supplied with the same source, through its VCC and Ground pins. As a consequence, the relay board and the ESP32 are sharing a common ground.
The relay I’ve bought is this one. The input is supposed to be triggered with 4 mA “only”, which shouldn’t be an issue for the ESP32.
I’ve no information on the voltage level…but obviously, the relay board looks like any other board used in all the tutorials I’ve seen so far (so the 3.3V from the GPIO pins should be able to switch the relay board input to HIGH value…)

the GPIO 14 is directly plugged on the relay board INPUT pin. My program is OK (I’ve tested the GPIO pin : it’s HIGH with 3V when it is supposed to be high, and 0V when it is supposed to be low).

The relay is not doing anything but switching HIGH from the beginning (even if my program is setuping the relay LOW from the beginning).
Whatever is the GPIO pin status (HIGH or LOW), the relay remains HIGH. Again, I’ve measured the voltage from the GPIO pin : I really have 3V or 0V…with no effect on the relay.

and what is even stranger : when I manually unplug the GPIO pin, the relay switch off (LOW).
And if I connect the relay board’s INPUT pin to the ESP Ground, it switch back to HIGH ! Whatever ESP pin I connect to the relay board’s INPUT pin, the relay is switching to HIGH. And as soon as I disconnect the INPUT pin, the relay is switching off (LOW).
How is it possible that connecting the INPUT to the GROUND is considered as a signal ?

Anybody has an idea what is going on, and how to correct this ?
Thanks a lot,

Thats not the correct to connect that. That relay board is 5v and your gpio pins are 3,3v as they are referenced from from the 3,3 of the ESP.
In short you’ll get either 3,3 or 2 volts at the relay input.
You will need either for example npn transistors or a different relayboard.

Its not about the mA but the relay checks the 5V to the inputs. Anything 2V+ will trigger the relay and since it will never dip below that it will stay activated

Edit: how have you connected the relay module exactly?

Is this a board with 1 relay and a red, green LED?
I guess I know what’s wrong.
Post your yaml code in esp32.

Answer to Dujith : I have connected the relay to the ESP exactly this way :
link here except that I have a one-way relay (and it is a 2 ways here, but doesn’t change anything, right ?)
What you say does make no sense to me : when I connect the ground (0V) or the 3V3 pin or the GPIO pin (alternatively beeing 0V or 3V3 thanks to the program) to the relay’s INPUT pin, the relay is triggered (activated, HIGH) and ONLY when i disconnect the INPUT (nothing connected on it), it deactivates.
So it is not only 2V+ as you’ve said that trigger the relay, but anything between 0V and 5V ! only beeing not connected to anything un-activated the relay…

answer to pepe59 :
yes, i guess…i have 2 big LED, one green (that shows the board is powered up) one red (that shows if the relay is activated or not).
Here is a picture of the relay I’ve just taken :

My code is not done with YAML but with C (Arduino IDE). Here it is :


// Digital GPIO where the switch/relai is connected to
const int relai = 14;          
const long checkFreq = 10000;
unsigned long lastCheck = 0;
boolean statut_high;

void setup() {
  
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  
  pinMode(relai,OUTPUT);
  digitalWrite(relai, LOW); 
}

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - lastCheck >= checkFreq) {
    if (statut_high == false) {
      Serial.println("activation du relai");
      digitalWrite(relai, HIGH);
      statut_high = true;
    } else {
      Serial.println("repos du relai");
      digitalWrite(relai, LOW);
      statut_high = false;
    }
    lastCheck = currentMillis;
  }
}

Could not find the specs for that relay, but its as you say for this one:
0V at the Input is Activated, +V at the PIN is Deacivated. Problem is that it wont deactivate as its looking at the +5V compared to the +3,3V. So there is still a bit of Voltage difference and it will stay activated. Only when there is no difference (or little) in voltage will it deactivate.

You should measure the input to the +5V and see what voltage you have when its connected like you posted.

Also i think GPIO 14 outputs a PWM signal at boot, might cause weird issues in the future.

but then, why the relay deactivates when I unplug the INPUT pin ? it should be similar to 0V signal, and activate itself as if I’d have connected the ground, shouldn’t it ?

What GPIO are you suggesting to avoid troubles due to the PWM signal ?

No, You are pulling the input down to whatever you apply. The input is 5V in rest and you pull it down to 0V to activate it, thing is that it has a range and will trigger way before it hits 0V
Basically its looking if the input is NOT 5V
image

What GPIO are you suggesting to avoid troubles due to the PWM signal ?

Depends on your ESP32 what you have available, can u link the model?

This relay switches against GND.
I could probably remove it in the ESPHome yaml.
I don’t know Arduino code and can’t help with that.

answer to Pepe59 : thanks for your help.
answer to Dujith : same thanks ! :wink:
I have an ESP32 WROOM 32U.

OK, it makes sense. So I have to drive my relay through a transistor then…is that right ?

GPIO18 should be fine
As for the NPN:

The resistor isnt that important value wise. 1k should do fine too, its there for protection.
Output high on the GPIO will open the gate and the input will be pulled down to ground Relay goes on, GPIO low and the input will stay +5V Relay goes off.

1 Like

Thank you !
I need to buy a transistor and I’ll test this schema as soon as possible !

Ok, I’ve received my transistors and did the changes accordingly.
It’s almost working but I still have an issue, that seems really strange to me.

When I power up the ESP32 through a USB cable connected to my computer to read the console output and check that everything is ok : it works as expected ! Good news ! I still have to connect the relay’s ground to ESP32’s ground to have a common ground (it doesn’t work if I do not so). The relay is powered up thanks to an external source supplying 5V. Expected behavior is that the relay is not activated at setup. And it’s works fine. When the signal that is supposed to activate the relay is triggered, the relay activates as expected, that is ok.

When I power up the ESP32 with an independent power source, the same that I use for the relay : it no longer works :flushed:
From the beginning, the relay activates, although the signal that normally activates it is not triggered.

The only difference between previous working scheme and this one is that the 5V pin of the ESP32 is connected to the external 5V power supply of the relay and that the USB cable is no longer connected…

Any idea what could be the issue ?!
Thanks !

Since I cannot check through the serial monitor what’s going on when I’m powering up the ESP32 through the 5V pin, I’ve tested the setup procedure by activating and deactivating the relay many times, to check if the setup procedure was running well.

It is not !

When powered up through the USB cable, it is running correctly.

I know that I can power the ESP32 directly through the 5V pin : another project is running correctly this way.

Why in this project, the common power supply is screwing up everything ?
The current capacity of the power supply is more than 400mA (in case one may think it is not enough to supply both the relay and the ESP32)

For what it’s worth, I have a similar problem. A 5V low trigger relay that doesn’t work properly with my code. I’ll put the code at the bottom of the message. The relay is this .
However, on the seller’s website I saw that the relay must be activated with a voltage between 0 and 1.2V

BUT the relay works correctly with the code if it is connected to 3.3 V

I didn’t understand anything but I would like to know how to make the relay work correctly with 5v. Any help is welcome. Thank you

My code:

switch:
  - platform: gpio
    name: "Luce"
    id: rluce
    restore_mode: ALWAYS_OFF
    pin:
      number: 32
      inverted: true

I found the solution here. Now it’s work just fine on 5V.

Code:

switch:
  - platform: restart
    name: Restart device

  - platform: gpio
    name: "Luce"
    id: rluce
 
    pin:
      number: 32
      inverted: true
      mode: 
        output: true
        open_drain: True

Thanks for this - I had the same issue, could not get these relays to operate consistently, then found this relay that works natively on 3v inputs. Never had an issue since.

FWIW

hi! canyou help me? i’ve the same problem, but i i use this code i get this error:

switch.gpio: [source <unicode string>:324]
  platform: gpio
  pin: 
    pcf8574: pcf8574_hub2
    number: 7
    inverted: True
    mode: 
      output: True
      
      [open_drain] is an invalid option for [mode]. Please check the indentation.
      open_drain: True
  restore_mode: ALWAYS_OFF

What is “switch.gpio:” ? A custom switch ?

try:

switch:
  platform: gpio
  id: id1
  pin: 
    pcf8574: pcf8574_hub2
    number: 7
    inverted: True
    mode: 
      output: True
  restore_mode: ALWAYS_OFF

Instead of giving 5V to VCC pin of relay module, provide 3.3V.

Relay module Pin connections:
VCC=3.3V
GND=GND
IN=To any digital I/O pin of ESP32.

The problem exists only with ESP32.

I have tried this, and it is working perfectly.