Saturday, July 14, 2012

Home Power Monitor - Arduino ADC Speed

An important part of my home power monitor is the ability to measure the instantaneous power for a given circuit; this will allow me to calculate other important nerdy things like the power factor and reactive power.  This approach requires frequent sampling of the voltage and current waveforms so that the average power over many 60Hz cycles can be found.  There are two important points in making this work well:

  • The voltage and current should be measured (ideally) simultaneously so that the power at that specific point in time can be determined.
  • The rate at which new voltage and current measurements are made needs to be high enough that the accumulated samples form an accurate representation of the source waveform.  This is especially true if the current waveform has transients and nonlinearities (due to non-linear loads caused by power electronics).
Both of these factors are highly dependent on the speed of the analog-to-digital converter (ADC).  Assuming that multiple-channel simultaneous ADCs are not being used (which is the case for my system), separation in time between the voltage and current measurements will be entirely a function of the conversion time.  The measurement of the current can't start until the measurement of the voltage is complete.  Similarly, the rate at which the 60Hz voltage and current waveforms can be sampled is determined by how long it takes one voltage-current measurement pair to be made.

For the sake of simplicity, this prototype system I'm building now is going to use the Arduino's on-board ADC.  This ADC has a built-in multiplexer which has a reputation for not being very fast but since I am planning on using an external multiplexer to accommodate all the power circuits I want to measure (the Arduino can only mux in 8 channels, my plan calls for 13-16), I can get away with only using a single channel for the on-board ADC. 

The question that remains, then, is how fast the on-board ADC can do a conversion.  My prowling of the internet found some very experienced people who found a way to adjust the clock that the ADC uses when converting.  The ADC clock runs at a scaled down rate (prescaler) of the main system clock and the scaling rate can be changed programmatically.  A part of the documentation for the microprocessor used in my Arduino states:

The ADC accuracy also depends on the ADC clock. The recommended maximum ADC clock frequency is limited by the internal DAC in the conversion circuitry. For optimum performance, the ADC clock should not exceed 200 kHz. However, frequencies up to 1 MHz do not reduce the ADC resolution significantly.

Operating the ADC with frequencies greater than 1 MHz is not characterized.
Ignoring the actual results from the conversion, I decided to characterize the time it takes for the ADC to complete a given number of conversions.  The results from this test are below showing the conversion time for a varying number of conversions.  The last column is the difference in the 1 million conversions time as compared to the previous prescaler value.

Prescaler Value     1k conv.   10k conv.   100k conv.   1M conv.   Difference
2                     7.52us     7.43us      7.42us       7.42us    -
4                     9.53us     9.32us      9.30us       9.30us    1.88us
8                    12.78us    12.57us     12.55us      12.55us    3.25us
16                   19.15us    19.04us     19.04us      19.03us    6.48us
32                   32.19us    32.02us     32.00us      32.00us   13.00us
64                   60.28us    60.24us     60.24us      60.24us   28.24us
128                 112.24us   112.02us    112.00us     112.00us   51.76us


There are two reasons to feel confident in this data:

  1. The conversion time is reasonably consistent across the various number of conversions.  There is not a dramatic difference in the calculated conversion time when the loop contained 1 thousand conversion and 1 million conversions.
  2. The difference in conversion times scales very nearly linearly with the prescaler value.
Based on these results and the documentation's note that the ADC should operate reasonably well up to 1 MHz, I am planning on setting the prescaler to 16 giving me that 1 MHz conversion rate.  Doing the math: 16 channels at a conversion time of 19us per channel gives me a total of 304us to sample all channels.  With a 60 Hz waveform this allows 54 samples across all channels in one cycle.  This is maximum conversion rate I should expect; the test above does nothing with the conversion results except store them.  The arithmetic I need to do will slow the process down; characterizing that will be another test I'll have to do once the hardware is complete and I have a fuller start on the hardware.

Sunday, July 08, 2012

Home Power System - Hardware Build - Part 2

Here's the latest developments in the home power system monitor:




The black rails down the side are the connectors for the current sensor connector modules. Yes, that makes it a connector for a connector. This is what happens when you buy connectors that you can't solder directly to the board. I've also decided to provide connectors for (and thus modulize) the analog mux I'm going to be using to read all these signals into a signal ADC port on the Arduino, the Arduino itself, and the radio I'll be using to send the information on to a computer for reporting.

The two sockets towards the top are the amplification modules for the current sensors. Each socket will hold two MCP6002 op-amp DIPs giving me a total of four channels per socket. I would have used the correct sized socket (one DIP per socket) but I forgot to order more and my supply here at home is almost gone.

