Logging statistics from a power meter with an Arduino and a LDR

My electrical power meter has a pale yellow LED that flashes once for ever watt hour used. Inspired by power.labitat.dkjespereklund.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.

Power Meter schematics (LDR Arduino)

Power Meter schematics (LDR Arduino)

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:

  1. The reader part set up on the breadboard for testing
  2. The “rear” side of the reader PCB. The pins at the top are (left to right): 5v, Pin 2, Gnd.
  3. The “front” of the reader PCB with the LDR that’ll be positioned in front of the blinking LED on the power meter.
  4. 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.
  5. 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:

Xively power meter example

Example from Xively. The peaks is an oven turning on and off

Voila, it works! :D

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

DiggDeliciousGoogle BuzzSlashdotRedditShare

Related Posts:


This entry was posted in Arduino, Electronics, Linux, Raspberry Pi and tagged , , , , , , , , . Bookmark the permalink.

8 Responses to Logging statistics from a power meter with an Arduino and a LDR

  1. michael says:

    hey, great work you made here ;)

    The openenergy project also have a arduino shield with optic reader and input for 4 pcs. CT trafo’s for non invasiv reading http://techmind.dk/arduino-singleboard/elforbrug/

  2. nkh says:

    Thanks :) I did look into the openenergy projects, but I ended up backing Neurio on Kickstarter (it also uses CT trafos, and does much more than “just” measureing the ussage). So my Arduino setup will proably end up being temporary, but it’s always fun messing with the Arduinos, and most of the time you learn something new!

  3. Søren says:

    What’s the resistance of your LDR. I only get a small response with the LDR I mounted. 0.8V , not enough to drive my DI.

    • Søren says:

      I’ll just put in a variable resistor instead of the 4.7K to “boost” the signal.

      • nkh says:

        Hi Søren,

        Glad you figured it out!

        • Søren says:

          Put in a resistor with half the resistance, didn’t give me that much more voltage with my test LED. But trying out a couple of different LEDs , it looks like it would be a good idea to put in a trimpot in order to properly adjust things.

          As far as I can see on the datasheet for a 547, it can endure quite high voltages. So can you see any problems in using for example a 24V supply instead of the 5V supply ?

  4. Søren says:

    Nice. That actually did it. Although seems like your circuit turns the 547 ON when there is no light and OFF when there is a light, is that correctly understood. So it is sort of needs to count a pulse on a falling edge?

  5. nkh says:

    So, it turns out that Xively probably isn’t the best (free) place to store the data anymore. If your data is now stuck there you can get them out like this: Export data from Xively to CSV with a bash script

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>