Arduino AC measurements

Sometimes there is need to measure power going to different devices. Measuring DC power consumption is pretty easy, but when you try to do the same for AC circuit, things start to become complicated. How to Measure Electrical Power article will discuss best practices for making electrical power measurements, starting with power measurement basics and proceeding to the types of instruments and associated components typically used to make measurements. DC power measurement is relatively simple as the equation is simply watts = volts x amps. For AC power measurement, the power factor (PF) introduces complexity as watts = volts x amps x PF. This measurement of AC power is referred to as active power, true power or real power. Power is typically measured with a digital power analyzer or a DSO (digital storage oscilloscope) with power-analysis firmware.

What if you want to implement power mesurement yourself? Power consumption in AC circuit is correctly measured by calculating volts x amps = volt-amps (apparent power) over time, using at least one complete cycle. Using digitizing techniques, the instantaneous voltage is multiplied by the instantaneous current then accumulated and integrated over a specific time period to provide a measurement. This method provides a true power measurement and true RMS measurements for any waveform up to the bandwidth of the instrument.

In addition to math, you will need to provide some sensors to measure current and voltage. For AC current measurements potential sensor types are shunt resistor, current transformer, hall current sensor and rogowski coil. Usually current transformer and hall current sensor are the most suitable types to use with Arduino (they both provide isolation from measurent circuit). For AC voltage measurements the most suitable sensor types are voltage transformercapacitive voltage divider and resistive voltage divider. From those alternatives only voltage transformer can provide galvanic isolation from circit being measured.

Can this measurement of AC power made using Arduino? The answer is yes, ans there are several ways it can be done. AC Power Theory – Arduino maths web page provides inntroduction how this can be implemented with Arduino (check also Advanced maths). There is EmonLib library that provides you a set of ready made AC measurement calculation routines that you can use easily.

Arduino sketch – voltage and current from OpenEnergyMonitor project shows how you can do the AC power measurements with Arduino.

#include "EmonLib.h"              // Include Emon Library
EnergyMonitor emon1;              // Create an instance

void setup()
{  
  Serial.begin(9600);
  
  emon1.voltage(2, 234.26, 1.7);  // Voltage: input pin, calibration, phase_shift
  emon1.current(1, 111.1);        // Current: input pin, calibration.
}

void loop()
{
  emon1.calcVI(20,2000);          // Calculate all. No.of crossings, time-out
  emon1.serialprint();            // Print out all variables
}

To us this simple looking source code, you need to have EmonLib installed (it does all the complex calculations so you don’t need to worry about them). The simplest way is to download EmonLib zip packet and extract it to arduino libraries folder. It will give the needed library and project examples.

Here is example code I used “voltage_and_current” (my modified version):

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include “EmonLib.h” // Include Emon Library
EnergyMonitor emon1; // Create an instance

void setup()
{
Serial.begin(9600);

emon1.voltage(2, 11.7 /* 234.26 */, 1 /* 1.7 */); // Voltage: input pin, calibration, phase_shift
emon1.current(1, 5.5 /* 111.1 */); // Current: input pin, calibration.
}

void loop()
{
emon1.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)

float realPower = emon1.realPower; //extract Real Power into variable
float apparentPower = emon1.apparentPower; //extract Apparent Power into variable
float powerFActor = emon1.powerFactor; //extract Power Factor into Variable
float supplyVoltage = emon1.Vrms; //extract Vrms into Variable
float Irms = emon1.Irms; //extract Irms into Variable
}

I got the following output when I measured 20W halogen lamp system:

ardpower

 

Here is picture of the Arduino Nano + Nano Sensor Shield based test circuit I used:

image

In this test circuit I measured 12V halogen lamp system current and voltage. For current mesurement I used ACS712 sensor based current measurement sensor module with +-5A current measurement range. For voltage measurement I used a simple voltage divider module (22k ohm from input pin to +5V, 22 kohms from input pin to ground, 120 kohms from AC voltage source to input pin).

One note in plannng to use this power measurements: If you plan to measure mains power, you need to understand all the safety details related to mains power. When testing the circuit, I highly recommend that you have a low voltage test system that you can sefely test and debug your designs (hardware and software). I have used 12V halogen lamp system for this: a traditional 12V transformer gives out safe 12V AC output usually at several amperes, you can control the load easily by turning 12V lamps on/off as needed. When you have debugged your design at safe voltages, you can start thinking of working with higher dangerlous voltages!

 

Related project pages:

