I'm learning arduino and electronics. Please feel free to point out my mistakes.
I want to control a 220VAC 8W water pump (like this) through a 3.3V relay module (like this) from the GPIO of an ESP32 dev kit. The pump is plugged into a power outlet connected to the relay module (built according to this tutorial).
I can't stop the board from crashing and, after experimenting (see below) when the ESP2 is powered through a 5VDC phone charger. The crashes do not occur when it is powered through computer USB.
Version 1: Initially, I connected my relay to the ESP32 on all 3 connectors (VCC, IN and ground). The pump worked beautifully, but the ESP32 crashed (LED flashing fast and Serial monitor printing gibberish, until pressing rst buttong) after 2-5 triggers / within 1 hour and randomly. The board spcifically crashed upon the relay being turned off.
Version 2: I then added the highest resistor value I could, before the IN would no longer turn the relay on (Version 2). The ESP32 froze less often (maybe after 10-20 triggers / 2-4 hours roughly). Still no luck.
Version 3: Finally, and reading around, I connected my 1N4007 and a LED in parallel to the resistor (Version 3). The board still hangs as previously, but I can specifically see the LED being very dimly lit when the relay is on, and emitting a flash of brighter light when the relay is turned off. The crash occurs immediately after. The 1N40007 does not seem to do anything (tried both ways).
What voltage is the relay board rated at? How to connect the Diode place it back in its box and do not use it. You should connect directly to the relay module not add a resistor. Is the motor a brush type, if so it may be causing electrical noise that is crashing your system. Stray wiring etc acts as antennas which are bad. Keep the motor away from the ESP unit.
You should not use a 3volt relay powered from VCC of the ESP.
A 3volt relay draws more coil current than the ESP itself, and the 3.3volt regulator of the ESP is not designed for that.
Just use a 5volt relay board with opto coupler. They can be controlled with the 3.3volt logic of the ESP.
Power the module from the 5volt (USB) connection of the ESP.
The relay contacts might need a snubber circuit or TVS across.
Leo..
JCA34F:
Where is the 12V? The pump has 3 wires? Post a link to the pumps datasheet or seller's webpage.
You cannot use a diode on AC.
I originally built the rig a while ago and should have checked. The pump is actually connected to mains using a power outlet connected to the relay built like this. The pump and the power outlet both have 3 wires (neutral, live and ground). I understand that flyback diodes do not work on AC. However, I'm suspecting from my empirical experimentation that the issue is not on the pump side (as ESP32 doesn't freeze at all when powered through computer USB), but on the 3.3/ESP32 side of the relay. Does this hypothesis make sense at all ?
gilshultz:
What voltage is the relay board rated at? How to connect the Diode place it back in its box and do not use it. You should connect directly to the relay module not add a resistor. Is the motor a brush type, if so it may be causing electrical noise that is crashing your system. Stray wiring etc acts as antennas which are bad. Keep the motor away from the ESP unit.
The relay is 10A 250VAC 10A 125 VAC // 10A 30VDC 10A 28VDC // JQC3F-03VDC-C (like this). I don't know if the motor is a brush type or not (see picture if the inside below). The motor is in a (thick) plastic PVC box and I've had no crashes operating the same system with an arduino UNO, so I don't think it is from the eletrical noise.
Wawa:
You should not use a 3volt relay powered from VCC of the ESP.
A 3volt relay draws more coil current than the ESP itself, and the 3.3volt regulator of the ESP is not designed for that.
Just use a 5volt relay board with opto coupler. They can be controlled with the 3.3volt logic of the ESP.
Power the module from the 5volt (USB) connection of the ESP.
The relay contacts might need a snubber circuit or TVS across.
Leo..
I've seen a few tutorials with relays powered by the VCC of an arduino. Is the current issue specific to the ESP32 ? I read about the opto coupler solution to isolate the circuits but don't know which to order and try. Also, could you expand / point me to a schematic with the snubber circuit or TVS across ? I don't understand how they are meant to be connected.
This is OK: https://www.aliexpress.com/item/1005001567474787.html
It is a relay module, not simply a relay. You are triggering it (not powering it) with an ESP GPIO pin. The coil may be around 50 ohms resistance so the module itself should be powered from 5 volts.
I prefer a relay module with an opto coupler because these are guaranteed to be triggerable at 3.3 or 5.0volts. Some similar looking relay modules work only with 5 volt logic because they use a PNP transistor which needs > 4.3 volts to switch it off if the board is powered at 5.0volts.
In general, it is better to buy components where there is a schematic available, such as, for example : 2 Channel 5V Relay Module - Wiki
swiip:
I've seen a few tutorials with relays powered by the VCC of an arduino...
The ESP32 is not an Arduino.
An ESP32 is more power-hungry than an ESP8266 based board.
The extra ~80mA (when active) of a relay module could be too much for some USB chargers too.
A 3volt relay module (~150mA) draws certainly too much from 3.3volt VCC.
Leo..
6v6gt:
This is OK: https://www.aliexpress.com/item/1005001567474787.html
It is a relay module, not simply a relay. You are triggering it (not powering it) with an ESP GPIO pin. The coil may be around 50 ohms resistance so the module itself should be powered from 5 volts.
I prefer a relay module with an opto coupler because these are guaranteed to be triggerable at 3.3 or 5.0volts. Some similar looking relay modules work only with 5 volt logic because they use a PNP transistor which needs > 4.3 volts to switch it off if the board is powered at 5.0volts.
In general, it is better to buy components where there is a schematic available, such as, for example : 2 Channel 5V Relay Module - Wiki
Thanks. From your answer, I understand that it is fine to power the relay module with the ESP32 5V out, and to trigger it with the GPIO (3.3V), is that right ? Wawa seems to be saying otherwise. I now realize that the schematics could have helped but I can't find any for my module right now. Otherwise, I might as well get some new relay modules, which have opto couplers for certain just to exclude that factor.
swiip:
Thanks. From your answer, I understand that it is fine to power the relay module with the ESP32 5V out, and to trigger it with the GPIO (3.3V), is that right ? Wawa seems to be saying otherwise.
No, 6v6gt is right.
Just make sure the USB supply can provide the peak current for the ESP32 and the current for the relay coil.
5volt@~80mA is certainly easier than ~150mA from 3.3volt VCC.
Leo..
Thanks to both ! To test whether the crashes may be a power issue, I will power the relay separately (using my Arduino Uno). The IN will then be the only link between ESP32 and relay. If it no longer crashes, then that would be it. I'll let you know.
That sounds like a very electrically noisy 7 watt mains powered pump, indicating your problems are not only on the low voltage side of that relay module.
I guess those are equivalent and probably a better quality than the 10 cent Ebay item.
I'm not an expert here, but with the symptoms you have described, in you position I'd try it. Since the cost of the individual part is not so significant when you consider the despatch costs, go for the this one: P409EL474M275AH330 - RC Snubber, 470nF, 275VAC, 630VDC, 20%, KEMET
If anyone else contradicts this choice, listen to them instead of me.
So the problem is that the system crashes when powered through the USB connection by a "phone charger" but not from a PC USB port.
Now these two arguably should be equivalent, on in fact the "phone charger" should be capable of providing more current than the PC. Unless it is rated at less than 500 mA. What is the current rating of the phone charger?
The single bad blunder here is using a relay module designed for 3.3 V. This will overload the ESP32 regulator, at least under some circumstances.
You need to be using a 5 V relay module, powered by the 5 V connection, not the 3.3 V. You also need a 220 µF capacitor across the Vcc and ground pins of the module.
That the system crashes when powered through the USB connection by a "phone charger" but not from a PC USB port does suggest that switching the AC to the pump is not actually the significant problem. And an 220 V AC 8 W submersible pump does not have brushes nor generate interference.
This is the preferred module:
Aliexpress item
With the link removed, you connect "JD-VCC" and "Gnd" via a pair of wires running together, to 5 V and ground of your 5 V power supply, and "VCC" and "In" to the 5 V and output pin at your ESP32. The "Gnd" connection is for the relay power supply, not the Arduino or ESP.