My electrical power meter has a pale yellow LED that flashes once for ever watt hour used. Inspired by power.labitat.dk, jespereklund.blogspot.com/2011/10/elmaler-overvagning-med-arduino.html and other projects, I set out to count the flashes with an Arduino and output some nice graphs showing my electricity consumption.

I wanted to use an LDR (LDR’s on Amazon ) to “see” the flashing LED on the power meter, but none of the example circuits I found online seemed to work. Well, they worked if I flashed a bright LED at them, but the LED on my power meter was apparently too weak. After some experimentation I came up with the curcuit on the right. It only uses the LDR two resistors (4.7 k Ohm and 10 k Ohm) and one transistor (547), you might have to vary the resistor values according to your LDR and light source.

This is the process from breadboard to reader mounted on the power meter:

The reader part set up on the breadboard for testing The “rear” side of the reader PCB. The pins at the top are (left to right): 5v, Pin 2, Gnd. The “front” of the reader PCB with the LDR that’ll be positioned in front of the blinking LED on the power meter. The Arduino hanging below the Raspberry Pi (temporary setup). I made a quick “shield” for the Arduino with a cable running to the reader PCB. The reader PCB mounted (with tape) on the power meter.

I used the Arduino code from jespereklund.blogspot.com and modified it slightly, so that it outputs a slightly different string in the serial console, and so it outputs it once for every new blink, instead of on a fixed schedule. My version of the sketch is here: PowerMeter_nkh_20131006.rar

This is the output of the sketch:

Count= 1 | Error: 0 | wCur= 414.79 | wAvg= 414.79 |

Count= 2 | Error: 0 | wCur= 415.94 | wAvg= 415.37 |

Count= 3 | Error: 0 | wCur= 418.51 | wAvg= 416.41 |

Count= 4 | Error: 0 | wCur= 412.65 | wAvg= 415.46 |

Count= 5 | Error: 0 | wCur= 417.05 | wAvg= 415.78 |

Count= 6 | Error: 0 | wCur= 414.79 | wAvg= 415.62 |

Count= 7 | Error: 0 | wCur= 415.85 | wAvg= 415.65 |

Currently the Arduino UNO (Arduino Uno R3 on Amazon ) is connected to a Raspberry Pi (Raspberry Pi on Amazon ), and everything is set up a bit hackish… at some point I hope to move the setup to an Arduino Yún (Arduino Yun on Amazon ), so the Raspberry Pi won’t be needed. Anyways, on te Raspberry Pi I first run a line of code that listens to the Arduino and then adds the output to a text file:

cat /dev/ttyACM0 >> poweroutput2.txt

Then I run a bash script that reads the last line of poweroutput2.txt and if it’s changed since the last read it’ll post the new values to Xively. This is powermeter.sh:

#!/bin/bash # # Listens for new input from the Arduino then writes the results to a DB #

i=”0″ count=”0″ countold=”0″

while [ $i = 0 ] do count=`tail -n 2 poweroutput2.txt | awk ‘{print $2}’` if [ "$count" != "$countold" ]; then countold=$count error=`tail -n 2 poweroutput2.txt | awk ‘{print $5}’` wCur=`tail -n 2 poweroutput2.txt | awk ‘{print $8}’` wAvg=`tail -n 2 poweroutput2.txt | awk ‘{print $11}’`

# Post result to xively wget –no-check-certificate -O – –header=”X-Http-Method-Override:put” \ –post-data “wCur,$wCur wAvg,$wAvg count,$count error,$error” \ –header “X-ApiKey: INSERT_YOUR_API_KEY ” \ –verbose \ https://api.xively.com/v2/feeds/ INSERT_YOUR_FEED_ID .csv fi done

This will populate the database at Xively and you should (after a while) see a graph something like this:

Voila, it works!

While trying to make this work, I’ve come across a number of websites that have been helpful and/or served as inspiration:

Related Posts: