As we saw in Part 4, the “thicker” the AC wave appears on a graph of our MCP3002 measurements, the more power it’s consuming. This “thickness” is a great proxy for power consumption called the “amplitude” of the wave - the distance between the highest and lowest points of the wave.
When an appliance is using no power, the amplitude should be 0. In testing, I found that jittery power can sometimes produce a resting amplitude of up to +/-6, so our actual resting amplitude turns out to be 12.
In order to find the amplitude of the 60Hz wave, we need to measure it for at least 1/60th of a second. This will guarantee that we’ve measured an entire cycle of the wave. If we run the MCP3002 at 50 kHz, then we’ll need at least 50,000/60 = 834 measurements to measure a full cycle.
This will only let us measure a 1/60th of a second time period, though. Maybe the appliance isn’t using much power during that tiny window. If we increase the amount of time we’re measuring for, we can get a better assessment of power usage.
Let’s arbitrarily pick 7500 measurements. That’s just about 9 cycles, or 3/20ths of a second.
We can see that measuring for just 3/20ths of a second will accumulate a lot of data. If we want to do meaningful work with this long list of numbers, we need to simplify it down to a single representation of state.
State is a concept from computer science. When we say something has a state, we are describing the properties of that object at a particular point in time. Like on October 27th, a pumpkin might have a state of ‘picked’. Then, on October 30th, it goes from ‘picked’ to ‘carved’ and on Halloween it goes to ‘jack-o-lantern’. By November 3rd, it’s ‘rotten’.
Our pumpkin has many states, but our appliances really only have two for our purposes - ‘on’ and ‘off.’
Simplyifing the Measurement
To make a shorter list of numbers, we can take out unnecessary information from our sine wave. The ‘unnecessary’ information is basically ‘anything that isn’t a peak or a trough’ since those are the only points we care about to calculate amplitude.
We could just take the highest and lowest points of the entire 7500 item list and call it a day, but this might be a problem when there are outliers or measurement errors that are sudden spikes in the data. So we want to amplify the effect of peaks and troughs, but smooth out the effect of any one outlier peak or trough.
Make a sliding ‘window’ and every 100 measurements, find the max and min value of the 50 preceding and 50 next measurements.
Taking the average of these ‘windowed’ amplitudes means that any one accidental spike will only count toward the average of its own peak or trough, and the rest of the wave cycles will push the average closer to its true value.
Notice how, in this image, a single spike makes the global maximum around 625 while the true maximum, seen over several cycles, is closer to 595. Using the ‘window’ approach pulls the average maximum closer to its true value.
To get from an average peak / trough to an average amplitude is as easy as subracting the trough from the peak.
Finally, we come to our measurement algorithm: a function that starts off by measuring each channel 7500 times at 50 kHz and spits out a single number - an adjusted-average amplitude.
Let’s have a look at some real-life amplitude measurements during a washer and dryer cycle:
At rest, each appliance usually has an average-adjusted amplitude of 6.
From Amplitude to State, and Keeping Track of State
So then, to get from amplitude to appliance state is not a taxing calculation: Any amplitude greater than 12 is ‘on’, anything 12 or less is ‘off’
We set up our measurement script to run once every minute and find the current state of the appliance. But the script only knows what the appliance is doing this very minute. How can we find out what it did in the previous minute?
For that, we’ll need a database. InfluxDB is a great choice for this application, as it’s designed for measuring things over time.
So to add to our script that runs each minute, we’ll write the measurement and state to the database:
We can now keep track directly of when an appliance is on or off:
Comparing States and Taking Actions
Now we can compare the current state to the previous state.
Most of the time, we’ll find that the state is unchanged. An appliance in motion tends to remain in motion, and an appliance at rest tends to remain at rest. But occasionally, we’ll find that the state is different - the appliance started up or the appliance powered down.
So we have to get the last known state:
Now we compare the last known state to the current state:
There are two possible state transitions: ‘on’ to ‘off’ and ‘off’ to ‘on’. We’ve called these ‘powering up’ or ‘shutting down’. I only want to get text messages when an appliance shuts down - after all, when it turns on someone is standing there pushing the ‘on’ button.
To get text messages from our Python script we’ll use the awesome Twilio Python REST API.
Now I’m happily enjoying my laziness and a prompt turnaround on laundry:
When I was following the OpenEnergyMon guide I noticed they were including a capacitor in their circuit:
What’s that there for? It’s to provide an alternative path for some of the current to flow to the ground. Capacitors take some time to charge up and then discharge all at once, in a way that is dependent on the frequency of the current flowing through them. An arrangement like this with a resistor and a capacitor together is called a low-pass filter, meaning that only signals with a low frequency will be able to pass. It will filter out excess noise.
I did some experimenting by measuring the voltage signal with and without the low-pass filter.
Across the entire range of the ADC:
and zoomed in:
As you can see, it’s not trivial. A perfectly clean signal would measure 511 every time, but there’s some jitter. With the low-pass filter, the difference between the maximum value and the minimum is around 15. Without the filter, it’s much as 50. To a crude approximation, the filter adds around 10% accuracy to our measurement.
Two Jacks and Two Channels
Up to this point, we’ve only been measuring one input at a time. With 2 channels, the MCP3002 can read 2 voltages. And with 2 laundry appliances, that’s what I need to do.
We’ll add a second jack to the circuit:
We can share the same voltage divider, and direct our output to channel 1.
Measuring the Real Appliances
Now, in order to translate voltage readings into the state of the appliance, I need to actually measure the appliances. I turned the breaker off to the dryer, unplugged it, and took off the back cover to fit the current sensor around 1 of the wires. A 220V dryer in the US has 4 wires coming into it. The green ground, 2 phases of power (red and black) and a white neutral. After some trial and error, I found that my dryer had the heating element powered by the left phase and the motor powered by the right phase.
For the washer, I turned off the breaker to the outlet it was plugged into and put the current sensor around the power line inside the outlet.
I cued up the measurement script and took some readings of the dryer:
It was basically perfect. I was excited here - the washer reading stays almost exactly at baseline and the dryer reading starts out there. When the dryer turns on, it is instantaneously recognizable in the graph.
In Part 5, I’ll show you my algorithm for turning thousands of measurements into a single meaningful data point.
We’re using the SPI interface, so we want to hook up the Pi to the MCP3002 like so (note the little divot on the left side of the MCP3002 for alignment):
MCP3002
RPi
Reason
VDD
3V3 +
Operating voltage; max voltage
CLK
SCLK
Clock sync
DOUT
MISO
Data out of the MCP3002 and into the Pi (Master In; Slave Out)
DIN
MOSI
Data into the MCP3002 and out of the Pi (Master Out;Slave In)
CS
CE0
Chip Select - there are 2 data channels on the Pi and we’ll use the first one, 0
VSS
GND (3V3 - )
Ground (On the CanaKit breakout, it’s also called “3V3 -“
This leaves 2 pins unconnected on the MCP3002 - CH0 and CH1 - the 2 channels of data we can measure. Let’s leave them empty for now.
Programming the Pi
Now we can finally measure using the Pi. Boot into your Pi and get it into SPI mode:
Since no current is flowing through the CH0 and CH1 pins, they are essentially at zero volts. There can be some error in the measurement, but it’s close enough.
Measuring Some Voltage
Cool, now let’s see the high end of the scale. We can measure up to 3.3V. Let’s hook up CH0 to the 3V3 rail with a 10kOhm resistor to prevent damaging the Pi:
Measure again:
Cool, we’ve measured the top of the range and the bottom of the range!
Let’s measure some real voltage from the CT sensor:
Now, the CT sensor is connected to GND across a 68 Ohm resistor. When no current flows through the CT sensor, we will read 0V. We put the CT sensor around a 12A space heater and echo the output of the measure program to text:
I took that and plotted it in Excel:
One thing to notice is that we’re only getting half of the information about the AC wave. This is because when the wave is at its peak, we’re pushing voltage toward the GND pin. When the wave is in a trough, it’s trying to push voltage back toward the 3V3 pin to no avail and the MCP3002 still reads it as 0V.
We need to get the steady-state voltage up to a higher amount so that we can still read a positive voltage with the MCP3002. How about halfway?
Voltage Divider
Resistors create something called a “voltage drop” which is some EE wizardry that I don’t understand. Take it for granted that when you have a resistor, the voltage on one terminal is measurably and proportionally different to the voltage on the other terminal. We can exploit this principle to put our circuit at that “steady state” I mentioned before.
By connecting two resistors in series and then measuring the voltage in between them, we can obtain a desired voltage that is a specific fraction of the input voltage. This arrangement is called a voltage divider.
From the equations there, we postulate that if we have 2 resistors of equal resistance connected in series, then the voltage between them will be exactly half the input voltage.
R1 = R2 = 100kOhm
I’ll use 100kOhm resistors to lessen the amount of current this circuit will drain from the Pi in case I use a battery pack.
Let’s measure the heater again:
Cool. We can now see the full AC sine wave of this space heater - which is an ideal resistive load, always using a consistent amount of power. You’ll noticed it’s centered around 1/2 of our 10-bit scale: 511.5.
You’ll notice that these come with 3.5mm TRS plugs on the end. This will make them easy to plug in and remove, but now I need a jack to receive them.
SparkFun TRRS 3.5mm Jack Breakout
These jacks come already assigned to a breakout board, which will make it convenient to use them with a breadboard - once some headers are soldered on:
Break Away Headers - Straight
Once the parts arrived, I needed to solder the header pins onto the jack breakout for attaching to the breadboard. I’d never soldered delicate electronics, only larger wires so I picked up some 0.32 solder and watched some tutorials.
With the parts soldered and prepared, it’s time to start measuring.
Measuring Current
So a little math here. According to the datasheet, our current sensor has a 2000-turn ratio. This means a current of 30 Amps, divided by 2000 turns, will yield a resultant current of 15mA. What voltage will that current be at? Well, theoretically, an infinite voltage (I don’t quite understand why this is, but I trust the opinion of people smarter than me). If you just hook the CT up to a multimeter, that meter will have resistance in the circuit and so you’ll get a “voltage drop” across the two leads of the multimeter.
And so we get to measuring a 100-Watt lightbulb with just the multimeter hooked to it for a test. Turns out, with a measly 0.8 A of current, we’re already up to 2V. This won’t do. I need a way to get the voltage down further.
The key bit is that our maximum measurable voltage is 3.3V - that’s the potential we’re feeding into the MCP3002. Since we’re measuring AC current, it means that 3.3V is the peak-to peak meaning that we actually want to measure a max of 1.65V as our zero-point or reference point. At the maximum peak of the AC wave, we’ll have 1.65V reference + 1.65V from the sensor = 3.3V. At the minimum trough of the AC wave, we’ll have 1.65V - 1.65V = 0V.
The equation for burden resistor calculation is:
With a 30A current, we’d have a burden resistor value of 38.8 Ohms.
Since RadioShack had 68 Ohms as the closest resistor value (heh), I can actually measure up to 17A safely:
So we have a circuit that won’t overvolt the Pi! In part 3, we’ll hook the sensor up to the Pi and start measuring it.
The time indicators on my washer and dryer are notoriously unreliable. Also, my and Mel’s busy brains occasionally forget that we started doing laundry. I wanted to build a system that would text me when my washer or dryer finished running.
While searching for some hardware to help me tell if my washer or dryer were running, I came up empty - it seems like any prefab way to measure this would involve either buying a new washer and dryer or buying expensive hardware. I therefore decided to make my first foray into electronics hardware.
How to Tell If an Appliance Is Running
The first part of getting my washer to text me that it’s done is to tell when it is running. I thought of a few ways to do this:
Tap into the circuitry of the appliance and check if a certain part is running. This will work well for “smart” appliances with circuit boards, but would require messing around inside the washer/dryer, possibly voiding my warranty, and needing to come up with a new method if I get a new washer/dryer.
Sense vibration the way that a few other online solutions do. This seemed highly error-prone to me.
Measure the power consumption of the appliance with a Hall effect sensor. If the appliance is consuming power, consider it “running.” This would involve putting a sensor inline with the power cable feeding an appliance, such as the ACS712. This is a more-attractive option because it’s a one-size-fits-all approach for both the washer and the dryer and could be extended to other appliances like a dishwasher or a rice cooker. The downside is that it involves interrupting the flow of power and messing with mains power, which is risky.
Measure power as in option 2, but instead use a noninvasive current transformer sensor. This what whole-home monitoring solutions like Neurio use. Neurio is one of the premade options I considered, but it seems too unproven and new. CT sensors use a ring-shaped clamp that goes around one of the wires in the circuit. This has the benefit of being wholly separate from the mains current, making it safer.
I opted to use a CT sensor. Since I already had a Raspberry Pi available, I decided I would use it as the platform for building my device. It has the benefit of being easy to program, running a full Linux desktop environment, and having tons of forum support online. Another option is to use an Arduino. In the future, I’d like to try migrating to a standalone, low-power board like the ESP8266.
CT sensors are very sensitive - they can measure current to a precise degree and can be used to tell exactly how much power you’re consuming. My needs are a lot simpler - I don’t need to know how much power is being consumed, just whether or not it is.
So here are the building blocks: a Raspberry Pi to run software and send text messages; current sensors to measure the power usage, and…something in the middle.
Something in the Middle
One thing I quickly realized is that the Raspberry Pi does not have an analog input. Raspberry Pi’s inputs, called general-purpose-input-output or GPIO, can only tell whether a voltage is above a certain threshold (“high”) or below it (“low”), because they are digital inputs.
Current sensors work by taking a high voltage current at high amperage and converting it down to a few milliamps of current at only a few volts. But since they are measuring alternating current, the voltage will be rapidly switching signs back and forth:
Theoretically, I could have used the GPIO to measure this state with only 1 bit of resolution (very crude, only on or off). I wanted something a little more precise, and some Googling led me to an Analog to Digital Converter (ADC).
An ADC takes an analog signal and discretizes it - from an infinite number of values to specific steps. Since I wanted to measure 2 inputs (washer and dryer), I got a 2-channel ADC, the MCP3002. This chip has a 10-bit resolution, which gives you 1024 possible values instead of only 2. Much better.
Stay tuned for Part 2, in which I put together a parts list and get to soldering.