Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

How to measure AC Real Power and Apparent Power with Arduino?

When talking about Power in Alternating Current (AC), it is more complicated as it involves waveform and phase difference. Basically there are 3 terms for AC Power: the Apparent Power, Active Power and Reactive Power

Apparent Power is referring to total power in an AC circuit which is including dissipated and absorbed power. This term is important for power generator and cable design as to make sure all cabling and power can support all kind of loads. Apparent Power can be calculated by multiply root-mean-square (RMS) voltage value and (RMS) current value.

The second term is the Active / Real Power. It is the actual power consumed by the load. Active Power can be calculated by averaging the product of instantaneous Voltage and Current waveform values. The third term is the Reactive Power which is referring to power that is wasted and not being used by the load as useful work due to phase difference between voltage and current waveforms. When Reactive Power is added up with Active Power, it will give you the Apparent Power. 

We will use Arduino to measure Apparent Power and Real Power. In order to measure AC Power, we need to use 2 separate sensors which are Single Phase AC Voltage Module and AC Current Module. Each module can be fit into Arduino analog input pin. For Arduino UNO, there are 6 analog input pins (A0-A5). Arduino NANO has 8 pins while Arduino MEGA has 16 input pins. The analog input pins will map input voltages between 0 and 5V into integer values between 0 and 1023 with resolution of 4.9mV per unit (5.00V / 1024 units). 

 

Arduino UNO (compatible board)

If you still not yet own an Arduino Micro-controller Board, you can get it cheap at our affiliate link here !!!

Warning ! You are now dealing with high power source ! We assumed that you have the basic electrical knowledge and know what you are dealing with. You may need guidance from experienced guys if you are new to electrical work. Safety and Precaution must be always have in mind. We shall not be responsible for anything happening to you.

You can measure AC Voltage such as 110Vac, 220Vac and 240Vac by using the market-available Single Phase AC Voltage module. It can detect the instantaneous waveform for voltage value. The signal can be used with AC current sensor to determine further useful information such as power factor,  direction of current flow, real power and apparent power. 

You may need to pre-calibrate the module. By using the trimpot potentiometer, The amplitude of voltage waveform can be adjusted. It is recommend to use a second meter that reads RMS voltage for easier calibration. For more information about this module and how to calibrate, kindly go to my previous post about this AC Voltage Module here !!!

Single Phase AC Voltage Module

This module is equipped with ZMPT101B high-precision voltage transformer and op amp circuit. It can measures AC voltage within 250V. The corresponding output signal can be adjusted using the trimmer potentiometer. You can grab this module at our affiliate link here !!!

The second sensor that we want to talk about is the Current Sensor Module. The widely used current sensor that compatible with Arduino is the ACS712 Current Sensor Module. It utilizing hall-effect phenomenon which voltage is produced from the movement of current within the region of magnetic field. The voltage produced by hall effect is directly proportional to the applied current making it suitable to estimate the applied current from the voltage sensed.

The standard ACS712 Current Sensor Module rated at 5A, 20A and 30A which are suitable to most applications. You may get them by our affiliate link  here !!! The 5A module has the resolution of 185mV/ampere, 20A module has 100mV/ampere while 30A module has the resolution of 66mV/ampere.

These modules require direct contact which I think is a major drawback. It has to be connected in series to the measured value.The wiring of the existing system need to be altered in order to fit the module into the existing system. 

If you are inexperience guy dealing with AC power, I personally don’t recommend this module. This module is acceptable for DC low voltage but it can be dangerous in AC power if care is not taking seriously

Fortunately, there is also a hall-effect sensor type with split core transformer type (as picture on left). It is the Hall-Effect Split-Core Sensor HSTS016L module. The model ranges from 10A up to 200A. With split core current sensor type, not alteration on the existing system required. You can get it via our affiliate link here !!! The output voltage of this sensor is 2.5V +/- 0.625V with decent accuracy. I highly recommend this sensor for measuring AC current.

For demo purpose, I will be using this Hall-Effect Split Core Sensor that does not require to electrically in contact with existing wiring system which is definitely a safer way. If you need more information about these 2 types of AC Current sensors, kindly have a look on my previous post on AC module sensor here !!!

Signals detected by Arduino are in analog values. Below are the signals being detected by the voltage (Blue) and current sensor (Red). You can copy the below code to try your own. You can see the waveform in Serial Plotter. Connect Voltage module in A2 pin of Arduino while Current module in A3 pin. The magnitude of voltage (Blue wave) can be calibrate by AC voltage module using the on-board trimpot and compared with reference RMS volt meter. For a pure sine wave like this, the RMS value in voltmeter multiply by square root 2 is equal to peak voltage (magnitude from middle of oscillation). The magnitude of current wave (Red wave) is subject to your applied load. You may altered the Arduino code to increase its magnitude for display purpose. The AC wave pattern might not be 100% smooth like voltage wave and it is subject to the load on how the current being drawn. The phase shift or phase difference between voltage and current also subject to applied load.

