Arduino library · Sensors

EmonLib: RMS voltage, current and power from AC sensors

Yes. You can use the EmonLib library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (AC voltage sensor, ZMPT101B). The example below, "Mains volts and amps with EmonLib on a ZMPT101B and an ACS712", runs on the Arduino Uno R3.

Built in on Mokxi: #include <EmonLib.h> and it builds, with nothing to install. Free, and no account needed.

What EmonLib does

EmonLib is OpenEnergyMonitor’s library for measuring AC power with an Arduino. It samples a voltage sensor and a current sensor on two analog pins for a number of mains half cycles, then works out the RMS voltage, the RMS current, the real power in watts, the apparent power and the power factor. Home energy monitors built on an SCT-013 clamp, a ZMPT101B module or an AC adapter mostly run on it.

An ACS712 or ZMPT101B library gives you one quantity from one sensor. EmonLib’s gain is the pairing: by sampling volts and amps together, sample by sample, it finds the real power a load draws, not just volts times amps, which is wrong for motors and switching supplies. calcVI() does the whole sum; calcIrms() alone measures current when you have no voltage sensor and want an estimate.

Each sensor needs a calibration number, its output scaled to real volts or amps. In Mokxi the ZMPT101B’s calibration is the module’s ratio, and the ACS712’s is 1000 over its millivolts per amp (5.405 for the 5 A board). The example reads a 230 V ZMPT101B on A0 and a 5 A ACS712 on A3 with a 50 Hz load of 0.35 A, and throws the first window away because the supply measurement is not ready for it. On an ESP32 the library still assumes 10 bits, so readings come out four times too large unless the calibration allows for it.

Library Manager nameEmonLib
AuthorOpenEnergyMonitor
Include#include <EmonLib.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, WCH CH32V003, Raspberry Pi Pico, Raspberry Pi Pico W, Seeed XIAO SAMD21, STM32, STM32 Nucleo-F411RE, Arduino Uno R4, ESP32 DevKit, ESP32-S3-DevKitC-1, Arduino Pro Mini 3.3 V, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4 WiFi
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAC voltage sensor, ZMPT101B, Current sensor, ACS712

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, WCH CH32V003, Raspberry Pi Pico, Raspberry Pi Pico W, Seeed XIAO SAMD21, STM32, STM32 Nucleo-F411RE, Arduino Uno R4, ESP32 DevKit, ESP32-S3-DevKitC-1, Arduino Pro Mini 3.3 V, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <EmonLib.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "EmonLib" by OpenEnergyMonitor from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add EmonLib to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.0, so a sketch written here builds there too.

What to know in the simulator: The voltage calibration is the zmpt101b part’s ratio and the current calibration 1000 over the acs712’s mV per amp. As upstream, ADC_BITS is 10 on the ESP32 boards, whose analogRead() gives 12 bits, so readings there come out four times too big unless the calibration allows for it. Not listed for the ATtiny85, whose flash the float sums overflow, or the ESP8266, which has one analog pin.

The calls you will use

In the order a sketch meets them.

EnergyMonitor emon1

One monitoring channel: a voltage input and a current input.

emon1.voltage(A0, 600, 1.7)

The voltage sensor’s pin, its calibration and a phase correction (1.7 is the usual starting point, 1 means none).

emon1.current(A3, 5.405)

The current sensor’s pin and calibration: 1000 / 185 for a 5 A ACS712, or about 111 for an SCT-013-000 with a 18 ohm burden.

emon1.calcVI(20, 2000)

Samples for 20 half cycles (zero crossings), giving up after 2000 ms, and fills Vrms, Irms, realPower, apparentPower and powerFactor.

emon1.calcVI(20, 2000);
Serial.print(emon1.Vrms);
Serial.print(" V ");
Serial.println(emon1.realPower);

emon1.calcIrms(1480)

Measures current alone over 1480 samples and returns the RMS amps.

emon1.serialprint()

Prints realPower, apparentPower, Vrms, Irms and powerFactor on one line, for a quick look.

emon1.readVcc()

The board’s own supply in millivolts, measured against the internal reference, which EmonLib uses to scale the readings.

Example: Mains volts and amps with EmonLib on a ZMPT101B and an ACS712

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "EmonLib.h"  // Include Emon Library

EnergyMonitor emon1;  // Create an instance

void setup() {
  Serial.begin(9600);
  // The ZMPT101B on A0: its calibration is the module's ratio, from a meter.
  emon1.voltage(A0, 600, 1.7);  // Voltage: input pin, calibration, phase_shift
  // The ACS712 5 A board on A3: 1000 / 185 mV per amp.
  emon1.current(A3, 5.405);     // Current: input pin, calibration.
  // The first window starts before readVcc() has a bandgap reading, so its
  // volts come out 0: measure once and throw it away.
  emon1.calcVI(20, 2000);
}

void loop() {
  emon1.calcVI(20, 2000);  // Calculate all. No.of half wavelengths (crossings), time-out
  float supplyVoltage = emon1.Vrms;  // extract Vrms into Variable
  float Irms = emon1.Irms;           // extract Irms into Variable
  Serial.print("Vrms: ");
  Serial.println(supplyVoltage, 0);
  Serial.print("Irms: ");
  Serial.println(Irms, 2);
  Serial.print("Irms alone: ");
  Serial.println(emon1.calcIrms(1480), 2);  // Calculate Irms only
}

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
  • Arduino Uno R3 A3 to Current sensor, ACS712 OUT
  • Arduino Uno R3 5V to Current sensor, ACS712 VCC
  • Arduino Uno R3 GND to Current sensor, ACS712 GND
  • Signal generator OUT to Current sensor, ACS712 IP+
  • Current sensor, ACS712 IP- to Resistor 1
  • Resistor 2 to Ground GND
  • Signal generator GND to Ground GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Irms reads 0.1 to 0.3 A with nothing switched on

That is noise in the RMS sum, which never goes to zero. Ignore readings below a small threshold, and make sure the sensor’s output sits at mid scale with no current, about 2.5 V on a 5 V board.

Vrms is 0 on the first reading

The supply measurement is not ready on the first window. Call calcVI() once in setup() and throw that result away, as the example does.

Real power is negative

The current sensor faces the wrong way, or the voltage phase is inverted. Turn the clamp around or swap the ACS712’s IP+ and IP- leads, or negate the current calibration.

All readings are four times too big on an ESP32

EmonLib assumes a 10-bit ADC, and the ESP32 reads 12 bits. Divide both calibrations by four, or set ADC_BITS to 12 in the library if you can edit it.

Questions and answers

Is it safe to measure mains with an Arduino?

Only through isolated sensors such as a clamp-on current transformer and an AC adapter or ZMPT101B module, and with the mains side enclosed. Never connect mains to a board pin.

Projects that use EmonLib

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try EmonLib without installing anything

Open the example, change the sketch, and keep your version in a free account.