Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(28) Measuring grid power consumption with an ACS712 Hall effect sensor connected to an Arduino

Summary
Here we describe the design, wiring and the actual building of a 240V AC grid power meter. The main components of the power meter are an ACS712 sensor, an Arduino Nano, and a 20×4 LCD display. This power meter can be used to measure household appliances power consumption up to 1,000W. The ACS712 also measures reverse currents, for instance AC output of grid tie inverters that convert solar panel power into grid power. The design is as prudent as possible because we are interfacing dangerous grid AC and the 5V DC of the Arduino microcontroller environment. Be careful!

Introduction
Every now and then I need a meter that displays how much grid power (load, expressed in Watts) is being consumed by an appliance (say, a computer) or is being produced (say, the AC output of a grid-tie inverter connected to a solar panel). Of course one can buy ready-to go power meters that are placed in between the appliance power plug and a wall outlet, but usually the description of such a device is not clear on the issue whether it also works with appliances that feed power back into the grid. In the past I purchased several power meters, but these either died soon, had displays that were too small or that stayed blank when connected to a solar power inverter.

On the market are several Arduino compatible AC power sensors that exploit the Hall effect. I decided to buy, test and implement a device containing an Allegro ACS712 sensor (figure 1). The output of this board is a voltage in proportion to the amount of current that runs through a wire. Such a current produces a magnetic field whose strength (proportional to the current) is measured by the ACS712. The advantage of the ACS712 is that both DC and AC currents can be measured and that the direction in which the current flows does not matter. These sensors are available in three ranges: 5A, 20A and 30A. Because I wanted to measure 240V AC grid power in the range of 0-500 W, a 5A rated ACS712 would be sufficient.

 

figure 1. ACS712 current sensor and how it connects with a power cord. Be aware that this device represents an interface between 240 V grid AC power and the 5V Arduino environment. Be careful!

At this point a warning should be taken into consideration. Arduinos are devices working with very safe, 5V DC. Connecting one such device to a sensor that measures grid power is potentially hazardous. I am absolutely not a fan of connecting an Arduino with whatever grid power device. The thought of connecting my computer’s usb port via an Arduino to a sensor directly exposed to 240V AC makes me shiver. In spite of the manufacturer’s specification that mentions a 2,100 VRMS separation between the AC side and the DC output of its ACS712 design I took all necessary precautions to potentially protect myself, my environment and my computer against the possibility of direct exposure to 240V grid AC. For example, the ACS712 is mounted on its own separate board, apart from the board carrying the Arduino. Both boards are positioned as much as possible away from each other, and they are mounted inside a grid-power safe container. It is not (and it should not be!) possible to inadvertently touch AC 240V carrying parts when the power meter is in operation. Programming of the Arduino was always done while the ACS712 was disconnected from grid power. All 240W AC wiring is fastened inside the box and positioned as far as possible way from Arduino wiring.

Hall effect
Edwin Hall (1855-1938) reported in 1879 in his thesis on the effect that made him immortal: the Hall effect. A tiny voltage builds up in a thin sheet of semiconductor placed perpendicular to a power conducting wire. Modern electronics further builds on this principle and supplies us with reasonably accurate, economically priced current detectors. The ACS712 is one example. This sensor is capable of detecting DC and AC currents. Output is analog (a voltage) in proportion with the current that flows through the device.

Electronics and supplies
1x Arduino Nano microcontroller, 1x ACS712 current sensor, 1x 20×4 LCD display, 1x 70×50 mm soldering board, 1x 80×60 mm soldering board, 2x 15-pin female pin header, 1x 16-pin female pin header, 1x 10k trim potentiometer, 2x 220 Ω resistor, wire, 4x M3 nylon spacer length 6mm, 1x M3 nylon spacer length 10mm 1x PVC container with acrylic lid, 90 x 150 mm, 1x 6-channel luster terminal, a 3-prong power cord, Dupont jumper wires, 1x green led, 1x 3-screw connector, 1x 5V power usb supply, 1 usb cord to connect the usb power supply to the Arduino Nano.

The ACS712 sensor miniboard
A green high-voltage connector is mounted on one end of the sensor board while the opposite end holds three pins, marked VCC, OUT and GND.
The insulation of a household power cord is carefully removed over several centimeters and the three wires exposed: blue, brown and yellow-green. Blue and brown carry current power while the yellow-green wire is earth. Only one wire (here the brown wire) is cut, and connected to the high voltage connector. To keep the sensor separate from the Arduino/display parts (safety matter) I mounted the ACS712 with two nylon bolts on its own 70×50 board on which also a 6-channel luster is attached to connect the high-voltage AC wires with each other and the ACS712 (figure 2). The entire assembly is fastened with screws onto the bottom of the container. The electric wires are for safety reasons attached to the bottom of the container with a small clamp, just on the inside of the opening in the container wall where the cords leave the container.

figure 2. ACS712 mounted on its own board, inside the container

Arduino Nano and LCD display
Because of the compact space and also because of safety considerations I decided to mount the microcontroller board on the back of a 80×60 two-sided soldering board while the LCD display was planned on the front of the board. For this purpose I soldered two 15-pin female pin headers on the back of the board, positioned such to hold the pins of an Arduino Nano. On the front of the board I soldered the 16-pin female pin header whose job it is to hold the pins of the LCD display. As the LCD display is a classical 16-pin parallel device the wiring is with 12 wires (wiring scheme shown in figure 3). The wiper of a 10k trim pot meter is in this ‘traditional’ wiring connected to pin 3 of the LCD pin header. In the wiring a green indicator led is included, connected to pin D6 of the Nano. This led informs the user that the power meter is functional, i.e. is plugged in some 240V AC outlet.

