PowerBox: The Safe AC Power Meter

By: Cliff Jao (cj72) and Xi Guo (xg37)

Introduction

We designed a device that measures and graphs various aspects of AC power and acts as a computer-controlled remote switch.

With the recent push for green energy and environmental friendliness, more and more people are concerned about their personal daily power usage. We developed a microcontroller-based device to measure AC power. The user will be able to insert the device between the wall socket and the device under test (DUT) to measure the amount of AC power being used. With this device, environmentally conscious people can use the C# application that we developed to monitor their energy usage with real time graphs and data display.

Our device calculates and reports the following parameters in real time:

  • Real power
  • Apparent power
  • Power factor
  • RMS Voltage
  • RMS Current
  • Frequency
  • Energy usage (Kilowatt-Hours)

In addition to measurements, our device also has a remote switch that allows the computer to turn the power on or off and set wattage limits for auto shutdown.

DISCLAIMER: AC POWER CAN BE EXTREMELY DANGEROUS UNLESS YOU KNOW WHAT YOU ARE DOING. PLEASE CONSULT A PROFESSIONAL AND REFER TO THE SAFETY SECTION FOR MORE INFORMATION.

High Level Design

Rationale

The primary inspiration for our project was the EnerJar, developed by Matt Meshulam and Zach Dwiel at Washington University in St. Louis. The Kill A Watt is a similar commercially available product. We improved upon the EnerJar by making it safer and simplifying the design significantly. The EnerJar is not optically isolated and therefore is potentially very dangerous. We extended upon the Kill A Watt by developing a computer interface to provide real time graphs and remote switching.

Background Math

In an AC circuit voltage, current, and power are defined as the following:

When the load is purely resistive, voltage and current are in phase. When the load is either inductive or capacitive, voltage and current are out of phase.

Using several trigonometric identities, the power can be expressed as:

The average (real) power, P. Real power is the energy that flows to the load. It is what the electric company bills home users for. It can then be written as:

Reactive power, Q, is the energy that flows back and forth in an inductive or capacitive load. On average, no reactive power is consumed. It can be written as:

Together, real power and reactive power form complex power. This is the actual power that the electric company is supplying. It can be written as:

The magnitude of complex power is called apparent power, |S|, in units Volts-Ampere.

The power factor, PF, a measure of efficiency. It is defined as:

Logical Structure

The block diagram is shown in Figure 1. Our device sits between the mains line and the device under test. Current measurements occur on the neutral line. The relay is switched on the AC live line. A voltage divider is used to step down the voltage to the correct levels. All signals are optically isolated before reaching the MCU.

Figure 1. System block diagram.

Hardware / Software Tradeoffs

Hardware was used to bring voltage levels into the appropriate range for the Mega32 internal ADC. In software, the Mega32 was used to sample the voltage levels at a frequency of 1 KHz and perform the appropriate calculations in real time. The software also handles interacting with the computer and controlling the relay switch.

The Mega32 ADC channels are multiplexed and not simultaneous. An external simultaneous ADC chip could have been used to sample current and voltage at the same time, resulting in slightly more accuracy. However, to save costs, the extra hardware was not used. The internal Mega32 ADC was switched in software to take one sample after the other. This was deemed good enough for our purposes.

Fixed point (20:12) was used instead of floating point to speed up multiplication and division operations. The Mega32 hardware performed floating point multiplication operations much slower than integer multiplication operations. Therefore, some accuracy was traded for speed.

Standards

The sockets used in most American homes adhere to the NEMA 5-15 standard, set by the National Electrical Manufacturers Association. It is rated for 15 A / 125 V at 60 Hz.

Figure 2. NEMA 5-15 Socket. Source: Wikipedia.

Figure 1 shows the standard socket. The left wider flat blade is neutral, and the right flat blade is AC live. Neutral is the zero voltage reference point. The voltage on the live contact is measured with respect to the neutral contact. AC live oscillates between roughly +/- 170 V about that point. The circular hole is earth ground. It is used as a safety mechanism for device with metal casings, so that it does not produce a dangerous electric shock when people touch the case.

The standard IEC connector defined by International Electrotechnical Commission specification IEC 60320 is commonly used to connect mains AC power to devices such as computer. One of these was salvage for use in our project. The one we used was a 3-conductor standard rated at 16 A.

Patents, Copyrights, and Trademarks

There are quite a few commercially available professional AC power measurement devices on the market. Kill A Watt is a registered trademark. The name PowerBox does not knowingly infringe on any trademarks. The design of our project does not knowingly infringe on any patents.

Hardware Design

Voltage Measurement

To measure voltage, the general idea is to use a very large voltage divider to divide the 170 V peak-to-peak signal down to level which can be sampled by the ADC. Using a 1001:1 voltage divider (with 1 MΩ and 1 kΩ), as shown in the Appendix B schematic, 170 V peak-to-peak is divided down to 0.17 V peak-to-peak. A very large resistor (1 MΩ) was used in the divider to limit the current between AC live and neutral. Assuming a 170 V drop, only 0.17 mA flows through the 1 MΩ resistor, dissipating 0.03 W, well within the power ratings of the resistor. To calculate the line voltage from the voltage divider output, the following equation can be used:

