ESP32 reboots when turning off relay - why?

In my boat I have a bunch of octocupler relays that I always use, this type, with V5 trigger voltage:

The power to the relay are fed from the same power supply as the ESP32, this one from Hobbywing:

Then I use them to control more powerfull relays, from 30A and up to 300 A (for start from the secondary battery if the starter battery is flat). I switch the secondary relays with 12V-, not 12V+, because this is an aluminium boat, and I prefer not to have more 12V+ running around the boat then absolutely necessary. The 12V- is taken from the 12V side of the Hobbywing PSU, and the 12V+ into the relays is taken from the closest power source to whatever I’m controlling. So far so good.With most relays that’s not a problem at all. But some of them are giving me grief. For stuff that needs more power (mains power to the wheelhouse, power block for pulling pots and so on) I like to use this relay, which is reliable and doesn’t get too hot:

I build my stuff into waterproof boxes like this one and fasten them with a lot of hot glue:

But for some of them I have a strange problem. When I turn them off, the ESP32 restarts. It only happens when I turn off the relay, and it only happens on the larger relays, not the 30A. I have even tried to put an extra relay in between (so the ESP32 relay activates a 30A relay, which activates and deactivates the 200 A relay), but it doesn’t seem to help. What on earth can this be? I am afraid I’ll have to pull apart a lot of stuff to put the 200A relays a bit away from the boxes, but I would prefer to keep everything together since the 200A relays are a bit to exposed for my taste. A short circuit or even creepage in an aluminum boat can be disastrous.

It’s the actual turning off that messes up the stuff, it doesn’t matter if I do it from the internal webserver of the ESP, MQTT from Node-RED or the integration in Hass.

I am far from an expert in this ‘field’, but do you think the act of the magnet field dropping in the relay could have an effect on either the ESP32 or power supply chips for the ESP? The ESP’s do have a Hall Effect sensor built in I believe, might make sure it is disabled as an input.

Do relays have reverse polarised diode in parallel with coil?

1 Like

@dproffer Interesting! That I must find out, I’ll go and read a bit on the ESPHome page.

@IgorZg Sorry, I have no idea what you are saying… I know nothing about diodes, resistors and stuff like that.

Edit: I see when logging is enabled that the reason for restart is set as “Reset: Software Reset CPU”

Driving relays isn’t quite trivial. You need a back-emf protection diode across the coil, and independent (star) grounding for the relay and MCU, and decent decoupling capacitors and a psu which can source and sink the require current.

Even if all this is done, I’ve found the esp32 to be very sensitive to electromagnetic noise. (And breaking any significant current leads to an arc=EMI, although at least AC will blow that arc out in a half-cycle). You’re lucky yours is restarting. I had one (controlling a fridge) lock up randomly (despite all the things mentioned before), and I never did get to the bottom of why.

The fact that this is happening when the coil is de-energised points to back-emf or EMI. Scope probe on the power rails (close to the esp32) should tell you if anything is getting back.

Regardless, I would seriously re-consider the risks of controller and relay failure. MCUs can and do lock up for all sorts of reasons. If that 200A (!) relay is controlling anything which could do you harm if it did or didn’t come on, you need to do some failure mode analysis on it.

At the very least I’d want a hardware watchdog making sure the controller was online, and known failsafe startup conditions.

1 Like

Thanks! I’m afraid you are using many words I do not understand, but I do understand “sensitive to electromagnetic noise”. So my best bet would probably be to move the offending relays away from the ESP, then? Oh, and no, they do not drive anything that would be dangerous, I have overrides in physical switches. I also have a watchdog in the Pi that will alert me if the ESP doesn’t work.

Your relay driver board almost certainly has built in protection diodes. When a coil is de-energised a voltage is developed backwards across it. Without a filter of some sort this back-emf will play havock with the power supply. The usual ‘filter’ is just a diode backwards across the coil, which shorts out the back-emf, thus confining it to the coil. However the relay driver board needs separate and solid power/ground connections. The esp32 will not like its power rails being pulled around and switching coils is going to do so.

If proper grounding (separate power and ground wires (but ground matters more) for the esp and the relay driver board, only connection between them being the ‘relay on’ line with no ground return) doesn’t sort things out then yes, sadly, you’re best off with the relays and the mcu as far apart as possible.

Maybe a simple test, just get cut a piece of ductwork to make a wall between relay and esp, wrap to insulate.