figure 3. Wiring diagram of the Arduino, LCD display and the connections with the ACS712. Note that the design is such that both the Arduino Nano and the LCD display will be mounted on their respective pin headers.

In the wiring diagram of figure 3 it is shown that the ACS712 is connected to pin A0 of the Nano. The ACS712 is an analog device (see below for the implications of this with AC currents).

Assemblage
Figure 4 shows the [still] open container with both boards mounted and the Arduino board connected with three wires to the ACS712 board; red (5V), dark blue (GND) and white (output signal of the ACS712). The LCD display is attached with four nylon spacers to the inside of the transparent acrylic lid of the container; the LCD display is additionally fastened to the board with one 10 mm spacer (this to prevent gravity to loosen the LCD display from its pin header). Thus, the LCD display keeps the board with the Arduino in position.
During the design phase the decision was made not to mount a 5V usb power supply in the container. This made it necessary to run a 5V usb power cord from the outside into the container. This is the black cord next to the two white power cords that can be seen in figure 5.

figure 4. Box with mounted boards: the ACS712 (right) and Nano/LCD display (left). The only physical connections between both boards are the 5V power supply, GND and the data wire.

After the transparent lid was fastened onto the body of the container software instructions were uploaded and the power meter connected to a test appliance and a power outlet.

Basics of operation of a ACS712
In rest, that is when the Arduino and the ACS712 are powered on; VCC = 5V while the AC appliance cord is unplugged, the voltage on the ACS712’s OUT pin is 2.5V. As soon as the appliance cord is plugged in and AC current starts flowing through the ACS712 the OUT voltage will change, that is: decrease at 185 mV per Amp current.
Under a DC load condition the current consumed by an appliance, once established, does not change. However, under a AC load condition the 240V voltage and current both change at a frequency of 50 (Europe, Australia) to 60 cycles (US, Western Japan) per second. Voltage goes up and down according to a sine function while current goes back and forth. This means that with the ACS712 we must measure the output voltage associated with peak current.
As a consequence of AC operation the voltage measured at the OUT pin will go up and down between the tops and bottoms of the 50Hz grid sine voltage curve.
The following steps need to be taken in the Arduino sketch to determine top and bottom values (sketch adapted from one published by Henry’s Bench (http://henrysbench.capnfatz.com)):

1. Find top peak voltage
2. Find bottom peak voltage
3. Calculate the difference between top and bottom peak voltages
4. Divide the outcome by two and mutiply with 0.707 to calculate rms volts

The A0 pin on the Arduino translates an applied voltage into an number between 0 and 1024. This taken into consideration produces the following sketch that was adapted from the one to be found at Henry’s Bench (http://henrysbench.capnfatz.com)) and at http://www.circuits4you.com

// Hall_sensor_LCD_display
// measuring 240V AC current using ACS712
// The ACS712 works with grid voltage AC so be extremely careful !
// source – http://www.circuits4you.com – also at henry’s bench.capnfatz.com
//
// floris wouterlood – June 10, 2019
// open source

#include <LiquidCrystal.h>

const int sensorIn = A0;
int mVperAmp = 185; // 5A version SC712 – use 100 for 20A Module or 66 for 30A Module
int Watt = 0;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
pinMode(6, OUTPUT);
Serial.begin (9600);
Serial.println (“Hall sensor”);

lcd.begin (20, 4); // set up the LCD – number of columns, rows
lcd.clear ();
lcd.setCursor (0, 0);
lcd.print (“Hall sensor”);

digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
}

void loop() {

Serial.println (“”);

Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root 2 is 0.707 – dealing with sine
AmpsRMS = (VRMS * 1000)/mVperAmp;

Serial.print(AmpsRMS);
Serial.print(” Amps RMS — “);
Watt = (AmpsRMS*240/1.3);
// note: 1.3 is my own empirically established calibration factor
// as the voltage measured at A0 depends on the lenght of the OUT-to-A0 wire
// 240 is the mean AC grid power voltage – this parameter fluctuates locally
Serial.print(Watt);
Serial.println(” W”);
lcd_control ();

}

// ***************** subroutines **************
float getVPP()
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here

uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}

// Subtract min from max
result = ((maxValue – minValue) * 5.0)/1024.0;

return result;
}

void lcd_control (){
lcd.setCursor (5, 2);
lcd.print (Watt);
lcd.print (” watt “);
}

figure 5. The finished box with all its components: working!

Calibration and conclusion
It makes good sense to calibrate the ACS712 AC power meter. This can be done by testing power consumption of appliances with a known Wattage. I have several antique incandescent light bulbs available for this purpose (20-40-60W) and I own several electric gadgets with known, fixed power consumption. Biggest problem is here that the values printed on light bulbs are not exact and stem from the era that grid voltage was 220V. Also the exact AC grid voltage at the power outlets in my home fluctuates between 235V and 243V (the solar panels on the roof of my home aggravate these fluctuations). I also found empirically that the length of the wire between the ACS712 OUT pin and the Arduino A0 pin does matter. Varying the wire length was achieved by chaining several Dupont wires. Arduino output was also tested with a commercial power in between the light bulbs and the wall outlet. The final ‘empirical’ calibration factor included in the above sketch is a division of the measured Watts by a constant (1.3). So, don’t assume that you will measure with this grid power consumption box the absolute amount of power consumption in Watts. You will at least get a nice approximation.

Sketch
The last version of the sketch Hall_sensor_LCD_display.ino can be found here.