Sunday, November 15, 2009

Using an Arduino to monitor gas consumption

Energy monitoring is something I have been involved in for ten years - both professionally and "socially".

First a Historical Note - this formed the basis of my "off the cuff" talk, "Gas c.1999" at Homecamp 09.

Back in 1998, whilst working for a telecommunications equipment company we needed to develop a low cost gas datalogger for automatic meter reading (AMR) applications. It needed to read gas meter pulse counts from a Schlumberger meter and transfer them via short range wireless and good old fashioned PSTN telephone modem to a remote PC based server.

The wireless transmitter had to have a battery life of 10 years, running from an AA sized lithium thionyl chloride cell, and the low data-rate modem had to be self powered from the telephone line. That, plus the need to store gas meter readings every 6 minutes for 16 months made it quite a design challenge. Oh - and it has to be intrinsically safe to use in the hazardous zone around gas meter installations.

A two part product that featured a short range radio, a telephone network connection and had to meet the requirements of Ex intrinsically safe equipment meant that dealing with the various approval bodies was a major part of this product and not the actual engineering itself.

Technically the project was a success. Fine resolution gas meter data automatically retrieved from a number of trial sites. But at £40 a unit, it wasn't the low cost the utilities wanted - when they were only paying £5 a year for their small contract army of meter readers. The project was shelved in late 1999 - conveniently on my workshop shelf.

So now we have "Gas c. 2009" - the subject of a possible future Homecamp off the cuff presentation.

In the intervening decade, things have changed somewhat. Low power radio gadgets are now commonplace, and low cost energy monitors have been available for a few years now. (Electrisave, Owl, Wattson, CurrentCost etc). PSTN modems are well and truly a thing of the past, as broadband has all but taken over as the means of getting data out from low cost sensors.

In addition to these technical changes, several cultural changes have occurred in the last decade. Thanks to the efforts of a group of pioneering computer scientists working for IBM, Hursley - it's now cool to monitor your home energy usage , and the Homecamp unconferences have shown that I'm not the only one with an interest in energy monitoring - (and I thought that I was the only geek in the village. Of course it is a global village now - with a lot more residents).

Counting pulses from a gas meter became a whole lot easier when some meter manufacturers started to provide an opto-reflective silvered zero on the least significant digit of the register. Prior to this, there was a little red rotary pointer which was almost impossible to detect reliably via optical means - unless you were lucky enough to have a Schlumberger meter which came already fitted with an RJ11 connector and a reed-switch providing a "volts free" pulse output.

Technically, Transco, the gas transport company own everything up to and including your meter. If you want to measure the pulse output, they would provide a "Chatterbox" - a Ex certified relay box, which echoed the pulsed on to your own equipment. It was powered by 4 alkaline D cells. Transco would charge you a fixed cost of about £500 to come and install this unit. This proved an obvious deterrent for all but the most determined energy monitoring enthusiasts. This issue needs to be addressed if we are to make much headway in monitoring gas consumption. Nobody wants to pay £500 for a clunky old relay box that cost £20 to make. What is needed is a low cost, Ex certified, universal pulsecounter, which anyone can fit and geta pulse count from. Wireless seems the obvious choice - an opto sensor that sticks to the front face of the register and a self contained 433MHz transmitter running of a lithium cell. Essentially fit and forget.

The turning point for me came in November 2004, when my gas utility replaced my old meter with an Actaris, "pulse ready" meter. This had the opto-reflective silvered zero, ready provisioned to accept a photo reflective sensor. Sometime in Winter 2004, I put one of these together on a small scrap of veroboard, and then wrote a pulse counter routine in PIC assembler. However, come the spring, and a new extension to the house, and the whole project was forgotten and the pulse sensor lost.

So a couple of weeks ago - I decided to make another. I still had some of the photo-reflective IR sensors left. This time the coding would be a whole lot easier. In the intervening 5 years, I had learnt just sufficient C code to be dangerous - prompted by the arrival of the ubiquitous Arduino. The timing was right an announcement of a forthcoming Homecamp in about a month, meant that I had to pull my finger out and get hacking.

Here's the photo-sensor on a small scrap of veroboard - pushed onto a couple of locating pegs conveniently provided by Actaris.

