Saturday, March 23, 2013

Kill-A-Watt Circuit Analysis

The Kill-A-Watt P4400 has been hacked many times, and there are a few schematics online. I'll use this one from Kevin Timmerman because I think it's the best of the lot, and it matches up perfectly with my KAW.  Note, there have been various versions of the KAW, so be sure to double check how this info compares to your own KAW.  My version is fairly recent.  My serial number is YCJ12965, if that helps.

Well, let's see how this thing works.  There are two circuit boards in the KAW.  The high voltage board has the AC plug and socket:

J1 is a 6 wire ribbon cable that connects to the display board.  Caution, ground is not connected to the KAW circuitry.  J1 pin 2 is connected to Neutral, which should be the lowest voltage in the KAW.  I say should be, because the KAW could be plugged into a miswired outlet, with Hot and Neutral swapped.  So "Neutral" could be 120Vac-rms, which is 340Vpp!

And even AC Neutral is not the same as earth ground, and is potentially dangerous.  Be safe, and never connect anything inside the KAW to the outside world.  If you decide to hack the KAW, decide right up front to use some kind of radio signal to transmit the data.

F1 is a 0.2A fuse between AC Hot and the rest of the KAW.  Note that J1 pin 1 connects AC Hot directly to the display board, via J1 pin 1.  The KAW measures the AC line voltage across J1 pins 1 and 2.

C1, R1, D2, D1 provide a half-wave rectified voltage of about 13.5Vdc to J1 pin 4.  This is the unregulated DC supply for the display board, and J1 pin 3 is the return path.

R2 is the load current shunt resistor, 0.0021 ohms.  It looks like a piece of coat-hanger wire, about an inch long.  The load current creates a voltage drop, that the KAW measures across J1 pins 5 and 6.  For the max rated load, 15A, the voltage drop is 15A * 0.0021 ohms = 0.0315Vac-rms. 

The display board is a mix of analog and digital, but for hacking, we're only interested in the analog circuits.


J1 is the 6 wire ribbon cable from the high voltage board.

J1 pin 3 is the DC power supply return path.  I'll call it "ground" for the display board, but in quotes as a reminder that it's floating and not safe to touch, or connect to any external circuitry, including a grounded scope probe! 

On the schematic there are three "ground" rails: one rail below Q2 and Q3; one rail below Q1 and going across to U1 pin 24; and one rail at the very bottom, for the push button switches and U4.

J1 pin 4 is the unregulated DC supply, about 13.5Vdc, with C11 to filter the ripple.  U5 is the familiar 78L05, three terminal voltage regulator, with 5Vdc, 100mA max output.  D1 is a 1N5235B, 6.8V zener, and Q1 is the ubiquitous 2N3904, wired as an emitter follower, providing about 6.1Vdc power for U3, an LM2902 quad opamp.

Both the 5V and 6.1V supplies are designed to support the KAW circuitry.  Neither has more than a few mA to spare, without compromising the KAW's own performance and accuracy.  So if you want to add a microcontroller and radio, plan on going back to the 13.5V unregulated supply and building out from there.  One easy way to do this is to "piggyback" another 2N3904 onto Q1.  Connect the new transistor base and collector to Q1's base and collector.  Use the new transistor's emitter as your own 6.1V supply.  Don't forget to add a bypass cap, and maybe a ferrite bead for high frequency isolation.

R27 and R12 form a voltage divider providing a reference voltage at about 2.33V.  U3B is a unity gain voltage follower, providing a buffered reference voltage on pin 7 for the remaining opamps.  This provides a DC offset to keep the signals in the 0 to 5 volt analog input range of the microcontroller U1.

U3D senses the 120Vac-rms, 340Vpp AC line voltage across J1 pins 1 and 2, and attenuates it by a factor of 100.  So U3D pin 14 is about 3.4Vpp, riding on a DC offset of 2.33V.

U3A amplifies the current shunt voltage across J1 pins 5 and 6, with a gain of 40.  At the max load current of 15A, the current shunt voltage is about 0.0315Vac rms or 0.089Vpp.  40x gain makes the output on pin 1 about 3.56Vpp, on a 2.33V offset.

When the load current is less than about 1A, the output on U3A pin 1 is too small for accurate measurements.  So U3C provides another 10x gain stage.  For a load current of 1A-rms,  the current shunt voltage is 1A * 0.0021 ohms = 0.0021Vac-rms, or about .00594Vpp.  The net gain of the two stages is 400, so U3C pin 8 will be about 2.38Vpp, on a 2.33V offset. 