To add some more reliability to your ESP32, you might have a look at this board. It has an external hardware watchdog timer. These guys do radiation work for a living. I have just started to explore this board so I do not have much more to say at this time. If nothing else, they might have some input on high energy environments and ESP32 if you reach out to them.

As an FYI, even though this maker is in Kazakhstan, my package arrived from Moscow. Wondering what KGB or J. Edgar lists I ‘lighted’ up on with this shipment :wink: .

Reset: Software Reset CPU - I am pretty sure that in moment of turning off this large relay, because lack of protective diode huge negative impuls is generated in inductor and propagated via power supply rails back to esp32. Then esp32 supply drops, inbuild brown out protection detect this and restart cpu. Put protective diode in parallel with relay coil.

1 Like

I can understand the EM explanation, I’m sure the coil on that relay is big enough to make some noise (sounds like 80’s rap lyrics). I have a sheet of 1 mm A4/316 stell, which I will try to form a barrier around the relay and see if that helps.

What I don’t understand is the explanation with a pulse from the large relay. I may have explained it poorly, but it’s like this:

ESP32 → relay board → 30A relay → 200A relay

So that means that the pulse would have to move over the closed 30A relay AND the closed relay on that relay board when the relay board closes the 30 A relay, which in it’s turn closes the 200A relay. The 200A and 30A relays both get 12V+ and - directly from the boat’s system, on the same ciruit as the Hobbywing PSU, yes. But there are several other ESP32’s on the same circuit, only a bit further away, so shouldn’t they have the same problem in that case? But of course that could just be me and my lack of understanding for electronics.

Perhaps a simple (too?) test, wire in an exact same ESP32 to power rail. Place it right next to existing ESP32, however hook it to nothing but power rail. Flash an LED on it in a loop. See if it reboots as well.

If you are inducing a spike or dip in the power rail of the ESP, this can happen without a physical connection thru the ‘magic’ EMF. Routing of wires can be a big factor.

I have seen ESP’s behave differently to voltage spikes / dips due to different wiring/devices connected to their IO pins.

Again, I am far from an expert here, however I am puzzled by the picture of your HobbyWing power supply, why do you have the ferrite choke on the (input or output, I am not sure which) line. I would think the purpose of a choke here would be to reduce the emission of RF noise by the power supply. If your speakers are buzzing when the power supply is on I get it. However, I wonder if this device could also be acting as a receiving ‘antenna’ to external RF, in this case the collapse of the magnetic field, and causing a spike or dip on power supply.

Another simple thing to do to your power cables is to make sure the positive and negative cables are carefully twisted together along all their runs. To take it further, put a copper shield jacket around these two wires and ground the shield.

1 Like

@dproffer The choke is for the PSU’s original use, in drones and other radio controlled devices. I remove them. As for the spike or dip, I don’t think there is none. If there was on the 12V side, I think the voltage sensor would pick, them up. And if it was so simple, it would probably happen every time. It doesn’t, I see now that I didn’t write that in the original message, sorry. I meant to. But it happens totally infrequenstly, I can klick the relay on and of five times without problems, and then it happens on the next few times, or it can happen more frequently. There is no logic at all to when it happens.

And about the power cables, I can’t imagine that it has anything to do with them, because if it was then it would happen when using the exact same setup, only without the 200A relay at the end. When I use only 30A relays, no matter what they are connected to, this never happens. With the 200A relay it happens even if the relay is not connected to anything. So my guess will still be that an EM pulse is hitting the ESP. I will try to find time to experiment with the thin sheet metal today or tomorrow, and I’ll post the results. :+1:

Problem solved. No doubt that it was the EM that did it, a piece of metal between the relay and the ESP made it all go away totally. Before I could turn off the relay max five times before the ESP rebooted. Now I stopped after 25, it didn’t seem to be any point going on. Thanks for the metal plate tip, @dproffer!

Good news! Glad you found a solution to your vexing problem. A good education for all of us.

Hoping you have fair winds, following seas and no magic smoke! :wink:

Thank you, sir! :+1: I think I’ll stick with the fair winds and not too much seas, it’s kind of difficult to pull crawfish pots in much seas. :rofl:

You could also try using a varistor. That would also make your relay contacts last longer.

I’m afraid I’m a total idiot when it comes to electronics. Where would that sit, and what would the values be? ?A link on eBay or Ali would be much appreciated!