ZMPT101B: AC voltage as an RMS reading
The ZMPT101B is a small voltage transformer on a module with an op-amp and a trimmer. It isolates a mains-voltage input and puts out a small AC wave centered on half the board鈥檚 supply, which an analog pin can sample. The ZMPT101B library by Abdurraziq Bachmid samples that wave over whole cycles and returns its RMS voltage, the number a multimeter shows.
Built in on Mokxi: #include <ZMPT101B.h> and it builds, with nothing to install. Free, and no account needed.
What ZMPT101B does
The library has to know the mains frequency to sample whole cycles, so it is the second argument of the constructor: 50 Hz in Europe, Asia and Africa, 60 Hz in North America. The sensitivity, set with setSensitivity(), stands for the module鈥檚 trimmer position, and it is found by comparing the reading with a trusted meter and adjusting until they agree.
Mains voltage can kill. The module鈥檚 input side carries the full line voltage on exposed pins, so build and calibrate it in an enclosure, with a fused supply, or try the circuit in a simulator first. The board side is isolated by the transformer, but the screw terminals are not.
| Library Manager name | ZMPT101B |
|---|---|
| Author | Abdurraziq Bachmid |
| Include | #include <ZMPT101B.h> |
| Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | AC voltage sensor, ZMPT101B |
Install it
It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6.
In Mokxi it is built in: #include <ZMPT101B.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "ZMPT101B" by Abdurraziq Bachmid from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add ZMPT101B to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.1, so a sketch written here builds there too.
What to know in the simulator: The sensitivity is the part鈥檚 trimmer setting (its ratio), which a real board is calibrated for against a meter.
Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 Arduino Pro Mini 3.3 V 路 ESP8266 NodeMCU 路 ESP32 DevKit 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 ESP32-S3-DevKitC-1 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Seeed XIAO ESP32-C3 路 Seeed XIAO ESP32-C6
The calls you will use
In the order a sketch meets them.
ZMPT101B voltageSensor(A0, 50.0)
Creates the sensor on A0 for a 50 Hz supply. Use 60.0 for 60 Hz.
voltageSensor.setSensitivity(500.0f)
Sets the sensitivity that matches your module鈥檚 trimmer. Adjust it until the reading agrees with a meter.
float v = voltageSensor.getRmsVoltage()
Samples whole cycles and returns the RMS voltage.
float voltage = voltageSensor.getRmsVoltage();
Serial.print("Voltage: ");
Serial.println(voltage);
delay(1000);voltageSensor.getRmsVoltage(10)
Averages over ten cycles for a steadier number.
Example: Mains RMS voltage from a ZMPT101B
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <ZMPT101B.h>
// Find this by calibrating against a meter: here, the part's trimmer setting.
#define SENSITIVITY 600.0f
// The module's OUT on A0, on 50 Hz mains.
ZMPT101B voltageSensor(A0, 50.0);
void setup() {
Serial.begin(115200);
voltageSensor.setSensitivity(SENSITIVITY);
}
void loop() {
float voltage = voltageSensor.getRmsVoltage();
Serial.print("Voltage: ");
Serial.print(voltage, 0);
Serial.println(" V");
delay(1000);
}Wiring
- Arduino Uno R3 A0 to AC voltage sensor, ZMPT101B OUT
- Arduino Uno R3 5V to AC voltage sensor, ZMPT101B VCC
- Arduino Uno R3 GND to AC voltage sensor, ZMPT101B GND
Common errors and how to fix them
The voltage reads far too high or too low
The sensitivity does not match the trimmer. Measure the mains with a multimeter, then raise or lower the value passed to setSensitivity() until the two agree, and do not touch the trimmer afterwards.
It reads a few volts with nothing plugged in
That is noise on the analog pin. Keep the module鈥檚 wires short, power it from the same supply as the board, and treat readings under about 10 V as zero in the sketch.
Readings wobble by several volts
The frequency in the constructor is wrong for your country, so the library cuts cycles in the middle. Use 50.0 or 60.0 to match the mains.
Try ZMPT101B without installing anything
Open the example, change the sketch, and keep your version in a free account.