So for best accuracy in measuring load current, digitize both U3A pin 1 and U3C pin 8, and use whichever one is appropriate based on the load current.  For load current greater than 2A, use pin 1.  For load current less than 1A, use pin 8.  Between 1A and 2A you can use either and they should agree fairly closely, assuming you have good calibration factors.

Recall that J1 pin 1 is AC Hot, 120VAC rms.  D2, Q2 and Q3 convert that to a 5 V logic level square wave, used by the KAW to measure the AC line frequency, nominally 60 Hz. 

Note that Arduino clock frequency is not very accurate, so you might want to use this signal, on Q3's collector, to accurately measure across an integer number of AC line cycles, to ensure a "true rms" reading.

So that's it, I hope it's useful.  Feel free to leave comments or questions. 

Friday, March 22, 2013

Meet the JeeNode and Moteino

One of the great things about Arduino for advanced hackers is the wide array of clones and derivatives.  On the other hand, I think the Ethernet and wireless shields are way too expensive.  That's why I've chosen RFM12B, for my wireless nodes and R-Pi base station. 

The JeeNode is an Arduino derivative, with RFM12B on board, available here in the USA from Modern Devices.  I've got a couple JeeNodes and they're great.  Jean-Claude Wippler's JeeLabs has extensive libraries, code, blogs and forums.  The JeeNode fits (barely) into a Kill-A-Watt P4400, and I'll be posting more about that project soon. 

And I recently came across Felix Rusu's LowPowerLab.  Felix has created his own Moteino, another Arduino-with-RFM12B derivative.  I just got two of them and they look really good:

I'm really pleased with the design and build quality on the Moteino, and looking forward to using them, and blogging about them.  As you can see, they're very compact, with the radio on one side and the ATMega328 on the other.  One of these will easily fit in a KAW, and in fact Felix has posted his own WattMote project. 

Felix and I traded some notes on KAW vagaries, and agree that the biggest hurdle is getting enough power out of the KAW to run the radio.  Which is somewhat ironic considering it plugs into the AC mains.  But we've each got some low-power tricks to share, so check out his project and stay tuned here for more details on mine.

Next up, my circuit analysis of the KAW. 

Wednesday, March 20, 2013

Low Cost Current Clamps for Whole House Power Monitor

I was looking for two current clamps to measure the currents through the main breaker for my house.  It's a 200A service, and the conductors are about 5/8" or 16mm.  The ones I found at Sparkfun and Seedstuidio are too small to fit around my wires.  Larger ones that I found were either closed core or expensive.

Finally, I found these very nice, low-cost current clamps from Steren Shop.  They just came yesterday and I won't have time to really test them out till the weekend.  But I just had to open them up and see what I got, for only $11.29 each.  I have to say, they look pretty decent!

That's a 6" ruler on the right.  The jaws are plenty big enough to go around my wires. I took the back off one of them, and as I hoped, I can reduce the overall size of these by removing some (or all) of the black housing, if needed to fit them inside my breaker box.

The circuit board has two "burden" resistors, selected by the range switch.  There's also a trim pot for calibration.  I'll probably come up with my own value for the load resistor, and do the calibration in the Arduino sketch.  So I might remove the circuit board and switch and use just the yellow jaws and my own resistor.  The spring for the jaws is part of the black plastic housing, so I'd need something else to keep them closed.

In any case, this will be an AC voltage signal, so I'll need some signal processing before the Arduino analog inputs.  I may copy part of the Kill-A-Watt opamp circuit.

As a bonus, these came in a nice zip up case, so if you want to use these as they were designed, to plug into a DVM, there you go. 

These are made in China, and the US distributor is Steren Shop.  The model number on the front says Steren 603-285.  They are CE marked, and rated for 600V, and 400A max.

Saturday, March 16, 2013

JeeNode Kill-A-Watt - Part 1

I'll bet your thinking: great, just what the world needs, another Kill-A-Watt hack!?  Well, I've spent more hours than I'd care to admit working on this, and I've come up with a number of hardware and software improvements that I think will make this worth your while:
  • Uses a JeeNode with RFM12B radio for low cost
  • Uses both Kill-A-Watt current ranges for best accuracy
  • Improved power supply and reset circuit for reliable operation
  • Measures and transmits true AC RMS Voltage and Current 

JeeNode Kill-A-Watt being programmed

I'd like to acknowledge the work of others before me, because they helped me, and so I can refer you to them and keep my posts shorter.  By all accounts, Ladyada had the first published Kill-A-Watt hack, aptly named the Tweet-A-Watt My only issue with her design is that I'm cheap, so Xbee modules aren't for me. 

