# Using the raspberry pi to measure multiple electrical circuits

I am looking to use the raspberry pi to measure the power consumption of multiple household electronics and record the information to analyze later on. However, I'm not sure if it's possible to connect as many devices as I would like to the Pi (up to 40). So far, I've done a little research on my own and found an 8-channel, 10-bit ADC (http://www.adafruit.com/datasheets/MCP3008.pdf) which seems promising. However, to be able to connect 40 devices, I would need 5 of these and some way of multiplexing the 10bit output.

This is where I'm currently stuck. I'm not sure what the best way is to pull the potential 50bit output into the raspberry pi. Perhaps I could use a number of shift registers connected in series? Would this work or is there a better solution?

Thanks

• Did you bother to read the MCP3008 datasheet? – Matt Young Jul 1 '14 at 20:07

I am looking to use the raspberry pi to measure the power consumption of multiple household electronics and record the information to analyze later on.

OK that's power you want to measure and power is voltage x current, not current on its own or voltage on its own - you need to reserve one ADC input for the voltage waveform and you need to take samples of voltage and current at about 1kHz and do the math internally on the MCU. You should also consider reserving an ADC especially for the voltage as it needs to be sampled in the identical time slot so there is no phase angle error when calculating power.

If you think you can do it with CTs, diodes, capacitors and a DC input to your ADC then think again - that's not what power is BUT if you in fact want to measure current then that's ~alright.

## OK I'm going to mention the ADC

A bit more about the ADC in the question - if you want to do this properly don't use this device - it can't do simultaneous sampling of all the inputs. I've suggested running at a sampling rate of 1kHz for a reason because, for a 50 Hz cycle (20 milli secs) you'll be able to take 20 samples and properly account for up to the ninth harmonic$^1$ of the current but, you need simultaneous sampling because if you "slipped" one sample between voltage and current measurements you'd be 1 millisecond out in 20 milliseconds and that's a phase angle of 18 degrees and gives a "power factor" error of 1-cos(18deg) = 4.9%.

# Harmonics

$^1$Ninth harmonic - I'm talking about distortion on the current waveform and being able to reject harmonics - you can do this by taking samples rapidly enough - think of it like aliasing - you need to sample at twice the highest frequency to be able to "handle" that frequency. Ninth harmonic is OK but only OK. Here's a typical current waveform of a pice of electronics with a conventional power convertor inside: -

The cleaner looking waveform (near sinusoidal) represents the AC voltage and the badly misshaped waveform the current. I can't do fourier analysis in my head but I know intuitively that this waveform is rich in harmonics and you need to sample fast enough to minimize error.

What about triac controlled lights? Here's the waveform so if you have any dimmers don't expect very accurate results until you take samples at maybe 5kHz: -

I guess the good news is (if you want to use a low sample rate), that ordinary power-inefficient tungsten filament lamps are low in harmonics because they are a resistive load but, if you have a lot of these then you're best spending your money on eco alternatives rather than monitoring the power. It's a little ironic that the easiest devices to measure power on are the ones you should be considering getting rid of! LED lighting, fluorescent lighting etc. are all fairly rich in harmonics unless you buy the ones with power factor correction. Here's what it can do: -

Without any power factor correction, the current waveform on the right is distorted and lags behind the voltage. Applying passive PF correction (middle) helps restore the waveform a little but in a power supply with active PF correction (left), the current waveform is almost perfectly restored to a sinusoidal wave that is aligned with the voltage.

Power factor info taken from here.

The MCP3008 has an SPI interface. SPI is a nifty protocol in that it allows you to bus multiple slaves together, minimizing the number of wires required. I'm assuming that Raspberry Pi has an SPI interface, so you'll need to verify that. What this would mean is that you can connect 5 of those ADCs to your raspberry pi, sharing the SCLK, MOSI, and MISO (a.k.a. Clk, Din, Dout) and each ADC would get its own SS\ signal (a.k.a. CS). This configuration would require a total of 8 I/Os from your R-Pi. You'll also need to provide the ADCs with VDD, Vref, DGnd, and AGnd according to the datasheet. Wikipedia provides a good beginner's description of SPI and the references at the bottom can provide further explanation (in addition to the description in the MCP3008 datasheet). You'll also have to find a guide on how to write software for the R-Pi that communicates over SPI (again, assuming it has that capability).

Wikipedia SPI description

• Not only does the Pi have SPI but there are ready-made libraries for it. – John U Jul 1 '14 at 20:50