Still more problems with 3v relays and Arduinos

This has got me looking for similar modules.
I've found one that has even has a built-in isolated mains power supply so great if you are using the relay module to switch mains power in that you don't even need a separate low voltage power supply. It uses an ESP32 so is not directly suitable for the OP's application but maybe there is an ESP8266 equivalent. https://de.aliexpress.com/item/1005005983735481.html
It also has proper mounting holes and breaks out many GPIO pins

EDIT
I couldn't resist and have added one to my shopping list for a time switch application for controlling lights using an algorithm which respects sunrise and sunrise times.

Agreed.

It's why I hang on to a few old filament lamps for testing.

and where does your AC mains power come from?

I've just noticed another design flaw.
According to the diagram, linked in post #1, you have a led (with 150R series resistor) connected with its anode on GPIO2 (D4) and its cathode on GND. It is not wrong to have a led on this pin but that pin must be HIGH at boot time and fails if it is LOW (see table post #54). Therefore, the led should be wired so its anode is on 3v3 and the cathode on GPIO2. As you have it, the behaviour depends to some extent on the forward voltage of the LED so, for example, a red led would be worse than a blue one. If you correct this then you also have to invert the program logic because a LOW will switch the led on and a HIGH will switch it off.

We are still waiting to see what appears on the serial monitor during one of these "hangs". This is a really basic debugging step before all these other experiments with component substitution etc.

@6v6gt I did the serial monitor test about 12 hours ago, there is no output to the serial monitor when the hang occurs. However this does not indicate whether the swipe system is hung or the whole device is hung. I was going to put a heartbeat message on the serial every second to try and indicate what has happened. May not happen for a day or two.

I think you are heading down a rabbit hole.
The fact that everything worked when you bypassed the relay and connected the AC straight through indicates it’s an AC problem.

@jim-p I agree it might be AC, but I also controlled the relay via a completely separate arduino and it still hung. And I created 2 test scenarios, one with the relay feed by mains and the arduino fed by USB, and the reverse, and both tests hung. I am thinking I might make both the relay and the arduino powered by separate USB feeds to eliminate those.

Note: I am also using ethernet over power for the same dual wall socket that the system is using, but I have been doing that for 2 years and this issue only came up 2 months ago.

It doesn't matter, the other test (post #22) has proven that the problem occurs only when AC goes through the relay. Although I'm still stumped as to how this causes the Arduino to hang.

Some bad power supply designs will create a lot of high frequency EMI and feed it back through the AC lines. You might want to disconnect any unnecessary equipment from you mains when you do the testing.

Thanks, will try that when I next get the chance.

Am AFK the next 2 days, will make more attempts in the weekend.

Thanks all for feedback so far.

It’s unfortunate that you can’t use a reference system, as that would have been a major advantage for isolating the issue.

In that case, the only sensible approach is to systematically eliminate the previously mentioned problem areas (“There are typically several possible sources of problems”) through controlled testing.

I would suggest the following step-by-step method:

1. Clean baseline setup (no mains involved)
Build a minimal, controlled test setup completely isolated from any mains voltage.

  • Use only the MCU, basic wiring, and a simple output like an LED
  • Verify that the logic works reliably (on/off switching)
  • Use the Serial Monitor extensively for diagnostics
  • If necessary, start from the absolute minimum (e.g. the blink sketch) and build up step by step

2. Introduce power supply and SSR (still no mains)
Add the intended power supply and a solid-state relay (no mechanical relay), but keep the SSR disconnected from mains voltage.

  • Maintain physical separation (ideally ≥ 1 m) between MCU and power/relay section
  • Verify that switching the SSR control input does not disturb the MCU
  • This step checks for coupling effects via wiring, grounding, or the power supply itself

3. Switch the real load
Only in the final step, connect the SSR to the actual mains load.

  • Observe whether the system remains stable during switching events
  • If problems appear here, the root cause is very likely EMI, load characteristics (e.g. inductive behavior), or insufficient suppression (snubber, filtering, layout)

One important point: it is quite possible that your overall design is operating close to its limits. In such cases, whether the system works or not can become somewhat random, depending on small variations in components, wiring, or environmental conditions.

Regardless of the outcome, I would strongly recommend physically separating all parts that are directly connected to mains voltage and using a high-quality power supply with good isolation and low noise on the output side. Poorly filtered or noisy supplies can easily couple disturbances into the control electronics and cause exactly the kind of behavior you’re seeing.

This approach may feel a bit tedious, but it is often the only reliable way to get to the root cause in systems affected by intermittent or EMI-related issues.

Do you or have you seen any application generated messages at all to the console during the execution of this application ?
Usually, a glitch leading to a hang or the hang itself generates some sort of console output. That is assuming that the application does/can write to the serial monitor with the odd debug/progress message. If there is a hang then a watchdog timer should cut in and spew out messages. There may be exceptions such as landing in a loop which contains a delay/yield statement. A crash at reboot, say following a glitch would also generate a series of messages.

