Mains voltage monitor

Measures 220 V mains through an isolated ZMPT101B module and warns about sags, surges and blackouts in Telegram or Home Assistant.

Power & batteries Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 2–3 h

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

🛒 Ready kit in the shop

Why a plain voltmeter is not enough

In many Ukrainian homes the voltage does not just disappear: it sags in the evening peak, then jumps when the power returns. A stabilizer hides the problem, but it cannot tell you how often it happens. This node turns the socket into a log: you see the minimum and maximum per hour and decide whether you need a stabilizer, a relay with a delay, or a complaint to the utility.

How the measurement works

The ZMPT101B is a small voltage transformer, so the mains side is galvanically separated from the ESP32. The board outputs an AC wave lifted to about half of the supply. You sample it a thousand times a second or more and compute the RMS over whole mains periods (a multiple of 20 ms at 50 Hz). Do this on ADC1 pins only, because ADC2 stops working while Wi-Fi is active.

  • Calibrate once against a good multimeter at two points, for example 215 V and 235 V, and store the factor.
  • Send an alert only after the value stays outside the limits for several seconds, and add a hysteresis, otherwise one dip means ten messages.
  • The module is isolated, but the wires and terminals on the mains side are still 220 V: fuse, closed enclosure and strain relief are not optional.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • ZMPT101B AC voltage sensor module
  • Inline fuse holder + fuses
  • Plastic project enclosure
  • Lever connectors (WAGO type) 2/3-pin

Wiring

Module Module pin ESP32 pin Note
ZMPT101B VCC 5V VIN
ZMPT101B GND GND GND
ZMPT101B OUT signal GPIO34 (ADC1) input-only, ADC1 works with Wi-Fi
ZMPT101B AC in L / N mains via 1 A fuse enclosed, qualified person only

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WebServer.h>

WebServer server(80);
String buildJson();   // implemented in the project below

String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }

const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";

void startWeb() {
  server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
  server.on("/json", []() { server.send(200, "application/json", buildJson()); });
  server.begin();
}
const int PIN_V = 34;
float CAL_FACTOR = 1.0;   // adjust so that the reading matches a multimeter
float vrms = 0;

float readVrms() {
  const int N = 1000;
  double sum = 0, sq = 0;
  for (int i = 0; i < N; i++) { int v = analogRead(PIN_V); sum += v; delayMicroseconds(200); }
  double mean = sum / N;
  for (int i = 0; i < N; i++) { double d = analogRead(PIN_V) - mean; sq += d * d; delayMicroseconds(200); }
  return sqrt(sq / N) * (3.3 / 4095.0) * 230.0 / 0.75 * CAL_FACTOR;   // 0.75 V rms sensor output at 230 V is only a start value
}

String buildJson() { return String("{"Vrms":") + num(vrms) + "}"; }

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  analogSetPinAttenuation(PIN_V, ADC_11db);
  connectWiFi();
  startWeb();
}

void loop() {
  server.handleClient();
  static uint32_t t = 0;
  if (millis() - t > 1000) { t = millis(); vrms = readVrms(); Serial.println(vrms); }
}

Build tips

  • Calibrate: compare the reading with a multimeter and adjust CAL_FACTOR.
  • Turn the sensor trimmer until the waveform is not clipped at 250 V.

What you need

Buy the parts kit · Estimated total: $13.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
ZMPT101B AC voltage sensor module × 1
$2.50
Buy at AliExpress Buy at Amazon.com
Inline fuse holder + fuses × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Plastic project enclosure × 1
$3.00
Buy at AliExpress Buy at Amazon.com
Lever connectors (WAGO type) 2/3-pin × 1
$2.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Ukraine: we buy and sell these parts ourselves, order them on this site.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Smart energy meter

Reads voltage, current, power and kWh with PZEM-004T and publishes them to a web page, MQTT or Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–4 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Inverter load manager

Watches the inverter / battery current and switches off non-critical loads with a relay when the battery runs low.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Balcony solar power monitor

Measures output and yield of a plug-in solar kit with a PZEM meter and shows it on a web page or in Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–4 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.