I plan to deploy lots of different kinds of data collecting nodes, and a Raspberry Pi central receiver and web server.  And so for me, the RFM12B modules are ideal, either as an add on to Arduino (or Pi), or as part of the excellent JeeNodes.  I use the 433 MHz version here in the US, and they have excellent range, easily covering all rooms on both floors and attic in my house.  I may even deploy some outside.

The Tweet-A-Watt created quite a buzz, and there are lots of forum posts about it, and alternative approaches.  Some are very dangerous, bringing electrical signals out of the Kill-A-Watt to be connected to an Arduino, or a computer or whatever.  I need to be very clear about this: DON'T DO IT!  The Kill-A-Watt has about 340 Volts peak to peak inside.  The plastic case is there to save your life. 

The only safe way to hack a Kill-A-Watt is to have a completely self-contained radio transmitter.  Note, I've seen designs (that I won't link to) that use an opto-isolator to provide a serial link, but I wouldn't even trust that, it's still wires that connect the inside of the Kill-A-Watt to the outside.  And besides, that design leaves all the math to be done by the receiver.

And bear with me please just a moment longer, lest you be fooled into thinking it's safe if you know what you're doing.  Just because the circuit board "ground" inside the Kill-A-Watt is connected to AC Neutral, does NOT make it safe.  For one thing, the Kill-A-Watt can be plugged into an extension cord or miswired outlet, such that Neutral and Hot are swapped.  And even if Hot and Neutral aren't swapped, Neutral itself can easily, and typically, be a few volts away from earth ground, with plenty of Amps to back it up.  So as soon as you plug in your Arduino to a dangerously hacked Kill-A-Watt and to the USB on your computer, there's an excellent chance you'll see smoke and fire.  Don't do it.

Beyond Ladyada's inspiration, the most helpful info I found was an excellent Kill-A-Watt schematic.  By studying this schematic and carefully experimenting with the Kill-A-Watt, I figured out that the Kill-A-Watt has two current ranges.  Ladyada's design, and all others I've seen, only use the coarse current range, on pin 1 of the quad op-amp.  But when the load current is less than about 1 Amp RMS, that output has more noise than signal.  That's why there's a another (filtered) amplifier stage.  Pin 8 provides good clean current readings, from milliamps to about 2A, where it saturates.  So my design, like the Kill-A-Watt itself, chooses the appropriate current range depending on the load.

(Note, by "carefully experimented," I mean that I used an isolation transformer between the Kill-A-Watt and the AC mains.  And I used a high voltage differential scope probe to connect to the Kill-A-Watt.  I always powered down before connecting or moving probes or loads.  And I applied power from a safe distance using a switched power strip, just in case there was an accidental short.)

Well, that's it for the overview and safety warnings.

Part 2 will be a lot more interesting, I promise.  I'll present my hardware design, with pictures and schematics.

In part 3, I'll bring it all together by covering the software, low power mode, measurement math, calibration, transmission, receiver.  Actually that might be more like parts 3 and 4.  We'll see.

Oh yeah, just about the time I was finishing up this project, I came across a very interesting site, LowPowerLab and this fellow has a very nice "Moteino," rather like a shrunken JeeNode.  In fact, if I'd found his site a month ago, I've have used the Moteino instead, because it's smaller and easier to fit inside.  And sure enough, he's got his own Kill-A-Watt hack called the WattMote.  I do plan to get some Moteinos and review them...

And I'm beginning to think the main market for the Kill-A-Watt is the maker-hacker community!

Tuesday, March 5, 2013

Infrared Fan Speed Control


This project demonstrates using an infrared light beam to measure the speed of a fan, and PWM to adjust the speed to a preset value.  I used a small 12 Vdc fan from a PC CPU heat sink.  It will run on 5V, somewhat slow, and drawing only about 40 mA, perfect for this project.  Although an Arduino digital output could safely drive the fan, I decided to play it safe and use a transistor.  Here's my schematic, created with TinyCAD:



The 10K Ohm pot sets the target speed.  The PWM output on pin D5 drives the transistor to regulate the motor speed.  The fan blades interrupt the infrared beam, and that generates interrupts via pin D3.  Here's the sketch:


/*
 IR_MotorSpeed.pde miketranch comcast.net V0.4 Feb 8, 2013
 10K pot sets desired speed 
 IR detector measures fan speed
 adjusts PWM voltage to regulate speed
 Arduino UNO pins used:
   D3  IR detector, interrupt 1
   D5  PWM output drives motor
   A0  10K pot
*/

const int potPin = 0;    // 10K pot 5V to ground, wiper to pin A0 
const int PWMLED = 5;    // PWM output to transistor to motor
const int IR_Pin = 3;    // Hall Sensor Output on Pin 3
const int IR_Int = 1;    // Int #1 on Pin 3
volatile int count = 0;  // count interrupts from IR Sensor