OpenEnergyMonitor building blocks

Emoncms

Home Energy Monitoring System

Watt Meter build walks you through Power Measurement basics

DIY Digital AC Watt Meter

Digital Data from a Cheap Power Meter

 

33 Comments

  1. Tomi Engdahl says:

    Self Built Power Meter Uses Dual Sense Transformers
    http://hackaday.com/2015/08/08/self-built-power-meter-uses-dual-sense-transformers/

    [Renaud] built a AC power meter from scratch. While commercial power meters like the Kill A Watt are available [Renaud’s] build gives an interesting insight into the methods used.

    currentAt the heart of [Renaud’s] design lie two sense transformers. The first is a typical voltage stepdown transformer. This brings the AC line voltage down to +/- 10V, which is more amenable to digital sampling. The second is a current sense transformer

    http://renaud.schleck.free.fr/wattmetre.php

    Reply
  2. Tomi Engdahl says:

    Another mains power meter project:

    WiFi Power Monitor
    http://hackaday.com/2015/10/04/wifi-power-monitor/

    Building your own hardware to measure AC power isn’t a simple task. There’s a number of things to measure, including voltage, current, power, and power factor. The Atmel 90E24 is a single chip solution designed for this exact purpose. Connect a few components, and all the power data is available to a microcontroller over SPI.

    Aside from the Atmel 90E24 device, a high power and low resistance resistor is needed for shunt sense current measurement.

    [hwstar] built a custom power monitoring board based on this IC. His AC-Emeter will give you all the measurements you’d want, and includes an ESP12 module for data collection and WiFi connectivity.

    Hardware for an ESP8266 or Arduino based AC power meter using the Atmel 90E24 energy management chip
    https://github.com/hwstar/HW-AC-Emeter

    Reply
  3. Tomi Engdahl says:

    Energy monitor with Arduino
    Measure energy consumption with Arduino
    https://hackaday.io/project/8505-energy-monitor-with-arduino

    Recently I discovered http://openenergymonitor.org/
    It has all necessary information for building home energy monitor – software and hardware.
    I have a few ideas for improvement.

    Reply
  4. Tomi Engdahl says:

    Using low cost appliance energy meter for AC power measurements:

    Energy Meter Hacking – Reading EOUT Pulses
    http://steelcityelectronics.com/2015/08/02/energy-meter-hacking-reading-eout-pulses/

    Reply
  5. Tomi Engdahl says:

    Finally, a Power Meter Without Nixies
    http://hackaday.com/2016/01/22/finally-a-power-meter-without-nixies/

    https://github.com/TSalwach/ICPA/wiki/Design-considerations

    First steps lead me to OpenEnergyMonitor. I looked into https://github.com/openenergymonitor/EmonLib/blob/master/EmonLib.cpp

    and found that is not something that I’m looking for.

    it’s one phase only (3 phase = 3 Arduinos ~ still cheap for AVR based one but not so cheap for 12-bit resolution ARM based one )
    Arduino’s way of using ADC’s is very slow – only about 50 I,V samples per mains cycle. They do some “weird” filtering to deal with that.

    I wanted:

    to collect all 3 phases currents and voltages simultaneously
    to make AC waveform graphs
    12-bit resolution
    accuracy…as I plan to use only one device on whole home and like to see 1W difference on meter capable measuring some kWatts
    reactive power with sign (indication of capacitive or inductive load)
    to have frequency measurement like on MainsFrequency.com – read the story behind that
    to have whole system in my home network – without need of internet connection and cloud storage
    I love everything open and I wanted this project to be replicable, customizable and very simple

    After some research starting at AC power I’ve found excellent application note: Texas Instruments SLAA577G – Implementation of a Three-Phase Electronic Watt-Hour Meter

    Reply
  6. Syed says:

    how you get this calibration??

    emon1.voltage(2, 234.26, 1.7); // Voltage: input pin, calibration, phase_shift
    emon1.current(1, 111.1); // Current: input pin, calibration.

    Reply
  7. Syed says:

    i am confused about calibrating voltage and current sensor??? i want to make pf meter to measure pf of loads such as bulb.

    Kindly help

    Reply
    • Tomi Engdahl says:

      I did the calibration on one of my protoype using the following method:

      I had 12V halogen 20W lamp that used traditional transformer (gave out 50 Hz AC).
      I connected the Arduino board plus to multimeters (one for current and other for voltage).
      Then I looked at what the Arduino showed and what the multimeter showed. For example if the voltage Arduino showed was for example around twice what it should be, I adjusted the cablibration values according to the difference (in this case multiplier should be either twice or half of the original value depending how it is used, I don’t remember, but easy to test which is right direction).. Do the same for current.
      For phase shift, use the data from the current transformer data sheet.

      Reply
    • suleman says:

      Are u done it???

      Reply
  8. Talha says:

    emon1.calcVI(20,2000);
    What it is for? why 20 and 2000

    Reply
    • Tomi Engdahl says:

      The first parameter (20) defines over how long time period the calculation is performed. Here the value 20 means over 20 zere crossings (=10 AC cycles).

      The second parameter (2000) is some kind of timeout to stop analyzing if it takes more than reasonable amount of time.

      Reply
    • Thien says:

      20 means sample. My code on 1 second: emon1.calcVI(2000,1000); // Calculate all. No.of half wavelengths (crossings), time-out

      Reply
  9. Akpellas says:

    First of all, I’m not an energy expert, but I would like to explain two concepts that I think are misunderstood.
    The EmonLib library is designed to measure 3-phase electric power , so uses the factor (sqrt root) 3 = 1.732050 (380V), but when you want to measure 1-phase electric power, the factor is (sqrt root) 2 = 1.414213 (230V), in this case must be replace value 1.7 –> 1.41 in the formula
    emon1.voltage (2, 234.26, 1.41); // Voltage: input pin, calibration, phase_shift.
    About Power factor,this value is provided by the company that builds motors, which writes on specifications plate, and shows how many energy transforms in job, always this value is between 0.80 to 0.85, never will be over 1. In 1-phase electric power isn’t need power factor, due Electrical appliances consume low energy.

    Reply
    • Thien says:

      Thank’s about your comments!
      emon1.voltage(0, 240.074, PhaseV); // Voltage: input pin, calibration, phase_shift
      Where: PhaseV = 1.41; adaptor Power 220/9 therefore calibration = 240.074

      Reply
  10. nabin bera says:

    I’m using 230/2 volt PT and 5/1 amp 15 VA CT to step down voltage and current respectively.and using two 8.2 ohms 2watt resistor in series across CT to get voltage drop corresponding current.this two parameters are fed to arduino analog pin A0 and A1 .so what will be the calibration values and phase sift emonlb.h related program.

    Here is the program
    #include “EmonLib.h” // Include Emon Library
    EnergyMonitor emon1; // Create an instance

    void setup()
    {
    Serial.begin(9600);

    emon1.voltage(2, 234.26, 1.7); // Voltage: input pin, calibration, phase_shift
    emon1.current(1, 111.1); // Current: input pin, calibration.
    }

    void loop()
    {
    emon1.calcVI(20,2000); // Calculate all. No.of crossings, time-out
    emon1.serialprint(); // Print out all variables
    }

    Reply
  11. Tomi Engdahl says:

    Put a Reverse Engineered Power Meter in Your Toolkit
    http://hackaday.com/2016/07/11/put-a-reverse-engineered-power-meter-in-your-toolkit/

    It seems that one can buy cheap power meters online and, well, that’s it. They work just fine, but to use them for anything else (like datalogging or control or…) they need a bit more work. The good news is that [Thomas Scherrer], alias [OZ2CPU], just did that reverse engineering work for us.

    Inside these budget power meters, you’ll find an LCD driver, a power-monitoring chip, and an STM32F030, which is a low-cost ARM Cortex M0 chip that’s fun to play with on its own. [Thomas] traced out the SPI lines that the power-monitoring chip uses to talk to the microcontroller and broke in to snoop on the signals. Once he got an understanding of all the data, tossing an ATmega88 chip on the SPI line lets him exfiltrate it over a convenient asynchronous serial interface.

    Peacefair pzem-021 energy meter hacked SPI to SERIAL out AVR mega 88
    http://webx.dk/oz2cpu/energy-meter/energy-meter.htm

    The Peacefair PZEM-021 energy meter is very popular and super cheap at ebay
    the unit is very good and usefull for all sorts of projects and to be used when working with mains powered electronics.

    Reply
  12. Tomi Engdahl says:

    Root Mean Square
    http://hackaday.com/2016/08/04/root-mean-square/

    Meters

    You can compute RMS voltage using an oscilloscope. However, with a meter, it can be tricky depending on what the meter measures. Most meters that don’t claim to measure RMS, read the average value of the voltage. Some meters measure RMS but only for a sine wave. Older true-RMS meters used thermal or electrodynamic methods to measure the RMS value. However, modern meters are usually adept at measuring RMS, at least for pure AC signals. A very few meters will have an option to measure RMS voltage for signals with a DC component, like an offset.

    According to Fluke, an average-reading meter will read 10% high on a square wave, 40% low on the output of a single-phase diode rectifier, and anywhere from 5% to 30% low on the output of a three-phase diode rectifier. Big difference.

    Reply
  13. Mohit yadav says:

    How can measure ac power for variable load.
    Suppose 2 load is on for “t” sec then, 1 load aaded or removed form it how can we measure power now.
    Which type of sensors we should use. To measure current and voltage.

    Reply
  14. Tomi Engdahl says:

    Decabit: Or The Conspiracy Theory That Wasn’t
    http://hackaday.com/2016/12/06/decabit-or-the-conspiracy-theory-that-wasnt/

    [LDX] first noticed the odd sounds coming out of his ceiling fan, regularly, on the hour and half-hour. Then he noticed that the lights were flickering as well. Figuring something was up, he built a logging power-line monitor to see if he could decode the shadowy signals and figure out what cryptic messages were being transmitted over the power lines. Naturally, he suspected the Illuminati were behind it.

    Even if you’re not prone to flights of fancy, you might want to keep track of your power line because it can serve as an accurate long-run timebase for projects, or because it can tell you something about the overall health of the grid.

    Investigating AC
    Get your tinfoil hat ready!
    https://hackaday.io/project/18619-investigating-ac

    I used a Teensy 3.1 running at 96MHz connected to a voltage divider circuit from a 10v AC adapter, the waveform is shifted up above 0v to capture the full wave.

    The code averages 2 readings before pushing to serial port, use the ‘Serial Plotter’ function

    to view the wave (it is really fast but screenshot-able).

    Reply
  15. Tomi Engdahl says:

    How to Trick Your Electrical Meter By Saving Power
    http://hackaday.com/2017/04/04/how-to-trick-your-electrical-meter-by-saving-power/

    A group of Dutch scientists have been testing out some of today’s “smart” electrical meters to check their accuracy, among other things. Not ones to disappoint, the scientists have found consistently false readings that in some cases are 582% higher than actual energy consumption.

    Their results? Well, “results varied wildly, with some meters reporting errors way above their disclosed range, going from -32% to +582%. Tests with uncommon results were repeated several times

    Millions of Smart Meters May Over-Inflate Readings by up to 600%
    https://www.bleepingcomputer.com/news/hardware/millions-of-smart-meters-may-over-inflate-readings-by-up-to-600-percent/

    Lab tests carried out by Dutch scientists have shown that some of today’s “smart” electrical meters may give out false readings that in some cases can be 582% higher than actual energy consumption.

    The study involved several tests conducted on nine different brands of “smart” meters, also referred to in the industry as “static energy meters.” Researchers also used one electromechanical meter for reference.

    Using a simple test rig, portrayed above, researchers connected the smart meters to various power-consuming appliances found in regular homes, such as energy saving light bulbs, heaters, LED bulbs, and dimmers.

    Problems blamed on smart meter designs

    Researchers blamed all the issues on the design of some smart meters, and, ironically, electrical devices with energy-saving features.

    The latter devices, researchers say, introduced a large amount of noise in electrical current waveforms, which disrupt the smart meter sensors tasked with recording power consumption.

    “The reason for faulty readings appears to be the current sensor,
    and the associated circuitry,”

    Since the research only covered smart meters commonly installed in Dutch homes, researchers say that around 750,000 smart meters deployed around the Netherlands may be giving out false readings.

    Worldwide, the numbers of possibly faulty smart meters could be in the millions, especially after some governments, especially in the EU, have pushed for smart meters to replace classic electromechanical (rotating disk) meters.

    UPDATE [March 27, 2017]: ESMIG, which describes itself as |the European voice of the providers of smart energy solutions” has published research that disputes the findings of the Dutch researchers. Here are their main conclusions :

    The electromagnetic interference phenomena created in the tests of the University of Twente grossly exceed emissions limits allowable under EU regulation for equipment typically used in households.
    These conditions would not be found in any imaginable normal household scenario.
    There is no reason to question smart metering technology.

    Reply
  16. Tomi Engdahl says:

    How Many Watts Are You Using?
    http://hackaday.com/2017/04/22/how-many-watts-are-you-using/

    [Bogdan] built up a simple whole-apartment power monitor from scratch over the weekend, and he’s been nice enough to walk us through the whole procedure, starting with picking up a split-core CT sensor and ending up with a finished project.

    The brains of his project are an ESP8266 module, which means that he needed to adapt the CT sensor to put out a voltage that lies within the chip’s ADC range of 0 V to 3.3 V.

    The microcontroller reads the ADC frequently, does a little math, and you’re done.

    The rest of the code was borrowed from here or there. EmonLib takes care of the math, ArduinoOTA allows him to reflash the firmware over the air, and Blynk takes care of making a nice Android app for visualization.

    Power meter for the entire apartment
    http://bogdan.nimblex.net/diy/2017/04/19/apartment-energy-monitor.html

    Reply
  17. Tomi Engdahl says:

    How to Measure Electrical Power
    http://www.edn.com/design/test-and-measurement/4392053/How-to-Measure-Electrical-Power

    If a product uses power, then power consumption and power quality measurements must be made as part of product design and test. These measurements are essential to optimize product design, comply with standards and provide nameplate information to customers.

    This article will discuss best practices for making these measurements, starting with power measurement basics and proceeding to the types of instruments and associated components typically used to make measurements.

    Reply
  18. Tomi Engdahl says:

    Review: SmartPi – smart meter extension for Raspberry Pi
    https://www.elektormagazine.com/news/review-smartpi-smart-meter-extension-for-raspberry-pi

    What is the SmartPi?
    The SmartPi is an extension board to turn a Raspberry Pi into a smart energy meter. All the parts, including the enclosure, are available separately but it is also possible to buy a fully assembled device (discussed here). The assembled version is built on a Raspberry Pi 3 model B.

    The SmartPi can measure current (up to 100 A, with a hardware modification up to 300 A is possible), voltage (up to 400 V) and frequency on three phases plus neutral and calculate several flavors of power (active, reactive & apparent) and consumption.

    The software is open source and available from GitHub.

    The evaluation SmartPi came with three current sensors.

    The web interface showed three currents, three voltages and three frequencies.

    Conclusion
    The SmartPi is a Raspberry Pi extension board for makers, not a consumer product. The software supporting it is basic — three-phase only, configuration over SSH —, documentation is insufficient. People without RPi or Linux experience will be lost quickly.

    On the other hand, if you are looking for a platform on which to build yourself a smart energy consumption metering system, the SmartPi definitely is a good start.

    Reply
  19. Tomi Engdahl says:

    Nabito Is an Open Meter for EV Charging
    https://blog.hackster.io/nabito-is-an-open-power-meter-for-ev-charging-d221093d26e8

    As electric vehicles become more popular, one problem is where to charge them.

    In order to provide a charging and billing method for these spaces, Systems Distributed has come up with Nabito — the Open Socket.

    The device is powered by an OrangePi single-board computer, and users are pointed to a web interface where they can interact with it via a QR code. Additionally, an Arduino Uno with a current sensor is used to measure power supplied, and a relay switches the outlet on or off.

    Though there are commercial charging options available, this project was implemented for around $60, potentially making this type of metered charging more accessible.

    Nabito – the Open Socket: Smart Meter for EV Charging
    http://www.instructables.com/id/Nabito-the-Open-Socket-Cheap-Smart-Meter-for-EV-Ch/

    Reply
  20. Tomi Engdahl says:

    Dr. Wattson Energy Monitoring Board
    https://blog.hackster.io/dr-wattson-energy-monitoring-board-8b5b6fb6a23a

    The breakout board is equipped with a Microchip MCP39F521 chip to measure the power used on a single outlet. It features an Arduino library to facilitate use with this and similar dev boards, meaning your custom monitoring or even control solution is only a few steps away.

    Reply
  21. Tomi Engdahl says:

    Real-Time Energy Monitor with Arduino and LabVIEW
    https://www.open-electronics.org/real-time-energy-monitor-with-arduino-and-labview/

    This is a simple power meter to analize the current consuming in a house using the led indicator of a house energy meter.

    Reading the red led of a home energy counters the system detects the corrent consumption in a house.

    It is a noninvasive method

    The system consists of two parts: the Arduino board that detects the led pulses and sends the data via the XBee module, and a PC that recive the data through a USB/Xbee module and processes the data with LabVIEW so you can prepare and study the consumption in a very instant.
    Arduino sends two datas to the PC:
    1 – Real time datas
    2 – Average consumption measured in a time of 5 minutes.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

*

*