float vAnalogRead;
float iAnalogRead;
int voltageInputPin = A2; // Analog input pin for voltage sensor
int currentInputPin = A3; // Analog input pin for current sensor
int currentOffset = 0; // key in value for current offset setting
int voltageOffset = 0; // key in value for voltage offset setting
float virtualMagnification = 5; // to increase the magnification of current value in display

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

void loop()
{
vAnalogRead = analogRead(voltageInputPin) - 512 + voltageOffset;
iAnalogRead = virtualMagnification*(analogRead(currentInputPin) - 512) + currentOffset;

Serial.print(vAnalogRead);
Serial.print(" ");
Serial.println(iAnalogRead);
delay(300);

}

The middle point for both waves should be technically at 512 (analog value) and it fluctuates within 0 to 1023 by default. However, I have amended the code so that the waves are oscillate at middle point 0 value between 512 and -512 for easier calculation later. Before going further, calibration are needed before the measurement as some modules might have different deviation error. You may go to individual sensor post for more info.

How the signal being processed

We need to determine the 2 AC Power values, the RMS AC Power and Active AC Power value. Before going further, let us begin with initial calibration for both sensors. We need to minimize any potential deviation error or inaccuracy as much as possible. The sensors are very sensitive, make sure to use tight connectors and cable terminals. 

We need 2-time calibration for each module sensor. Both calibration need to be done during no current & voltage measured. The first calibration is making sure when no voltage or current measured, it shows exactly at 0 point. It is an analog value calibration. Some modules might not showing exactly at analog value 512 (I have shifted it to 0 point using Arduino code for easy understanding) when no value is detected. We need to add an offset value for this to adjust it back to origin when no value detected.

The second calibration is to further eliminate false signal value during RMS calculation. Even after the first calibration is made, there are still some minor ghost or electrical noise even when no voltage and current are measured. We have to add another offset to make it to zero value at the final stage for display. This second calibration must be done only after the first calibration take into effect. Both calibrations can be done manually (the harder way) or automatically by pressing the SELECT Button in the LCD Display Shield and wait for about 5 seconds. Of cause you need to have the shield in order to work. You may purchase at our affiliate link here !!!

The Root Mean Square Power Calculation

The RMS AC Power is the product of RMS AC Voltage and RMS AC Current values. The RMS AC Voltage and RMS AC Current are calculate separately. By finding RMS AC Power, RMS Voltage and RMS current are indirectly to be found. 

Our code is designed to calculate or display a value which is derived from averaging 1000 samples per set. I found an issue that shorter sample recording time (shorter than 1 milli second for 1 sample reading) might delay the processing time which made display time to slow down. I have set each sample is recorded every 1 milli second (0.001 second). In other words the total complete set is equivalent to 50 waves with each wave is divided into 20 sections or readings (for 50Hz). Technically it should last 1 second for 1 set of reading. It is also suitable for 60Hz system as the measurement time for 1 set of reading is 1 second.

Each single sample analog value is being squared initially and once the 1000 sample values are accumulated, the average value from the 1000 samples is then being square-rooted in order to come out the RMS analog value (for 1 sensor). Convert the RMS analog value into measured voltage or current value.  Do the similar calculation separately for the other sensor and multiply both RMS values to become RMS AC Power. Below is an example how the code works. I take a quarter of wave as an example.

In the conversion of RMS voltage, I multiply RMS analog value by 2. During calibration of this module, I have reduced the wave amplitude by half (that’s why in code need to multiply by 2) because the voltage wave started to distort or cut-off near 250Vac which may give inaccuracy in measurement. You can increase the supply voltage to the module to eliminate the cut-off effect (based on the module manual) but I think I going to leave it that way as I am not going to add additional voltage supply. As for conversion of RMS current, the mV/A is the rating of the current sensor and I just leave it as the unit symbol. 1023 is the analog value conversion while 5000 is the voltage value in mV. For more information about this, you can go to my post about AC sensor here.

The Instantaneous Power Calculation

Active or Real Power is simpler compared to RMS Power. It is the average value of all the multiplications of instantaneous voltage and instantaneous current value. Both voltage and current analog values are first converted into measured voltage and current value. The voltage is then multiplied by its instantaneous current value and become a sample reading. 

Similar to RMS power method, the Active Power is also derived from averaging 1000 samples per set. Each sample is recorded every milli second, in other words the total complete set is equivalent to 50 waves with each wave is divided into 20 sections or readings (for 50Hz). 

I recommend you to add a 16X2 LCD Display Shield which can be directly fit on to the top of the Arduino board without the need of extra wiring for the LCD Display. Without the LCD Display, you can only monitor the measured current value on PC via Serial Monitor. You can get the LCD Display board at our affiliate link here !!!. 

