Controlling a 5V relay module with an ESP32

Hi,

I have a small circuit which turns a 220VAC 85W pump on/off using a 5VDC relay module and Arduino. I decided to replace the Arduino with an ESP32 to get wifi capabilities but didn't realised that the ESP32 didn't support 5V!

After some research, the opinion was mixed - some say that a 5V relay won't work with the 3.3V from the ESP32 and you need a logic level shifter to up the voltage, and some tutorials directly connects the relay module to a GPIO pin on the ESP32 (and it seems to work).

What I'm worried about is that these tutorials don't consider the long term damage that connecting a 5V relay to a 3.3V ESP32 would have on the board.

Am I wrong to think so? What would be the simplest way for me to operate a 5VDC relay module with an ESP32?

Constraints:

Should not be a problem.

The relay board needs 5V power supply to actuate the relay coils, so make sure your 5V supply to the esp has enough current for the 4 relays also. Depending on the operation of your circuit, you may know that, for example, only 2 relays will ever be active at the same time, so the current requirement will be lower.

But the logic signal to actuate the coils does not need to be 5V. The relay modules contain an opto-isolator, so they are actuated by a small current rather than voltage. That current lights the IR LED inside the opto-isolator. The esp digital output pins should be able to source, or more likely, sink enough current to light those LEDs.

These relays are compatible with 3V3 logic of ESP32.
Relay is triggered by GND signal from GPIO that will connect at relay COM with NO pin. Otherwise when you put 3V3 to GPIO, it will connect COM with NC pin. Also you need to connect 5V from VIN of ESP32 to relay board for powering it.

1 Like

So if I understand this right, I should:

  • Supply 5V (with enough current) to the esp via USB
  • Connect the relay as usual through GPIO and Ground pin
  • Supply the relay module with 5V (through the Vin pin or externally?)

If the 5V I supply to thee relay comes from Vin pin, can the ground of the relay be connected to a ground pin on the esp or does it have to be external?

When powering through usb, it's best to avoid drawing more than 500mA, even if the PSU is rated higher. Other components and pbc tracks on the esp board may not be designed for more than 500mA, because that's the maximum that a usb 2.0 PC/laptop port provides.

So if powering through usb, you need to know how much current your relays could draw, plus the esp itself and any other components, and make sure that won't exceed 500mA. It would be wise to keep that current below 80% of that 500mA for safety.

If it could exceed 500mA, then it would be safer to use an external 5V PSU that does have plenty of current for everything. Connect that PSU to the Vin pin on the esp to power it, and to the relay board to power that also.

1 Like

OK, here is the circuit for each relay on the board.:

To control it with 3.3 V logic, you connect "VCC" to your 5 V supply - not 3.3 V. "IN0" to "IN3" go to the I/O pins and you write them LOW when you want to actuate the relay.

Power for the relays is supplied by a separate paired connection of "JC-VCC" and "GND" directly back to your 5 V supply. Four relays require a total of 360 mA from your supply.

If I understand this right:

  • I connect 5V external PSU to the esp via the Vin pin
  • From the Vin pin, branch off to VCC on the relay
  • IN0-3 goes to any GPIO pin on the esp.
  • And finally, connect JC-VCC and GND of the relay directly to the PSU

My apologies for the constant use of bullet points, I like lists :grin:

Hi,

List are good as it allows questions to be specifically made and answered.

  • I connect 5V external PSU to the esp via the Vin pin
    Yes.
  • From the Vin pin, branch off to VCC on the relay
    Yes.
  • IN0-3 goes to any GPIO pin on the esp.
    Yes. If the relay module will work with 3V3, try it, you won't damage anything.
  • And finally, connect JC-VCC and GND of the relay directly to the PSU
    Yes.

Tom... :smiley: :+1: :coffee: :australia:

does it absolutely have to me clicking power sucking mechanical relays? SSRs, which use MOSFETS rather than mechanical switches, work over a broad range of voltages; 3-32 volts

1 Like

The requirement was:

1 Like