← How-Tos
electronics 2 days ago ◑ 3 views ◯ 6 min read

ESP32-Based Power Quality Monitoring for Shop Machinery: Voltage Sag, Harmonics, and Brownout Detection

esp32power qualityvoltage sagharmonicsbrownoutzmpt101bemonshop electricalpredictive maintenance

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:

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.

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.

ComponentRoleNotes
ESP32 DevKit (S3 recommended)Sampling and analysisNeeds a reasonably fast ADC loop; S3's dual-core helps if you also run WiFi logging
ZMPT101B voltage sensor moduleIsolated AC voltage samplingAdjustable onboard trimmer for scaling; calibrate against a known-good meter
SCT-013-030 current transformerNon-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 calibrationThe ESP32's internal ADC is noisy and nonlinear near the rails; a reference improves repeatability
Enclosure with DIN rail mountPanel mountingKeep 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:

  1. 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_continuous driver in ESP-IDF, or the equivalent in Arduino-ESP32's newer ADC API).
  2. 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.
  3. Run a single-precision FFT (the esp-dsp library 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.
  4. 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:

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.