Another approach would be simply to abandon the old (hardware) design and start from scratch. Issues with the existing design and optimisation suggestions have already been pointed out several times. I have the feeling now that we are here trying to help you identify the last straw which broke the camel's back.
These changes will imply some minor changes to the code like shuffling GPIO pins etc.
You could even upgrade to an ESP32 but then with some additional changes to the code but probably not serious.

Having now seen that modules are available which appear to cover say 90% of the hardware complexity (e.g. post #82 and #72) I'd say that using such a device you have to only add the RFID reader and a couple of leds and would definitely suggest such an approach.

EDIT
This is a ESP8266 based relay module with built in isolated mains power supply and appears to break out all the usable GPIO pins so should be a good fit for your project: https://de.aliexpress.com/item/1005005693000487.html

To program it, you'd need a separate USB UART adapter module because, unlike the Wemos which you have been using, this board does not have such a built in device. Here is an example: https://de.aliexpress.com/item/1005005415840137.html .

No hurry.

Good find.
I looked for modules with AC supply included, but didn't see one in a hurry.
That module is the ideal replacement for the ratsnest that OP has been building.
And an upgrade to the ESP32 from the ESP8266, which is now obsolete.

My HomeAssistant server (N100) does that for me, with ESPHome loaded on the ESP devices. Controling and logging dozens of light at sunrise and sunset and it knows when internet connected devices are turned on/off, like the TV. HA turns off my lounge lights 5min after I turn the TV off. It knows, from the connected weather station, when it gets dark outside too.
Leo..

OK updates after a weekend doing other things.

Devices recap

10A - new device with 10A relay

10B - old legacy device with 10A relay, I have replaced the previously blown relay.

30A - new device with 30A relay

So I took all 3 devices down to the production site, and gave them a test.

10A didn’t work at all, I need to fix some cabling which has broken loose.

30A still had problems, not recognising swipes on mains but recognising swipes on USB power.

10B logged on fine, swiped on and off and switched the relay using mains power. I swiped on and off multiple times.

I took the ESP8266 out of 10A and put it into 10B, with (as far as I can tell) the same software onboard. It didn’t recognise any swipes at all.

Put the old ESP8266 back in 10B, and it worked fine again.

I visually tracked all cabling on 10B and it matches 10A.

Ergo, I think something is up with the drivers for the RC522 or the ESP8266 inside the GUI. I daren’t try to update code on 10B’s original ESP8266 as I will lose a point of reference if it stops working too.

I will try to get 10A re-cabled and working on USB, then downgrade the drivers on my Arduino IDE and see what happens.

Standby later this week.

You didn't say how you got on but anyway, in the mean time, I have got hold of one of the boards mentioned back in post #82.
It would replace most of your modules (power supply, relay module, microcontroller board) leaving you to supply only a separate RFID reader and two LEDs. For this specific board you'd have to upgrade the software because it is ESP32 based but usually that means merely swapping out a library or so. The wiring and assembly would be much cleaner and the board is only 80mm x 45mm and breaks out most pins.

I did a short write-up here with some sample code: ESP32 WiFi Bluetooth Single Relay module sample code

@6v6gt thanks for the extra testing and update. I have passed the project over to other members of my community organisation, and they are investigating. I have other issues I need to focus on, but will definitely bookmark this topic.

Another user in a different part of town has got their system working but has altered some of the plans to do so. i will ask them to send thee details through.

Appreciate the contributions everyone, will probably start a new topic in 6 months or so.

remember the current rating of relays and contactors depends on the type of load - see Electrical Standards For Contactors AC1, AC2, AC3, AC4, DC1, DC2 DC3

e.g. A contactor rated for 10 amps resistive load could be reduced to 2 or 3 amps for an inductive load such as a Squirrel-cage motor

I would assume cheap hobby type relays with a rating of 10amps printed on the case would be for a resistive load and reduce it significantly for an inductive load

if I have application switching an inductive load such as a 2kW pump I would use the ESP32 3.3V relay to swich an external contactor, e.g. for 2.2kW pump use a 20amp AC3 rated contactor

Yes.
I have microcontroller thermostat, uC controls small DPDT relay through a transistor, small relay provides signalling and switches 20A contactor for 3kW water heater load.
Keeps it modular and fixable.
Even the better relays come with faults built in.
Found a DPDT relay with a gap between the NC contacts you could just about slide a cigarette paper between. Luckily, only used the NO contacts.

Beginners always forget to add a snubber circuit to the relay contacts when switching inductive loads, which is the main sugarcube relay failure we see on this forum.
No snubber circuit welds the contacts shut (load permanently ON).
Leo..