(Eq. 1)

Current Measurement

To measure current, the general idea is to break the neutral line and insert a very small current-sensing resistor (0.2 Ω). This would create a small voltage difference across the resistor. Since we know the voltage drop and the resistor value, we can mathematically determine the current through the neutral line. Since the resistance is very small, very little power is dissipated through it. We carefully checked the ratings of this and other circuit components for power ratings. The resistor is rated for 3 W. We expected a 200 W computer to draw about 1.7 A. This results in a voltage drop of 0.34 V and a power dissipation of 0.58 W. To calculate the line current from the current-sensing resistor voltage drop, the following equation can be used:

(Eq. 2)

Isolation, Amplification, and Offset

In our first design iteration, we referenced the EnerJar design. We wanted to use an opamp bias the AC signal by 2.5 V in order to make the signal positive. The signal was to be put through a precision amplifier to convert it into the proper range for the ADC. This scheme would require that all of the grounds of each circuit element to be connected, which was deemed very dangerous. Therefore, after some analysis, it was rejected for safety reasons.

In our second design iteration, we added optoisolators to completely isolate the dangerous high voltage circuit from the MCU. Since the optoisolators stocked in lab were by nature digital devices, we wanted to use a PWM chip to convert the analog signal into pulses to send across the optoisolator. We also wanted to use precision amplifiers to convert the signal into the correct range. This would have been a workable solution, but we wanted to find a more efficient method that required fewer ICs.

In our third design iteration, a breakthrough occurred when we found the Avago Technologies HCPL-7520 linear optoisolator. This device has a linear transfer characteristics curve for input range -256 mV to 256 mV. The input is differential and the output is scaled to Vref. The gain is Vref / 0.512. This single chip allows us to bias the signal to Vref / 2, amplify it, and isolate it. It results in a solution that is simpler and cheaper to manufacture. The HCPL-7520 was used for both voltage and current measurements. Vref was set to Vcc (5 V) of the MCU. To calculate the input voltage of the HCPL-7520 from the output voltage, the following equation can be used:

(Eq. 3)

Combining the voltage divider (Eq. 1) and current-sensing (Eq. 2) equations with (Eq. 3), we get:

(Eq. 4)

(Eq. 5)

The voltage output was connected to Port A.0 of the MCU. The current output was connected to Port A.1.

Power Switching

To do remote power switching, the Sharp S216S02 solid state relay was used. This relay is optically isolated and rated for 240 VAC and 16 A. We used this relay to switch the AC live line. A 330 Ω and a 1 kΩ resistor were placed in parallel (248 Ω series resistance) to limit the current of the light emitting diode in the relay. Port D.3 of the MCU was used as the output to control the relay.

Supply

On the high voltage side, the optoisolators were powered using a salvaged 12 V AC-DC unregulated power supply. We regulated it down to 5 V with a regulator. On the low voltage side, the optoisolators were powered using Vdd and Gnd of the MCU.

Physical Design

For physical design, a solder board was used to place our circuit. The left side of the board contained dangerous high voltage elements. The right side contained MCU inputs. The 2 sides were completely isolated. Cables from inside the IEC connector were used to connect AC lines. The IEC connector head was used to connect our device to the wall. Devices under test were connected via a wall socket. A salvaged serial cable was soldered to the port instead of an RS232 connector. Everything was contained inside a plastic wall box for safety. Please consult the Safety Section for how our device was isolated.

Figure 3. Circuit board.

Figure 4. Open assembly.

Figure 5. Closed assembly.

Software Design

Timer 0 prescalar was set to divide by 64. The TIM0_COMP interrupt was set to run every 250 ticks. The ISR updates 3 virtual timers for 3 different tasks. It is also responsible for updating the clock time, which is in the unix time format (seconds since January 1, 1970 UTC).

Task 1 runs every 1 sec. It simply blinks the LED on Port D.2 to verify that the MCU is running correctly. Task 2, which runs every 1 ms, is responsible for ADC input and all power calculations. It also handles data output to the serial port. Task 3, which also runs every 1 ms, is responsible for reading input and processing commands from the serial port.

ADC Sampling

To get accurate power calculations, we used Task 2 to get ADC samples at a frequency of 1 KHz. To sample 2 channels, a conversion on A.0 (voltage) was started by setting ADMUX.0 to 0 and ADCSR.6 to 1. A while loop was then used to wait for the conversion to finish. ADCH was then stored to a variable. A second conversion on A.1 (current) was immediately started by setting ADMUX.0 to 1 and ADCSR.6 to 1. This result from ADCH was also stored to another variable.

Aref of the ADC was set to Vcc, and the ADC was set to left adjust the result, giving us 8 bits of accuracy if we only read ADCH. Therefore, to get the input, the following calculation is done:

(Eq. 6)

Current and Voltage Calculations

To ensure that per-sample floating point math operations were done quickly enough, 20:12 fixed point was used, giving 0.00024 (1 / 4096) of decimal accuracy. The values from ADCH were immediately converted to fixed point.

