Switch with delay and turn_off crashes ESP32

Hi all,
I made a tank sensor with an ESP32, a HC-SR04 ultrasonic sensor and dual relay board to drive a water pump. The idea is to measure when the tank is full and use the relay to drive a 230v water pump to empty the tank.
If I test runinng with a usb cable from the PC (so I can get logs too) everything run smoothly.
When I connect to the mains, once the switch turns off I loose connection to the ESP. Sometime it reboots, sometime it stay freezed and I need to power it down.
I use an old iPhone USB charger (1A) to power the ciruit and I measured it dreins about 0.26A with the two relays on.

Does anyone have any idea why this happens?

For reference, a piece of the yaml regarding the switch:

switch:
  - platform: restart
    name: "Restart"
    id: ${id_name}_device_restart
    disabled_by_default: True
    entity_category: "Diagnostic"
#
  - platform: gpio
    pin: GPIO16
    inverted: True
    id: ${id_name}_pump
    name: "Pump"
    icon: "mdi:water-pump"
    on_turn_on:
    - delay: 20s
    - switch.turn_off: ${id_name}_pump   

If you run it powered from charger without that pump actually connected on AC side, what’s behavior? You see relay LED working, Esp resetting or
?

Hi Kerosm,
I just tested. It seems everything is working properly of there is no load connect ed to the relay.
Phisically I have a wire coming from the socket that powershell the usb charger and I connect live and neutrale to the 2 relays.
I do not expect the Power from the water pump (it is an acquarium pump) to impact the stabilitĂ  of the usb charger.

Regards

It is possible that the pumps are creating a lot of electrical noise when switched.

Are the relays completely isolated from the pumps?

You may need to put a capacitors on the ESP board power supplies, 5V and 3V3. An electrolytic 10”F and an ceramic 0.1”F on each.

How much current does the pump draw? The relays I’ve used are rated at 10A. In the case of my 20A pump, I have the relay drive a high power relay.

I do pretty much the same thing but with an Arduino. I’ve had no problems.

Thx, I’ll try with the condensers.
The pump is an acquarium pump and from the spec it should absorb only 8W.

Regards

Why should you connect L and N? Feel free to post your circuit

Anyway, keep your low voltage wires short and far from pump wiring.
Rc-snubber across pump could also help .

If I understand, you switch L(ine) with one relay and N(eutral) with the other relay. Coming from the mains (in USA “panel”) N is connected the C(ommon) of one relay and the Normally Open (NO) of that relay is connected to the N of the pump. Similarly, L from mains to C of the other relay, NO of the other relay to L of the pump. Both relays are energized at the same time.

This wiring is not advisable because when the relays are not energized you would have mains voltage between the two Normally Closed (NC) connectors of the relays. A better way would be: the mains wires connected to NO and the wires of the pump connected to respective C relay connector.

Either way, switching the relays are almost certain to generate interference because there is what’s called bounce in any mechanical switch and that is doubled because the relays don’t contact at the same time. This would aggravate the inherent electrical noise of a motor starting.

In the USA we would only have one relay switching the L from the panel, the N going straight to the pump. I would like to believe that you can do that in Europe, too. This could reduce the electrical noise.

Let us know what works.

I believe the issue in Europe is that we don’t have a fixed wire for live.
We can put the plug in either way and that means either live or neutral is switched.
Obviously you can figure it out which is which but if someone carelessly unplugs it and puts it back then you might have a live wire where you don’t want.
I believe that is the reason for the double switching.

However
 am I dreaming there is such a thing as a double relay?

DPDT relay modules are more rare and thereon expensive. I don’t see difference in practice if you use common 2-ch relay module triggered from single pin.
Though I don’t see need to switch both line and neutral in the first place.
For example european certified smart plug just switches either or


True. But this could be a (more likely) a DIY solution where connection points might be exposed.
A smart plug is more contained and whatever the user plugs in to it is on them.
But I agree there shouldn’t be a difference.

1 Like

For the safety point of view yes!
For the problem with spikes when turning off a pump it doesn’t matter.

Hi all,
thx for all the comments. Originally I tought to use 2 relays exacly because I do not know where L is. I tried to use one relay only, but I get the same result.

Here is a picture of the whole cirtuit, I understant it is not so neat and clean:

The USB charger is on the left. On the bottom you’ll see 2 wago where I get mains, the plug for the USB charger and the wires that gets to the relay.

The led on the left (beside the USB charger) is a 3.3v to 5v converter.

The RJ45 is used to connect the external HC-SR04 ultrasonic sensor.

Regards

to find out which wire is neutral and live, you could measure the voltage between the wire and earth. if there is 230V, you’re on the live wire, if there’s nothing, you’re on neutral.

Could it be that the charger is not good enough?
It seems there is a few more components than just the ESP there.
Do you have another charger to test with? Beefier one perhaps?

According to the image, there is none.
But you can do the same with L and N.
Either it’s positive or negative.
But I believe the issue is not where the live is now. It’s where the live is next time you unplug and plug it back in.

I’m using a tool to find where live is, so I can plug it accordingly.

Regarding the USB charger, I did many test, but it is worth trying to change it once more.
Thx

A 1A charger is a really small device. I would first try a more powerful charger, such as a 2A charger, as it can definitely compensate for voltage fluctuations better. Next, I think OldSurferDude’s suggestion is a good one: an additional large capacitor (1000 uF or larger) on the relay/ESP power supply to provide more energy for the switching process, and a small capacitor to compensate for interference (220 nF debouncing).

I hope you don’t use the ESP board to power the relays (e.g., USB to the ESP32 development board and relay to the board’s 5V), but instead supply the 5V supply externally to the ESP board and the relay board. If multiple relays are powered by the ESP32 board, the ESP board could quickly become overloaded.

I use a RC snubber on the high-voltage side to prevent the relay from sticking (https://de.aliexpress.com/item/1005007054553588.html).