ESP32-Based Power Quality Monitoring for Shop Machinery: Voltage Sag, Harmonics, and Brownout Detection
A CNC router that randomly faults mid-job, a VFD that trips on startup, or a laser PSU that resets when the dust collector kicks on are usually blamed on the machine. Often the real culprit is upstream: a voltage sag when a big motor starts, harmonic distortion from another VFD on the same panel, or a brownout dip that a sensitive control board can't ride through. Utility power on a typical shop circuit is messier than the clean sine wave on a datasheet, and most makers have no way to see it. This guide walks through building a continuous power quality monitor around an ESP32 that logs voltage sags, estimates harmonic distortion, and flags brownout events on your shop's incoming power, so the next time a machine glitches you have a timestamped graph instead of a guess.
What Power Quality Problems Actually Look Like
Power quality issues fall into a few categories that matter for a home or garage shop:
- Voltage sags (dips): A short drop in RMS voltage, typically 10-90% of nominal, lasting a few cycles to a few seconds. Usually caused by a large motor starting (your own dust collector, air compressor, or a neighbor's AC compressor on a shared transformer).
- Swells: The opposite — a brief rise above nominal, often when a large load drops off the same transformer.
- Harmonic distortion: VFDs, switching power supplies, and LED drivers all draw current in non-sinusoidal pulses, injecting harmonics (3rd, 5th, 7th order and up) back onto the line. High total harmonic distortion (THD) can cause transformer and motor heating and confuse cheap control electronics.
- Brownouts: A sustained low-voltage condition, as opposed to a brief sag, often from an overloaded circuit or a failing utility connection.
- Transients: Fast spikes from relay switching or lightning-induced surges, which this build does not resolve well given the ESP32 ADC's sample rate, but which a cheap inline surge protector addresses separately.
Safety First: You Are Measuring Mains Voltage
This project puts a sensor in electrical contact with 120V or 240V AC. Treat every part of the build as live until proven otherwise with a meter, not an assumption.
- Do not build a bare voltage divider directly off the mains to an ESP32 ADC pin. Use an isolated voltage sensor module (see below) that provides galvanic isolation via a transformer or optical path.
- Terminate all mains wiring in an enclosed, fused junction box or subpanel tap, not open on a breadboard.
- If you are not confident reading your panel's wiring and comfortable working near live mains, have an electrician make the tap for you; the ESP32 side of the project is completely safe to build and test separately beforehand.
- This article covers monitoring only. It is not a substitute for a licensed electrician diagnosing wiring problems, and it does not replace panel-level surge protection.
Hardware
The core of the sensing chain is a ZMPT101B isolated AC voltage transformer module for voltage sensing, paired with an SCT-013 split-core current transformer if you also want per-circuit current and real power draw. Both provide galvanic isolation so the ESP32 side never touches mains potential directly.
| Component | Role | Notes |
|---|---|---|
| ESP32 DevKit (S3 recommended) | Sampling and analysis | Needs a reasonably fast ADC loop; S3's dual-core helps if you also run WiFi logging |
| ZMPT101B voltage sensor module | Isolated AC voltage sampling | Adjustable onboard trimmer for scaling; calibrate against a known-good meter |
| SCT-013-030 current transformer | Non-invasive current sensing (optional) | Clamps around a single conductor; needs a burden resistor sized for your ESP32's ADC range |
| Precision voltage reference (optional) | ADC calibration | The ESP32's internal ADC is noisy and nonlinear near the rails; a reference improves repeatability |
| Enclosure with DIN rail mount | Panel mounting | Keep the mains-side wiring fully enclosed |
Sampling Strategy
The ESP32's ADC can sample fast enough (tens of kHz with the right driver configuration) to reconstruct a 60 Hz waveform with enough resolution for basic harmonic analysis, but it is not a lab-grade power analyzer. The practical approach:
- Sample the conditioned voltage signal continuously at 128 or 256 points per 60 Hz cycle using the ESP32's ADC in continuous DMA mode (the
adc_continuousdriver in ESP-IDF, or the equivalent in Arduino-ESP32's newer ADC API). - Compute true RMS over a rolling one-cycle window rather than peak-to-peak scaling — this is what actually tracks sags and swells correctly, since a distorted waveform's peak doesn't track its RMS the way a clean sine wave does.
- Run a single-precision FFT (the
esp-dsplibrary provides optimized radix-2 FFT for the Xtensa and RISC-V ESP32 variants) over a buffered set of complete cycles to extract the magnitude of the 3rd, 5th, 7th, 9th, and 11th harmonics relative to the fundamental, then compute THD as the ratio of the sum of harmonic RMS values to the fundamental RMS value. - Flag a sag event when RMS voltage drops below a threshold (commonly 90% of nominal) for more than half a cycle, and a brownout when it stays below a lower threshold (commonly 80-85%) for more than a few seconds — these thresholds roughly follow the IEEE 1159 categorization used in commercial power quality meters, scaled down for a hobbyist instrument.
Logging and Alerting
Raw per-cycle data is too much to store indefinitely, so log selectively:
- Push a rolling one-minute RMS voltage and THD average to InfluxDB or a local MQTT broker every few seconds for a live Grafana dashboard.
- On a detected sag, swell, or brownout event, capture and store a short waveform snapshot (a few cycles before and after the event) to flash or an SD card for later waveform inspection — this is what actually tells you whether a glitch correlates with your dust collector starting or something upstream of your panel.
- Publish an MQTT alert or Home Assistant notification when THD crosses a configurable threshold (5% THD is a common "starting to worry" line for sensitive electronics) or when a brownout exceeds a duration you set.
Correlating Events With Your Shop
The real value of this build isn't the instantaneous numbers, it's the timestamped log. Once you have a few days of data, cross-reference sag events against what else was running: a dust collector or air compressor starting under load is the single most common cause of a visible sag in a home shop, followed by a neighboring circuit's AC compressor cycling if you're on a shared utility transformer. If sags correlate with your own equipment starting, a soft starter on the offending motor (common for compressors) addresses it directly. If sags appear with no obvious shop-side cause, that's useful evidence to bring to your utility if you suspect an external supply problem.
Calibration Notes
The ZMPT101B's onboard trimmer sets a rough scale factor, but don't trust it out of the box. Compare your computed RMS voltage against a trusted multimeter or clamp meter across several real readings (not just one) and apply a correction factor in firmware. Re-check calibration after any change to the burden resistor or if you swap sensor modules, since unit-to-unit variation on these inexpensive transformer modules is real.
This isn't a replacement for a commercial power quality analyzer if you're chasing a genuinely difficult industrial problem, but for the price of an ESP32 and two sensor modules it turns "the machine just glitches sometimes" into a searchable log you can actually use to diagnose what's going on upstream of the equipment itself.