# Dumb Meters… monitoring with Arduino

Armed with the knowledge about how meters have a ‘pulse’, I was then able to start building a sensor and simple Arduino sketch (C++ code) to calculate my electricity consumption.

### Power v’s Energy

Before I started, I needed to understand some basic terminology about electricity! I did not fully appreciate the difference between Power and Energy. In physics Power is the rate at which energy is consumed per unit time, expressed in watts or kilowatts. Energy is the amount of power consumed, expressed in watt-hours or kilowatt-hours (kWh). The simplest way I can start to describe this draw a parallel with a coal fire:

A fire burns coal at a rate of 40 pieces an hour (this is the Power of the fire). Therefore after 1 day the fire would have burned 960 pieces of coal (this is the amount of Energy used).

* In this example above the unit of energy is a piece of coal.

Then relating this to a light bulb:

A 40W light bulb consumes 0.04 units of electricity an hour (this is the Power of the bulb). Therefore after 1 day the bulb would have consumed 0.96 units of electricity (this is the amount of Energy used).

* In this example above the unit of energy is a kilowatt-hour (kWh), however other units of energy are watt-hour (Wh), joules (J), calories (cal) and British thermal unit (BTU).

You can convert between Power and Energy with some simple maths:

• Energy = Power * Time     (kWh = kW * hour)
• Power = Energy / Time     (kW = kWh / hour)

More on this subject can be found on this great website here.

### Round like a circle in a wheel

The reason why it is important to understand the difference between Power and Energy is because we need to understand what is happening each pulse (spin of the disk) on my electricity meter. On the front of my electricity meter it states that the disk spins at ‘200 rev/kWh’, therefore we can now calculate that for each revolution of the disk 0.005 kWh (Energy) have been consumed. We can know calculate that if the only thing I has switched on in the house was a single 40W light bulb the disk will rotate a total of 8 times in an hour (0.04 / 0.005 = 8). This will come in useful soon in my Arduino code…

### Putting the finger on the pulse

The next challenge is detecting the rotations of the disk on my Electricity meter. Along the edge of the disk is a small black vertical line which only occurs at one point in its rotation, the remainder of the disks edge is shiny silver. This enables me to use a reflective object sensor (Optek OPB704), “the sensor consists of an (LED) infrared emitting diode and a (NPN) photo-transistor mounted side-by-side in a black plastic housing. The photo-transistor responds (reports high) to radiation from the emitter only when a reflective objects passed within its field of view”… this is perfect for detecting the reflective edge of the edge of the disk (and the non reflective black stripe).

### Connecting the sensor to the Arduino

The next task is hooking the sensor to an Arduino. The OPB704 sensor has 4 pins (wires) on it which were connected as per the diagram to the right or the table below:

OPB704 Arduino Notes
Pin 1 Pin +5V LED Anode
Pin 2 Pin GND LED Cathode [via a 470 ohm resistor]
Pin 3 Pin A0 Photo-transistor Emitter
Pin 4 Pin GND Photo-transistor Collector

* Finally there is a 100 kilo-ohm pull down resistor placed between Pins 1 and 3 of the OPB704.

### Code for the Arduino

The final task is writing the sketch for the Arduino. The basic 3 tasks the Arduino need to perform are:

1. Detect Pulse – Continually loop round monitoring the Analog (A0) pin listening for when it changes from high to low (or in the example below, when the sensor reports over a given threshold), detecting the black stripe of the disk passes the sensor.

2. Calculate Power and Energy – Once a pulse is detected the Arduino then needs to carry out a set of calculations using timers, to work out power and energy values based on the mathematics discussed above.

3. Output Results – The result of its calculations are to be output to the screen (via Serial port), displaying power, energy used, pulse duration and sensor values. I have also connected a standard LED to Pin 9 which also flashed when a pulse (disk revolution) is detected. The following code is the Arduino sketch to perform the above tasks:

```#include <time.h>

// Setup variables
int sensor         = A0;    // OPB704 sensor
int led            = 9;     // Output LED
int threshold      = 800;   // Sensor sensitivity
double pulsePerkWh = 0.005; // Number of pulses per kWh
// found on the meter
// (e.g 200 rev/kWh)
boolean triggered  = 0;     // Pulse on/off
long pulseCount    = 0;     // Number of pulses
float timePulse, timeLast;  // Pulse timers
float duration;             // Used to calculate
power double kW, kWh;       // Power and energy

void setup() {
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop() {
/*
Detect Pulse - Continually loop round monitoring the
Analog (A0) pin listening for  when the sensor reports
over the given threshold.
*/
if ((reading > threshold) && (triggered == 0)) {
// New pulse detected - calculate power and energy
pulse();
triggered = 1;
} else if ((reading > threshold) && (triggered == 1)) {
// Waiting for existing pulse to finish
// Do nothing
triggered = 1;
} else {
// Waiting for new pulse
// Do nothing
triggered = 0;
}
output();
delay(50);
}

void pulse() {
/*
Calculate Power and Energy - Once a pulse is detected the
Arduino then needs to carry out a set of calculations
using timers, to work out power and energy values.
*/
// Calculate pulse times
timeLast = timePulse;
timePulse = now();
duration = (timePulse - timeLast);

// Calculate power and energy
if (duration > 0) {
pulseCount++;
kW = pulsePerkWh / (duration / 3600.0);
kWh = pulseCount * pulsePerkWh;
} else {
// Pulse too short
// Do nothing
}
}

void output() {
/*
Output Results - The result of its calculations are to
be output to the screen (via Serial port), displaying
power, energy used, pulse duration and sensor values.
An LED is also connected to Pin 9 which also flashed
when a pulse is detected.
*/
// Print results to serial
Serial.print(" | Power (kW): ");
Serial.print(kW, 6);
Serial.print(" | Energy (kWh): ");
Serial.print(kWh, 6);
Serial.print(" | Pulse Duration: ");
Serial.print(duration, 0);
Serial.print(" | Pulse Count: ");
Serial.print(pulseCount);
Serial.println(" | ");

// Switch on/off LED
digitalWrite(led, triggered);
}
```

The following is the result:

``` | Power (kW): 0.000000 | Energy (kWh): 0.000000 | Pulse Duration: 0 | Pulse Count: 0 | Reading: 968 |
| Power (kW): 0.000000 | Energy (kWh): 0.000000 | Pulse Duration: 0 | Pulse Count: 0 | Reading: 998 |
| Power (kW): 0.000000 | Energy (kWh): 0.000000 | Pulse Duration: 0 | Pulse Count: 0 | Reading: 963 |
| Power (kW): 0.000000 | Energy (kWh): 0.000000 | Pulse Duration: 0 | Pulse Count: 0 | Reading: 386 |
| Power (kW): 3.000000 | Energy (kWh): 0.005000 | Pulse Duration: 6 | Pulse Count: 1 | Reading: 935 |
| Power (kW): 3.000000 | Energy (kWh): 0.005000 | Pulse Duration: 6 | Pulse Count: 1 | Reading: 979 |
| Power (kW): 3.000000 | Energy (kWh): 0.005000 | Pulse Duration: 6 | Pulse Count: 1 | Reading: 982 |
```

To further this, my intention next is to connect an XBee (ZigBee) module to the Arduino so I can transmit the above power readings via wireless to another computer or device.