To derive the actual line voltage and current values, (Eq. 4) and (Eq. 5) were combined with (Eq. 6) to yield:

(Eq. 7)

(Eq. 8)

Squared current and voltage values are summed up over 1000 samples. This summation is then divided by 1000 every second to get the RMS values. There are obviously some simplifications that can occur to optimize the number of operations, which will be detailed later. Note that Vref completely cancels out.

Power Calculations

To calculate real power, voltage and current are multiplied during every sample and summed up. After 1000 samples are taken, the power summation is divided by 1000 to get the average power over 1 second. The following equation describes the operation:

To calculate apparent power, Vrms and Irms should be multiplied together every second. However, this is optimized as shown in the next section.

Power factor was calculated by dividing average power from apparent power.

Optimizations and Fixed Point Overflow Workaround

To optimize per-sample calculations, only the voltages sampled by the MCU are stored, and not the actual current and voltage values. This was also required to prevent fixed point overflow. The conversion to actual values is done only once a second. We will attempt to illustrate the optimization.

After refactoring (Eq. 7) and (Eq. 8), we get the following:

Let us define the following:

Therefore, to get the RMS values, we then do the following:

The average power is then described as:

This way, the factors in front of the summation can only be multplied once per second to prevent fixed point overflow and optimize operations.

Frequency Calculation

To calculate frequency, the time of individual 0 voltage crossings are stored. The differences are calculated. These frequency samples were averaged over 1 second to reduce error.

Energy Calculation

To calculate energy used, the average power every second was converted into kilowatt-hours (divide by 3600) and summed.

Relay Control

If shutdown is set to 1, Port D.3 is turned off. If the calculated power is greater than the limit (threshold), the relay is also turned off by setting shutdown to 1. A user must manually turn the relay on again after the limit has been exceeded.

UART Communication

Serial input and output were done concurrently by using the USART_RXC and USART_DRE interrupts to ensure that it was non-blocking. UART functions from ECE 476 were incorporated. Task 2 uses the puts_init function to asynchronously output the data once every second. The format was as follows:

unix time>:P<power>,S<apparent power>,I<current>,V<voltage>,F<frequency>,

... E<energy>,R<power factor

Task 3 used the gets_init function to asynchronously read UART input and use various string operations to process the following commands:

TIME <unix time (long)> - sets the clock time of the device

RELAY <0/1> - turns relay on or off

THRESHOLD <watts> - sets the wattage limit

C# Graphing and Control Application

A C# Windows application was written to communicate with the MCU. Please note that all calculations were done on the MCU. The C# application was used to display the power graph and report values. It was also used as a GUI to issue device commands. The ZedGraph open source library was used for graphing.

Figure 6. ECE 476 Power Monitor application.

Results

Our final device is set to sample at the rate of 1 KHz, and output data at the rate of 1 Hz. Since the microcontroller is handling all of the Analog-Digital conversion as well as all the calculation needed for the output, we decide not to push the MCU to its limit as we like some buffer incase a rare event happens in which the MCU takes longer to complete a specific task. Regardless, the waveform being sampled runs around 60 Hz, the sample rate of 1 KHz gives us about 16 samples per waveform, and it is likely that faster sampling can cause more noise within the collected data. And since our data is used to calculate the Root-Mean-Squared, any noise with high peak will be especially devastating to our result.

We designed this device to measure stable devices, so the 1 Hz report time is not terribly slow as there are very few devices that has power draw that changes faster than once a second. The designed application for this device is for power monitoring over duration of time, as it also keep track of total energy used through the plugs. Also the commercial power measuring device that we have also only updates at 1 Hz.

The error in measurement can be from opto-isolator noise as well as the lack of precision in ADC and data calculation. To test the accuracy of our project, we used a commercial power measuring device Watts Up? Pro to measure the statistics of power drawn by a single 50 W light bulb. The results are organized in Table 1.

Table 1: Accuracy Comparison for a 50 Watt Light Bulb

Our Project / Commercial Product / Error
Total Power Draw (W) / 51.9 / 50.6 / 2.57%
RMS Voltage (V) / 120.0 / 121.5 / 1.23%
RMS Current (A) / 0.43 / 0.409 / 5.13%

The absolute difference in the power measurement is about 1 W, which we consider is pretty good. We feel that with higher load or even a calibration our device can achieve better accuracy.

Safety in Design

Safety to the device is achieved through the use of optisolators as well as different power supplies. As one can see in Figure 3, the board can be divided up into two regions, the MCU safe region, and the unsafe region. Across the regions are the white linear optoisolator and the isolated AC relay. These components ensure that the any wiring mistakes or accidents in the unsafe region will not affect the safe region, which is connected to the MCU, which in turn in connected to computer via serial.

Safety to the operator is achieved by following the same strict guidelines in wiring of a house. The colors of the power lines are always used so that live line is always black, neutral line is white, and ground line is green. Wire twists are used to connect two power wires as one would do in wiring a house socket. In addition to these precautions, we always test our device before a live AC test so that we do not cause any damage to any other equipments or people. Further discussion will be presented in the ethical considerations section of Conclusions.