WasherBot and DryerBot, Part 3: Connecting to the Pi and Building a Voltage Divider
03 Dec 2016Table of Contents
- Part 1: How to Tell If an Appliance Is Running
- Part 2: Current Sensors and Burden Resistors
- Part 3: Connecting to the Pi and Building a Voltage Divider
- Part 4: A Low-Pass Filter and Reading From Laundry Appliances
- Part 5: A Measurement Algorithm and Text Message Integration
Time to get started with the Pi.
First, we need to hook the ADC up to the Pi. There are several good tutorials on this but I found this one to be the most helpful.
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:
And download my generic measuring script
run it, and you should see output like this:
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.
Here’s a good resource for learning more about voltage dividers
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.
How about something that’s not a perfect load?
…neat.