Fixing Relay Chatter When Switching AC Loads from an ESP32

πŸ”Œ Part of: Embedded Systems Fundamentals
Fixing Relay Chatter When Switching AC Loads from an ESP32

A relay that buzzes, clicks repeatedly, or resets your ESP32 is almost never a broken relay. It is a power, isolation or snubber problem. Fix those three and the circuit becomes as reliable as any commercial smart switch.

Three symptoms, three different causes

  • Relay buzzes or chatters when idle β€” the coil is not getting enough current from the 3.3 V pin.
  • ESP32 reboots when the relay clicks β€” the coil inrush current is pulling the 5 V rail down.
  • Relay sticks or contacts arc β€” there is no snubber across the AC load, so inductive kickback welds the contacts over time.

Identify which one you have before changing hardware.

Why cheap relay boards fail on 3.3 V logic

Many "5 V relay modules" sold online use a coil that draws 70–90 mA at 5 V. The module has an opto-isolator and a transistor, but the input side is often designed for 5 V logic. Driving the input from a 3.3 V ESP32 pin puts the transistor in a half-on state β€” enough to make the relay buzz, not enough to pull in cleanly.

The fix is one of:

  • Use a module explicitly rated for 3.3 V logic input (many now are, look for a "3V3" jumper).
  • Add a level shifter or a small NPN transistor on the input side.
  • Power the coil from a separate 5 V supply, and only switch the control line from the ESP32.

Separate the coil supply from the ESP32

The coil is an inductor. When it turns on, current rises fast; when it turns off, it produces a reverse voltage spike. If the coil and the ESP32 share a 5 V rail with only a small capacitor, the ESP32 sees the spike as a brownout.

5V PSU ──┬── Relay coil VCC
         β”‚
         └── 7805 or buck ── 3.3V ── ESP32

Better: give the relay its own 5 V supply. The ESP32 keeps its USB supply, and the two share only ground. This single change eliminates most reboot problems.

Add a bulk capacitor at the relay module

If you must share the rail, add a 470 Β΅F to 1000 Β΅F electrolytic capacitor directly at the relay module's VCC and GND pins, plus a 100 nF ceramic. The bulk cap absorbs the inrush spike; the ceramic handles the high-frequency noise from the contacts.

Snubber for AC loads

When the relay opens under load, the inductance of a motor, transformer or fluorescent ballast produces an arc across the contacts. Over hundreds of cycles this pits the contacts and eventually welds them shut. Add an RC snubber across the load:

  • R = 100 Ξ©, 1 W metal-oxide.
  • C = 0.1 Β΅F, 275 VAC X2 class (rated for mains).
  • Connect the two in series, then across the load terminals (not across the relay contacts β€” across the load).

Do not use a standard DC capacitor here. Mains transients will destroy it, and the failure can be dangerous. Always use an X2-rated part.

Flyback diode on the coil side

Most relay modules already have a 1N4148 or 1N4007 across the coil. Verify it is present with a multimeter in diode mode. If it is missing, add a 1N4007 with the cathode on the coil's positive side. Without it, the transistor driving the coil will eventually fail.

Common-ground mistakes

If the relay module is opto-isolated and you actually want isolation, do not connect the ESP32 ground to the relay module's coil ground. Use two separate supplies. If you do tie the grounds, you have defeated the isolation and you may as well use a non-isolated module. Choose one design and stick to it.

Debouncing in firmware

Sometimes the chatter is not electrical β€” it is your code toggling the pin too fast because a sensor is flapping. Add a debounce window:

const int RELAY_PIN = 26;
unsigned long lastChange = 0;
const unsigned long MIN_INTERVAL = 2000; // 2 s minimum

void setRelay(bool on) {
  if (millis() - lastChange < MIN_INTERVAL) return;
  digitalWrite(RELAY_PIN, on ? HIGH : LOW);
  lastChange = millis();
}

Two seconds is enough to protect the relay and to stop sensors from cycling it hundreds of times per hour.

What about solid-state relays?

For loads under 2 A, a solid-state relay (SSR) eliminates chatter, arcing and mechanical wear. Use a zero-cross type for resistive loads and a random-turn-on type for inductive loads. They cost more but last effectively forever and are silent.

Summary

Relay chatter is a symptom, not a fault. Check the coil supply, separate it from the ESP32, add a bulk capacitor, install a proper X2 snubber across AC loads, and debounce in firmware. Do those five things and your smart switch will click cleanly for years.

Comments