The circuit works perfectly on the bench, but as soon as the motor or relay switches on: the Arduino resets, the display shows strange symbols, and the sensor produces crazy numbers. That's noise (or EMI), one of the most common problems anyone building a real project runs into.
Sources of noise
- DC motors: the brushes inside produce small continuous sparks.
- Relays and coils: produce a high voltage at the moment of switching off.
- Switching circuits: buck and boost converters chop current at high speed.
- A large sudden load: drags the supply voltage down for a moment.
- Wi-Fi on the ESP32: draws current in bursts.
How does it get in?
| Path | Meaning | Main fix |
|---|---|---|
| Through the supply | Spikes travelling along the power wires | Capacitors, filters and separate supplies |
| Through ground | A large current shifts "zero" | A separate ground for loads, a ground plane |
| Through the air | A current loop acting as an antenna | Small loops and twisted wires |
Identify the path first, and the fix becomes much easier.
The fixes
1. Proper decoupling
A 100 nF capacitor next to every chip, and a large capacitor where the supply enters and next to big loads.
2. Ground
- A ground plane on the PCB. See the ground plane article.
- Large loads get a separate ground path back to the source, not passing under the sensors. The details are in the ground article.
3. Small current loops
For any fast-changing current, the area enclosed by its loop (out and back) acts as an antenna. Keep the outgoing and return wires side by side, and in a buck converter keep the loop of the switch, diode and capacitor as small as possible.
4. At the source
- A 100 nF capacitor across the motor's own terminals.
- A protection diode across any coil.
- A snubber (resistor and capacitor) across relay contacts switching inductive loads.
Motor capacitors in detail: on motors that are very noisy, fit 3 capacitors of 100 nF: one between the terminals, and one from each terminal to the motor's metal case. Solder them on the motor itself with the shortest possible leads, not on the board.
Common snubber values: a resistor of about 100 Ω in series with a 100 nF capacitor (rated for the load voltage), with both in parallel with the relay contacts. If the load is on 220 V, the capacitor must be a special mains-rated X2 type. See mains safety.
5. Separate supplies
- Motors and relays on a separate supply, or at least after a filter.
- An optocoupler if you need full isolation.
A simple filter for the microcontroller: if they must share the same supply, feed the Arduino's power through a ferrite bead or a small inductor, followed by a large capacitor (100–470 µF) + 100 nF. That's an LC filter that stops the spikes reaching the chip.
6. Wires
- Twist the outgoing wire with the return (twisted pair). This reduces the noise radiated from them and picked up by them.
- Keep sensor wires short, and away from motor wires. If they're long, use shielded cable.
- A ferrite ring (ferrite bead) on cables that radiate or pick up noise.
Ferrite is a material that passes direct current normally, but resists high frequencies and turns them into heat. That's why you find it as a black lump on display and charger cables.
7. A filter on the sensor input
An analog sensor on a long wire: fit a 1k resistor in series and a 100 nF capacitor to ground at the Arduino pin. That's an RC filter that removes fast spikes, and it protects the pin too. And for button and sensor inputs in harsh places, a TVS diode protects against sparks and static.
8. In code
- Average or median the readings instead of a single reading.
- Debounce buttons. See the buttons article.
- Watchdog: if the chip hangs, it resets itself and starts working again.
The median is better than the average when there's one crazy spike among normal readings: take 5 readings, sort them, and take the middle one. The odd spike is removed completely.
int median5(int pin) {
int a[5];
for (int i = 0; i < 5; i++) a[i] = analogRead(pin);
for (int i = 0; i < 4; i++) // a simple sort
for (int j = i + 1; j < 5; j++)
if (a[j] < a[i]) { int t = a[i]; a[i] = a[j]; a[j] = t; }
return a[2];
}
The watchdog on the Uno:
#include <avr/wdt.h>
void setup() { wdt_enable(WDTO_2S); } // if not reset within two seconds
void loop() { /* ... */ wdt_reset(); } // "I'm still running"
If the program hangs because of noise, the chip resets itself after two seconds. It's not a cure; it's a safety net for devices that run unattended.
How do you find where the problem is?
An oscilloscope on the microcontroller's supply line while you start the motor will show you the spikes. If you don't have one, try disconnecting the loads one by one until the problem disappears.
| Symptom | Start with |
|---|---|
| Reset when the motor starts | A large capacitor, a separate supply, and motor capacitors |
| Reset when the relay switches off | A diode across the coil, and a snubber across the contacts |
| ADC readings jump | An RC filter, a separate ground, and a median |
| The display shows symbols | Short I2C wires kept away from the motor |
| The ESP32 browns out | A 10–22 µF capacitor next to the module, and a shorter, thicker cable |
FAQ
Should I put the circuit in a metal box? A metal box connected to ground protects against noise through the air, but not against noise arriving along the supply wires. Start with the fixes above.
Does noise damage parts? Mostly it only disrupts operation. But high spikes from coils without a diode can genuinely destroy transistors and chip pins.