void setup()
{
  pinMode(IR_Pin, INPUT_PULLUP); // open collector IR detector
  Serial.begin(9600);
  Serial.println("\n\nInfrared Fan Motor Speed Control");
}

void loop() { 
  // static variables remember their values
  static int PWMval; // latest attempt to set target speed 
  
  // regular variables are initialized each time loop() executes
  int potVal = analogRead(potPin); // read the voltage on the pot
  // map to desired speed range  
  int SetSpeed = map(potVal, 0, 1023, 50, 400); 
  
  if (count == 0) {      // motor stopped, kick start it
    PWMval = 254;        // full voltage and power
    Serial.println("Full Power Start"); 
  } else {               // adjust speed
    float delta = (SetSpeed - count); // speed error
    if (delta > 0 )                   // too slow, increase voltage
      PWMval = PWMval + sqrt(delta);  // by square root of error
    else                              // too fast, decrease voltage 
      PWMval = PWMval - sqrt(-delta); // by square root of error
  } 
  
  PWMval = constrain(PWMval, 0, 255); // PWM must be 0 to 255
  
  Serial.print("Set speed: "); Serial.print(SetSpeed);
  Serial.print(" PWM: ");      Serial.print(PWMval);
  
  analogWrite(PWMLED, PWMval);  // set the motor voltage level
  delay(1000);       // give motor time to settle to new speed
  
  count = 0;
  attachInterrupt(IR_Int, IR_ISR, CHANGE); 
  delay(1000);               // measure for 1 second
  detachInterrupt(IR_Int);   // disable interrupt 
  
  Serial.print(" Speed: "); Serial.println(count);
}
     
void IR_ISR() {
  count++;  
}


It was fun working out the math to get this sketch working well with the hardware.  The interrupt is enabled for 1 second, and counts both edges of each blade, because of the CHANGE keyword.  I empirically determined the motor could be slowed to about 40 interrupts per second, or sped up to about 420 interrupts per second.  The map function converts the 10K pot setting to a SetSpeed value in the range of 50 to 400.

If the motor stalls, the PWMval is set to 254, essentially full power, but still giving pulses that are convenient for triggering a scope.  Otherwise, the error computed as SetSpeed minus the measured speed.  I tried adjusting PWMval up or down by the error value, but the motor speed didn't settle very well.  Then I realized that the PWMval is controlling the voltage, but the motor speed is probably more directly related to power.  So I tried adjusting PWMval by the square root of the error, and that works better.  Here's a sample output from the serial monitor:


Infrared Fan Motor Speed Control
Full Power Start
Set speed: 228 PWM: 254 Speed: 299
Set speed: 228 PWM: 245 Speed: 420
Set speed: 228 PWM: 231 Speed: 423
Set speed: 228 PWM: 217 Speed: 405
Set speed: 228 PWM: 203 Speed: 383
Set speed: 228 PWM: 190 Speed: 360
Set speed: 228 PWM: 178 Speed: 337
Set speed: 228 PWM: 167 Speed: 315
Set speed: 228 PWM: 157 Speed: 295
Set speed: 228 PWM: 148 Speed: 279
Set speed: 228 PWM: 140 Speed: 261
Set speed: 228 PWM: 134 Speed: 245
Set speed: 228 PWM: 129 Speed: 232
Set speed: 228 PWM: 127 Speed: 223
Set speed: 228 PWM: 129 Speed: 221
Set speed: 228 PWM: 131 Speed: 226
Set speed: 228 PWM: 132 Speed: 229
Set speed: 228 PWM: 131 Speed: 229
Set speed: 228 PWM: 130 Speed: 227
Set speed: 228 PWM: 131 Speed: 226
Set speed: 228 PWM: 132 Speed: 228
Set speed: 228 PWM: 132 Speed: 229
Set speed: 228 PWM: 131 Speed: 228
Set speed: 228 PWM: 131 Speed: 227
Set speed: 228 PWM: 132 Speed: 229
Set speed: 228 PWM: 131 Speed: 228
Set speed: 228 PWM: 131 Speed: 228





Sunday, March 3, 2013

TinyCad = Free and Easy Schematics

For many years I've been sketching little schematics by hand, and that's been fine for my needs, but now that I'm blogging I need a way to share my work.  With a little searching in google I hit upon TinyCad and the price was right, free!  I downloaded it, and installed it and within minutes I created a schematic.  It really is that easy, so if you've been thinking about taking the plunge, go for it!

Here's a PNG output of the schematic I just created (from a hand drawn original, that represents many hours of trial and error, er, I mean methodical design, LOL)



The JeeNode Kill-A-Watt will be featured in some future blogs, so check back...