The good news is you do not need to manually calibrate the offset settings if you got the LCD Display Shield with you. Below we have attached the code that utilizes the button function that could automatically calibrate by itself when you pressed the SELECT Button. You may download from the end of this page below.  

UNI-T Multimeter is a good quality with decent price. We have been using UNI-T multimeter for years has not been any issue. We were using UT33C model. It can measure RMS Voltage. You can get yours at our affiliate link here !!!

Peacefair PZEM-021 Energy meter is an compact AC energy meter that measures Voltage, Current, Power and Energy. Peacefair has a lot of model to measure different current requirement from 20A up to 100A. You can get it at our affiliate link here !!!   

Hardware Connection

You may need to find a way to fit the AC sensor cable into the Arduino pin. The sensor cable that I purchased came with pre-soldered at the tip, making it easy to go into the Arduino pins. Open the Split Core current module and there will show an arrow symbol which signify the way of current flow. Once everything ready, make sure the split core is closed tightly.  

Make sure underneath Voltage sensor does not have exposed conductor or metal plates which may cause you accidental short circuit at the terminal of the module. You need to get the low current fast blown fuse for voltage sensor cable while MCB or fuse of sufficient current rating based on your load to protect against any potential short circuit and fire risk.

In order to connect wiring between Arduino board and module, you need the dupont line cables male to female. You can get it at our affiliate link here !!!

You need the connector that can secure cables and isolate from accidental touch. Get the fast connector at our affiliate link here !!!

You need a fuse to protect your safety and cables!! You can get it here !!

Screw Shield / Expansion Shield

When there are a lot of wiring around especially more than 1 sensor, sharing pins will be difficult as existing pins (ground and 5V) are limited. This shield provides a lot of convenient terminals for each of the input and output pins. The shield can be mounted directly on top of the Arduino Uno board or in between the shields which made it very convenient to use. You can get it at our affiliate link here !!!

Datalogger Shield

If you plan to record the data in a proper way, you may consider this Datalogger Shield. It allows your arduino to record your data in SD Card. Datalogger shield is often installed together with LCD Display shield. Please find it at our affiliate link here !!! For more about this Datalogger Shield, kindly visit our post here.

Software Codes

The final step would be adding source code onto Arduino board. I assume you have installed the Arduino Software. If you have not installed the software, the link here can bring you to the official download site. Once you have downloaded the software, you may download the code file (.ino) for this application below (right click save link). 

There is 2 source codes file attached which are source code with and without LCD display shield function. If you don’t have LCD Display shield with you, kindly choose the code that is without LCD Display Shield but you have to manually calibrate and key in the 2 offset values for both sensors. However, I still highly recommend that you get a LCD Display Shield.

With LCD Display Shield, once the code is uploaded to the Arduino board, the current value will be shown on the LCD Display. We have added the auto calibrate function, once the SELECT button is pressed, the value returns to exact zero point. You may have to wait about 5 to 8 seconds long until all values are re-calibrated. If first press is not satisfied, you may repeat by pressing it again.

I will not display the code here because it is long. You can download the .ino file to see for your own. Almost all code lines are with explanation. 

Calibration Process 

Once the code is uploaded to the Arduino, if you got LCD display shield attached, you will see the current, voltage, active power and apparent power value. Make sure to turn OFF the AC voltage source that you are measuring. Press the SELECT button of the LCD Display Shield and wait for 5 seconds. It should go to 0 volt. Same for the AC current showing 0A. For people out there that do not have the LCD Display Shield, you can manually offset by entering the offset value in the code and re-upload again. There are 2 offset values (voltageOffset1 & voltageOffset2) need to calibrate. Do the same for AC current module for 2 offset values (currentOffset1 & currentOffset2) and finally is the powerOffset.

Trimpot Potentiometer AC Voltage Adjustment 

This setting is to adjust the magnitude of AC voltage wave. Unlike AC current, we calculate and get the expected current but for AC voltage, we need a reference voltage for adjustment.

Once calibration done, you should see the value 0 volt when no voltage is detected. Now, turn on the Voltage source and turn on the reference voltage reader (the multimeter or the energy meter). Compare both of the voltage value. Turn the trimmer potentiometer (trimpot) using a small screwdriver to reduce or increase the voltage value detected by Arduino. You need to turn the trimpot until the voltage shown in the LCD display Shield or Serial Monitor is the same as the voltage reference value in the volt or energy meter. And Congratulations, it’s Done !!!!

If you really read through the codes, we actually has reduced the potential wave amplitude by half (in formula is times 2). 

RMSVoltageMean = (sqrt(voltageMean))*2;

This is why when monitoring voltage is applied, the value measured is high and you need to reduce it. Full wave amplitude (x 1) get distorted when near to 250V which made us having this choice to overcome the distortion problem.

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

Result – In Serial Monitor

Result – In LCD Display

For Arduino Code Files, Remember to Right Click > Save Link As … You may alter the internal code as you wish. Happy coding !! 

AC Power.ino
AP Power with LCD Disply.ino