ESP32 stucks when switching on 230V fan via relay module


I'm building environment control box. One of the functions of this box is to control humidity and temperature using exhaust fan. I'm using ESP32-WROOM-32D devkit and 5v 6-way opto-isolated relay module:

Schematics are relatively simple:

i2c bus is used for RTC and sensors, which are powered by 3.3V voltage regulator. All circuit is powered by 5V meanwell PSU. ESP32 gets it's 3.3V from devkit's voltage regulator (later i'm going to use standalone ESP32 module and power it from voltage reg.).

The problem is that when controller switches on fan, it occasionally resets or even stucks. Watchdog can not handle that. Output on serial monitor is unreadable. If i make it turnining on and off every 5-10 secods, it happens very soon. If it's once in few minutes, it happens once a day or two or even week. When i disconnect fan from relay, problem goes away.

So it's definetely fan. And it seems that it's sort of interference going to MCU.

So my question is how to protect my circuit from that? I've already seen recomendation to use separate PSU, but relay module uses common ground for optocouplers and relay so it seems that cirucuit won't be 100% isolated.

I would highly appreciate any advise on this issue. I'm relatively new to electronics, so probably, the answer is somewhere nearby :slight_smile:

Thank you!

Many relay modules are set up to provide 5V to the optos and don't need ground - you turn on a relay by pulling its pin low. Then the relay coils can be powered separately. Which relay board do you have?

If your project turns on inductive devices such eletrical as motors,
the use of snubbers is strongly recommended.

Thank you for advise. Should it be enough to put varistor on relay contacts?

It's relay module from aliexpress:

I put HIGH on relay input to activate.

Post a few images of your wiring. I'd like to see how the 5V relay module is being powered by a 3.3V ESP32.

Blue and green wires are ground and 5v. They come directly from 5v psu.

The green wire is 5V to one side of the optoisolators and the signal from the ESP32 drives the other side of the optoisolator. I take it the green wire is the power for the optoisolators input side and that's 5V. The ESP32 is a 3.3V device. That is, to me, a not good thing do.

Got a datasheet for the relay board?

No schematics, but i've checked with tester. There's a 2.2k resistance between ground and cathode. And clearly seen 2.2k resistor on board. There's also zero resistance between anode and control pin (3.3v coming from ESP32 - white wires on photo). Between 5v and optocoupler there's nothing on control side. So ESP drives control side of optoisolator with 3.3 and current goes through it and 2.2k res to common ground.

But the jumpers are in the "High level trigger" mode, so the optoisolator input side references to ground, not 5 V.

Even if it was referenced to 5 V, the voltage drop of the isolator LED would put no more than 3.8 V on the ESP. On the common "Low level trigger" modules, there is a green LED in series with the opto-isolator so you must connect "VCC" to 5 V as it will not work with 3.3 V.

I use 3.3V to ignite the optoisolator input LED's, the JD-VCC jumper is removed, I put 3.3V on pin 2, and 5V on JD-VCC.

I don't think that board has a JD-VCC jumper.

My opinion is that the issue is that the 5V relay board will not work with the 3.3V MCU. Good luck.

But control circuit is isolated from relay circuit. Only GND is common.

I don't think that board has a JD-VCC jumper.

It does not. It has jumper that switches (as described in manual) control voltage from low range (up to 1.8v) to high range (3 to 5v). As I understood from testing, it actually connects opto to 5v instead of ground.

I've made some tests today. Used varistor and snubber made of X2 1uF cap and 60 ohm resistor. Seems, that snubber at least partly solved the issue. I still have to make some tests during the next days.

My question now is how to properly calculate resistor value, power and capacitor's capacitance? Expected load is up to 10A and 230V.