Power Calcs Design

A project log for Home Power Usage Monitor

Gather power data from the breaker panel and beam it to a Raspberry Pi for storage and web serving

drew-yenzerDrew Yenzer 08/06/2015 at 23:230 Comments

Hi there! These numbers may not mean much, but this project has had over 2000 page views, 26 followers, and 5 skulls (no idea what those are)! That's exciting! It's encouraging to know there are people out there that are interested in what I'm doing. Thanks!

Now then, today I'm going to talk about my software design for calculating power. I'm nearly done implementing a first draft of it. It still needs to be tested with hardware, or simulated, before I'd call it ready, but I'll use some code snippets to help explain things.

If you'll remember, the hardware is composed of a line voltage ADC sensor, and 3 current ADC sensors. I'm calling that the 3 power channels. There are 5 main values that I'm interested in:

As you can see, in order to calculate these values you need instantaneous voltage and current measurements. For each power channel I'll need to measure both voltage and current at a certain sampling rate. I'm going to measure voltage for each channel for accuracy. If I measured voltage once, then the 3 current values, the delay between the voltage reading and the last current reading could be significant enough to make it an inaccurate power measurement.

Sampling speed and processing time could be an issue then. Depending on the load, current draw can change very rapidly within a 60Hz period. In order to capture that I'll try to sample the waveform as much as possible with enough overhead to process the data within the PIC. I'll also use a power of 2 as the sampling rate over a 60Hz period in order to make the Mean calculation faster, theoretically. I'll start with 64 samples per 60Hz period, or 3840Hz. This will allow me to adequately capture out to the 16th harmonic (64/4). By the way, for an excellent read on sampling for engineers, check out

At that rate, if the PIC clock is running at 8MHz (2MHz instruction cycle), I'll have 520 instructions available between samples. This should be enough to capture and do some accumulation, but not enough to do the math needed for the power calculations above. However, each second I'll have 2 million instructions available, which should be more than enough for the square roots and divisions. If it's not, I have a max clock of 48MHz in the PIC to play with.

To capture the data I'll use a timer interrupt at 3840Hz with the capture function within the interrupt. It's normally not good practice to do much other than setting a flag within an interrupt, but in this case I need to ensure the data is captured at specific intervals. The power calculations can continue on in the main loop of the program, initiated once every second. After the calculations are done it will update the LCD and transmit data with the nRF24, all while new data is being collected through interrupts.

This post is already getting a little long. For next time I think I'll post some of the code and also try simulating it. Stay tuned!