I've completed four channels so far with four more to go before I get done creating channels for all of the circuits in my house that use both sides of the split-phase wiring. This makes eight current sensor channels to read four appliances here at home: mains, air-conditioner, oven, and clothes dryer. For now I'm only going to add three more channels (for a nice, even 11 channels total): refrigerator, entertainment center, and downstairs utilities like a chest freezer and clothes washer. Assuming this all works fine, I have room for three more channels in my current design.

Tuesday, July 03, 2012

Home Power System - Radio Assembly


Each one of those green things is a radio capable of transmitting and receiving data when hooked up to an Arduino.  You'll notice the there are actually two boards soldered together for each module.  My good friend Keith did the layout and legwork in getting the "motherboard" PCBs produced and last night hand-soldered all the components except the radio itself. Note the impossibly small size of the chip on the backside of the board (labelled "TXB0108").  Did I mention that Keith really enjoys doing detailed work like this?  I picked the boards up from him this afternoon and soldered on the radio "daughterboards" this evening.  My work is the messier of the two; forgive me, Keith.

The radios are quite impressive little beasts that provide a large amount of functionality in a very small package. For $7 I got everything I'll need to transmit my power measurements from one Arduino up to the Arduino I'm using right now for my home temperature monitor. I'll be able to use those existing scripts and data presentation methods to generate all the graphs I'll need. There is a lot of information out there on using this radio and I am hoping/expecting it won't be too difficult to get working. The only thing missing now from these modules is the antenna which will simply be a few-foot-long piece of wire.


The tiny chip on the back is a relatively expensive level shifter to convert my +5VDC Arduino signals down to a +3.3VDC that the radio is expecting. Other's have done this level shifting with resistors on signals headed to the radio (and nothing for the signals coming from). This seems to work just fine but technically such a plan doesn't quite meet Vih/Voh requirements; there is a small chance that some batch of chips will barely meet their specs but not work in this application. To avoid this possibility (and try out a new chip), Keith and I decided to go with the level-shifter and see how it works. I probably won't be testing the radios for a while as I'm focussing on getting the other hardware assembled.

Monday, July 02, 2012

Home Power System - Hardware Build - Part 1

For the past few months, in my voluminous spare time, I've been working on developing a system similar to my home temperature monitor that would monitor and record electrical power consumption.  The system will  measure the total power consumption of the house as well as that of a select few circuits (air-conditioner, oven, microwave, ...). The design has been almost entirely on paper until yesterday, when I started soldering and testing the first pieces of the design.  Below is what I've accomplished so far:



The two blue blocks on the left and right is where the two phases will be plugged in and the big white, brown and yellow blocks are transformers that will change the voltage from 120VAC (RMS) to 8VAC (RMS).  Most of the components along the top use one of the outputs from the transformer to make a +5VDC power supply; there is also a precision +2.5VDC reference (and capacitor) at the very left of the row of components.  Below that row of components are two op-amps (my new favorites, the MCP6002): one buffers the precision reference and the other (not quite complete) is used to scaling down the voltage supply from each phase to be sampled and used in power calculation.

I've been testing as I've built and so far everything works great.  I learned that the precision reference I'm using (MCP1525) REQUIRES a capacitor on the output and that its important to take your time and get the wires soldered in the right spot.  The later is a bit tricky when you're constantly laying the parts out on top but having the solder the connections on the underside. I'm considering this a prototype version and, assuming it works well, may at some point turn this into a more professional design by making a PCB.

For those interested in the theory, in broad strokes, here is how I plan on measuring power consumption. I am interested in measuring the full complex power which means I need a way of determining the phase difference between the voltage and the current. After some consideration, I decided the best way to go is to sample the voltage and current waveforms directly and calculate the power as the multi-period average of the product of those two.  This is the most general (and hardly ever used) method for calculating power and it allows me to measure the non-sinusoidal waveforms that power electronics (computers, TVs, ...) generate.  Using this technique also allows me to calculate RMS voltage and current which can be used to calculate the apparent power and from there the reactive power and power factor.

Using the general approach is powerful because it enables all these other measurements but it has its own complications.  Since this is truly an AC system, half of the waveform is positive and the other half is negative and is therefore outside the voltage range of the Arduino's ADC.  To make the system work, I'm having to do a bit of signal conditioning to shift all the AC waveforms so that they are centered around +2.5VDC and and adjust the amplitude of each signal so that its maximum voltage swing is 5Vpk-pk. (This is the reason for the +2.5VDC precision reference; it provides a stable elevated "ground" point for my signal conditioning).

Thankfully, the same mechanisms I'm using to isolate this board from the high voltages and currents in a home power system also serve to allow my reference point to be moved up from ground to +2.5VDC: transformers.  All the voltages and currents are measured through transformers which removes the negative side of the signal from the true ground reference and allows me to assigned this signal a reference of my choice. One of the op-amp on the board above handles the amplitude adjustment and scales the signal down so that it fits in the 0VDC-5VDC range.