The 4 wires are 5V supply, LED supply, photo transistor output and ground. I use an additional NPN transistor to buffer the voltage output and give me a bit more gain and sensitivity. Connect this into an Arduino ADC input and start sensing digits.

So the first thing I did was write a simple ADC routine which prints the ADC value to the serial port once per second so that I could see what was coming out of the sensor. This I captured in hyperterminal (aargh) and plotted out in XL - shown below. From this I could see that there was a very characteristic voltage pulse produced when the zero went past the sensor - but additionally all of the other digits had their own signature too - so with a bit of coding I could sense to the nearest litre.

A few lines of Arduino code, I had the pulse counter running, outputting the pulse count every second along with an (approximate) H:M:S timestamp. Having let this run overnight and during the time that the boiler fires for the water heating and central heating I had 60,000 readings which could be chopped into manageable chunks and graphed in XL. However this is such a tedious way of processing data so I thought I'd try something a bit more sophisticated.

I'd had recently acquired a CurrentCost CC128, and subsequently had been using Dale Lane's CurrentCost GUI for capturing the output of the CC. Why not write a sketch for the Arduino that mimics the CC XML data format, and then squirt this into Dale's GUI? Excellent idea and about 30 minutes of hacking the XML as a serias of serial print statements (thanks to CurrentCost for making their XML output format freely available on the Web) I soon had the Arduino interloping as a gas monitoring CurrentCost.

Individually, Pulse counts can be unspeakably dull. It's only when you get two of them, which are different, and separated by a suitable time period, that they become remotely interesting. If you take a reading at the same time each day, for example, and subtract them, then you get your daily consumption. If you shorten the sampling period from 24 hours to 1 hour, you can get a much finer resolution of when your boiler fires and how much it uses. You then extend this a bit further and start taking readings every 6 minutes and the picture becomes a lot more interesting. With 6 minute resolution you can see when the gas is being used, and by logging room temperature and external temperatures on other channels, you can start to see how quickly your house warms up, for a given outside temperature as a function of the gas you burn. Every house will have it's own thermal lag characteristic and once you optimise for this you will start to use it to your advantage and make reductions to your gas consumption - well that's the plan.

Real time boiler logging is a useful tool for diagnosing the defects of an existing system. In 2005 I made the poor choice of buying a 24kW condensing boiler. Had I bought a 12kW, it would have been more than adequate for my property and spend less time idling and modulating down to a lower power output. As with over powerful, fuel guzzling V8 cars - big is not always best.

Here's the first plot I got from Dale Lane's GUI showing the boiler modulating down as the return water temperature starts to heat up. The horizontal scale is 20 minutes and the vertical scale is from 8kW to 20kW.

Each pulse on my meter is the passing of 10 litres of gas. Gas is about 39MJ/m3 so by dividing 390,000 by the time between consecutive pulses, you get the instantaneous boiler power in kilowatts. Flat out, it takes about 16 seconds between pulses for my boiler - so I can deduce that it's consuming gas at about 24.4kW

Possible extensions: I have a couple of 10K pipe clip thermistors I got from Rapid Electronics. I'll use some of the spare Arduino ADC inputs to measure these and the internal and external temperatures.

I use a 433MHz Drayton Digistat to control the boiler. Having already cracked the "boiler on " and "boiler off" wireless packets using a PIC, I can get the Arduino to control the boiler.

Write a 3 term PID controller to control the boiler temperature - one that avoids massive overshoot, and consequent waste of gas.

I'm also currently hacking a "cheapernet gateway". This is an attempt at a very low cost ethernet gateway not dissimilar to an Arduino ethernet shield which will allow low cost wireless sensors to connect to the net and provide the means for realtime, live energy monitoring. Real time means once per second data (OPS) with a 2 second delay caused by the interweb thingy.


Anonymous said...


I like the idea of getting away from BANG-BANG Control on home natural gas furnaces. A servo-controlled valve on the gas would be very good with your PID control over it.

Neat stuff.

Northfield, Minnesota


Anonymous said...

I like it, especially the bit about the RJ11 socket, and well I never, what's that under British Gas sticky label below the dials on my meter? just what I have been looking for.

I am also looking to get all the realtime energy data onto my PC.

Well done PS of UK

John said...

Are you willing to let me have the protocol details of the 433MHz Drayton Digistat

Anonymous said...

Too bad no URLs schematics, code or logging software. No mention of "this is how you do it". Bleh!