Arduinometer, anyone?

Standard

On Sunday, I published an article showing how I’d built an Arduino-powered ethernet gas meter, to publish real-time gas meter readings to Pachube. Thanks to James Taylor (http://twitter.com/jtonline) who re-tweeted my initial tweet highlighting it as potentially of interest to the #homecamp folks, within a few hours some 200 people had read the article, and I’ve received some great feedback and inclusion on several sites on the web. Not bad for my first article of this kind, I thought!

I’ve been thinking that I should really extend this little venture into a project that adds support for:

  • Tracking electricity, gas and water meter values (all from the same Arduino), using cheap sensors
  • Add support for pulse meters, as well as what I’ve dubbed ‘reflective’ meters (not sure of the proper name, but these are the meters with the little reflective discs of metal on one of the dials).

I’ve created a project on Google Code for anyone who’s interested in following (there’s not a lot there just yet, though). I’ve got a busy few weeks ahead, but will do what I can in the interim. It’s look like June will be the first real chance I have to sit down and extend the code. If anyone’s interested in getting involved, or helping out in any way, please get in touch. I’m still fairly new to the world of Arduino and physical computing, so I’m still learning too – all feedback is appreciated!

How to build a web connected gas meter with your Arduino

Standard

A few months back, I bought a Current Cost electricity meter, hacked that and started pushing the data up to Pachube and displaying real-time electricity usage right here on my blog. But, electricity is only one part of the picture – we also use gas for our heating and hot water systems, and I wanted to track our usage of this too. The catch, of course, is that there doesn’t appear to be a consumer product on the market to do this for me, and I really wanted an excuse to go out and buy an Arduino and start playing with that.

My goal for this project was to hook the gas meter up to Pachube, using EEML as the format:

<eeml xmlns='http://www.eeml.org/xsd/005' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd'>
<environment>
<location exposure='indoor' domain='physical' disposition='fixed'>
<name>London, United Kingdom</name>
</location>
<data id='0'>
<tag>gas</tag><tag>cubic metres</tag>
<value>2</value>
</data>
<data id='1'>
<tag>temperature</tag><tag>degrees</tag><tag>celsius</tag>
<value>28</value></data>
</environment>
</eeml>

What follows is how I set about using my Arduino, an Ethernet Shield and various other parts in order to build the gas meter!

Introduction

Those of you who have been following my previous blog posts will know that I’ve been working on this for a while, finding a few minutes spare to tinker as and when I can. This post is the promised amalgamation of all those recent posts.

The completed Arduino Ethernet Gas Meter unit

The completed Arduino Ethernet Gas Meter unit

So, this is the completed unit (picture, left). Whilst this isn’t intended as a complete step-by-step tutorial, my aim is to provide enough information to enable somebody else to build one of these.

To keep things nice and neat, I mounted the Arduino, Ethernet- and Proto- shields into a plastic project box from Maplin. I drilled a ventillation hole and added a mini CPU fan connected to a temperature sensor to help keep the unit cool. I also wanted to add one because it’s nice to tinker around with new sensors and I hadn’t yet played with a temp sensor 😉

How does it work?

The main sensor on this unit is a photo-reflector, which is a fancy name for an infrared LED and an infrared sensor in one assembly. When the infrared sensor detects a reflection from the infrared LED, the strength of that reflection is fed back into the Arduino. I used a pull-up resistor circuit to connect this sensor to a digital input pin, which is switched to LOW when a reflection is detected. Because my gas meter has a little reflective metal disc on one of the dials, this gives me a nice reflection to count when the dial passes.

The Arduino then keeps a tally of all the reflections it has counted, which is accessible in XML (conformant to the EEML standard) over the web via the Ethernet Shield. To keep my meter in synch with the actual gas meter, one can pass a parameter via the HTTP querystring to the gas meter module to set it’s current value (useful, for instance, after a power cut or if you need to disconnect it). Because the unit is accessible over the public internet (without authentication), I only allow the querystring parameter to reset the meter value if a certain digital pin is ‘HIGH’ – a crude (yet effective) kind of physical security.

For fun, I then added a mini-CPU fan connected to a temperature sensor which causes the fan to start up when the temperature inside the unit gets a bit too toasty. It then kicks in until it has successfully dropped the temperature to within the threshold amount. The CPU fan runs off 12v, which the Arduino will happily produce – but it gave me an opportunity to use a transistor to switch the 12V supply on/off based on the state of a digital output pin @ 5 volts.

Contents

  1. Before you begin: finding out if this will this work for you!
  2. Shopping list – what you need to buy and where to get it
  3. Constructing the sensor circuit
  4. The sketch
  5. Connect it all up, fine tune and test
  6. Acknowledgements

Before you begin: check your gas meter

Please note: this project involves mounting sensing equipment in a non-permanent, non-invasive way to your household gas meter, based on my experience and research. I strongly encourage you to check for yourself whether these instructions are suitable for you, and, if so, only to proceed if you feel competent enough to connect this unit safely. This information is provided ‘as is’, and you use it entirely at your own risk.

This kit will only work on certain types of gas meter. Firstly, you must have the odometer-style gas meter (these are the ones with rotary dials with numbers on). Secondly, one of the dials, usually the dial representing a 100th of a Cubic Metre should have a little reflective disk between two of the digits (on mine, that is digit 5 and 6). I’ve no doubt that you could probably modify my design here to work with another type of meter, but you may need to change the photo reflector to either a pulse counter or something else appropriate. The underlying code to count the pulses and make the Arduino web-connectable should be the same.

Shopping list

1 x Arduino Duemillanove (or similar) ….. £23.81
1 x Sparkfun Ethernet Shield … £33.35
1 x 9v DC Power Supply with a 2.1mm centre-positive connector (Maplin: GS74R) … £7.99
1 x 220 ohm resistor
1 x 10K ohm resistor
1 x Transistor (if you plan to add the fan circuit and you use a 12V fan)
1 x Fairchild QRB1134 IR Photoreflector …. £2.62
1 x Project Box (eg. Maplin XYZXYZ) …. £2.99
1 x Mini CPU Fan (eg. Maplin) …. £4.99
1 x Breadboard – OR –
1 x Nuelectronics Protoshield …. £4.99

Total cost to build from scracth: £80.74
Total cost if you already have the Arduino, an ethernet shield and a few other bits and bobs: < £20.

You will also need/want the following tools to hand:

  • Soldering iron and lead-free solder (well, use whatever you like, but lead-free is better for you and the environment!)
  • Wire cutters
  • Some suitable wire/jump leads etc
  • A pair of “helping hands” if you have them

Goals

Before starting out, I had a quick think about the project’s desired behaviors. That way, coding it up would be a little easier because I’d know what I was supposed to be coding 🙂

Here they are:

  • Connect the photo reflector to a digital input
    • When the input goes LOW, increment a counter
  • Check the current temperature inside the unit
    • When the temperature is above the threshold, turn on the CPU fan.
    • When the temperature falls below the threshold, turn off the CPU fan.
  • Check if a web client is connecting.
    • Read the query string.
    • If the query string value contains “?x=”, change the internal count to the value of x but only when a digital pin is set to HIGH (so web users can’t set it for us!).
    • If the query string does not contain the above element, generate EEML containing the data that we need.

With these points in mind, I started to construct the sensor circuit.

Constructing the sensor circuit and preparing the project box

Ok, so first things first – let’s start by preparing the project box. Drill two holes into the box – one on top, and another on the side (as pictured):

Prepared Project Box

Prepared Project Box

It doesn’t really matter what size you make the hole on the side providing that there is sufficient room to fit a power cable, ethernet cable and the 4 wires from the photoreflector through. The hole on top should be just a shade smaller than the size of the fan that you have so that you can mount it to the inside of the case properly.

Next up, we’re going to construct the sensor circuit board. You can use either a breadboard, or – as I have used – an unassembled protoshield board from nuelectronics. I have used Fritzing to create a diagram of the entire circuit (shown below). I’ve also produced a schematic to accompany the diagram. I’m not quite sure how correct it is though, so be warned! (Corrections on a post-card, please!).

A tidier view of the wiring

A tidier view of the wiring

Schematic

Schematic

Start by soldering two wires – one each to the 5V rail and the GND rail respectively. Next, solder the resistors, transistor and temperature sensor onto the board, according to the schematic shown. Take care when soldering the transistor and temperature sensor to the board: the heat from your soldering iron may damage them.

Next, pass the bare sensor wires through the outside hole that you drilled and then solder them onto the board in the posistions shown. Make sure you use enough wire – you’ll want about 11-13cm for each connection you make (you can trim the non-soldered ends later).  Finally, solder the positive and ground wires for the CPU fan onto your board.

If you’re new to soldering (as I am), then do take extra care that your connections are good and don’t bridge multiple points on the board. If you do, you’ll likely create a short circuit and that will drive you nuts, trust me! When you’re done, you should end up with something that looks a bit like this:

The finished sensor circuit

The finished sensor circuit

The sketch

Now that you’ve done the hard part, here comes the copy and paste! Take the sketch below and place it into your Arduino suite. You’ll need to modify the following:

  • Line 13: Set your own unique MAC address
  • Line 14: Set your own unique IP address (accessible from computers on your network)
  • Line 15: Set your router’s gateway address

#include “Ethernet2.h”
#include “WString.h”
#include “stdio.h”

// — [ Pin setup ] —
int sensorPin = 5;
int ledPin = 9;
int fanPin = 7;
int tempPin = 3;
int unlockPin = 8;

// — [ Ethernet setup ] —
static uint8_t mac[6] = { 0x02, 0xAA, 0xAA, 0xCC, 0x00, 0x22 };
static uint8_t ip[4] = { 192, 168, 1, 99 };
static uint8_t gateway[4] = { 192, 168, 1, 2 };
int serverPort = 80;

String url = String(25);
int maxLength=25;

// — [ Variables to control when the unit fan kicks in/cuts out… ] —
float maxTemp = 26.0;
float minTemp = 24.0;

// — [ Variables to store our sensor values ] —
volatile int totalTicks = 0;
float tempc = 0.0;
int maxi = -100, mini = 100;

// — [ Misc. vars ] —
float inputVolts = 5.01;
int previousState = -2;
int delayMs = 1500;
int i;
boolean lock;

Server server(serverPort);

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(unlockPin, INPUT);
previousState = digitalRead(sensorPin);
Serial.begin(115200); // Use serial for debugging locally…
Serial.println(“Ardugas server saying: Howdy!”);
Ethernet.begin(mac, ip, gateway);
server.begin();
}

void loop() {
checkUnitTemp(); // Take action based on unit temp
checkSensor(); // Check the gas meter sensor
getCurrentTemperature(); // Get the unit temperature
listenWeb(); // Handle any web connections
digitalWrite(ledPin, !digitalRead(sensorPin)); // Light LED if sensor is LOW
}

// Serve pachube EEML and accept a querystring param to set the current value
void listenWeb() {

boolean read_url = true;
boolean unlock = false;

Client client = server.available();
if (client) {

Serial.println(“Ethernet client connected…”);

// an http request ends with a blank line
boolean current_line_is_blank = true;
int valuesChanged = 0;
while (client.connected()) {

if (client.available()) {

char c = client.read();
if (url.length() < maxLength) { url.append(c); } if (c == '\n' && current_line_is_blank) { // send a standard http response header, but change the response type to text/xml client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/xml"); client.println(); // This is our unlock pin. When set to high, we allow the querystring parameter 'x' to force-set our meter value. if (digitalRead(unlockPin) == HIGH) { unlock = true; } if (url.contains("x") && unlock) { Serial.println(url); String v = String(10); int startIndex = url.indexOf('=')+1; int stopIndex = url.indexOf('H'); v = url.substring(startIndex, stopIndex); Serial.println(v); totalTicks = atoi(v); } int t = tempc; // EEML client.println(""); client.println(""); client.println(""); client.println("Ardugas Server"); client.println(""); // Gas Meter Reading client.println(""); client.println("gascubic metres"); client.print(""); client.print(totalTicks); client.println(""); client.println(""); client.println(""); client.println("temperaturedegreescelsius"); client.print(""); client.print(t); client.print(""); client.println(""); client.println(""); client.println(""); client.println(); break; } if (c == '\n') { // we're starting a new line current_line_is_blank = true; } else if (c != '\r') { // we've gotten a character on the current line current_line_is_blank = false; } } } // give the web browser time to receive the data url = ""; delay(5); client.stop(); } } // Switch the cooling fan on if it's too hot! void checkUnitTemp() { if (tempc >= maxTemp) {
digitalWrite(fanPin, HIGH);
}
if (tempc maxi) {maxi = tempc;} // record max temperature
if(tempc < mini) {mini = tempc;} // record min temperature } // Count sensor values void checkSensor() { int currentState = digitalRead(sensorPin); if (currentState == LOW && previousState == HIGH) { if (!lock) { lock = true; delay(delayMs); totalTicks++; } } if (currentState == HIGH && previousState == LOW) { lock = false; } previousState = currentState; } [/sourcecode] Attach the Ethernet Shield to your Arduino, attach your sensor circuit, upload the sketch and then test it out according to the behaviours mapped out above. For testing, I used a a square of foil and passed it under the sensor. This should cause the sensor pin to swith to LOW, and increment the counter by one. To test if your cooling fan circuit is working, change the threshold values in the sketch above. Connect it all up, fine tune and test

Now for the exciting bit… it’s time to connect it up to your gas meter. Now, my gas meter was really quite awkward because there is a curved plastic bezel cover over the meter dials, which made it had to find the best point to get a reflection. That is, in the end, why I opted for a very sturdy and reliable ‘blu-tac’ mount! I think I’ll definitely replace that with a bracket at some point in the futre though.

 [Photo to follow shortly]

Now, connect up your circuit. Make sure you’ve got your test LED inserted into the ledPin you defined in the sketch. This will light up when your sensor detects a reflection. At this point it would be good if you have someone to help you by turning your hot water on/off, or, turn your gas hob or heating on for the duration of the setup. The object here is to have the dial start to turn so that you can find the best position for your sensor. Begin by holding the photoreflector just over the dial with the reflective disc. Wait for it to turn one revolution. The LED should light when the sensor detects a reflection – so make sure that it only lights up when the dial is directly underneath it. As the disc approaches the sensor, there is a period of ‘noisy reflection’ that my sketch accounts for by forcing a delay after the first detection. This prevents artifical inflation of the meter count.

Once you’ve found the ideal position, firmly fix it in place – I used blu tac quite successfully but you can use whatever you need to, providing it doesn’t pierce or physically interfere with the meter in any way. Now that it’s attached, you need to program your arduino with your current gas meter value.  To enable this, you must first connect a jumper lead from 5V to resetPin. Then, browse to the IP address you set, but add the querystring value ‘x=[your meter reading]’. For example, to set a meter reading of 894.121, and your IP address is 192.168.1.99, your URL would look like: http://192.168.1.99/?x=894121.

So, that’s it! I’ve been sitting on this article for about a week now, but due to work and other commitments I’ve not really had as much time as I’d have liked to work on it – so I thought I’d just get it out there and take as much feedback as possible from my readers to help improve it.

I hope you enjoyed reading and that you can derive some use from this.

Thanks!

Assembling the Sparkfun Protoshield for Arduino

Standard

Yesterday I finished assembling the Sparkfun Arduino Protoshield (v2) (if you’re wondering what a ‘Protoshield’ is, be sure to read the footer of this post for a quick explanation). I chose to purchase the Protoshield kit so that I could deploy my Gas Meter-reading gadgetry in a relatively small, neat package.

I previously purchased the Nuelectronics Protoshield kit – which is basically a variation on the Sparkfun model. Although there wasn’t anything wrong with the Nuelectronics version, in my experience working with Nuelectronics gear, you have to be a bit more proficient in the field of electronics etc., i.e. a higher level of competance is assumed. Whereas the Sparkfun gear, although used by both novices and professionals, is just that bit more refined and – in my humble opinion – higher quality. I actually broke the Nuelectronics protoshield on my first attempt, and it was at that point I decided to go with the flow and buy the Sparkfun model.

Although the board is made by Sparkfun, I didn’t actually buy mine from them directly as the shipping costs to the UK are quite high (at least the cost of the board!). Instead, I sourced the official board from SK Pang Electronics (a UK-based firm I hadn’t used before), for a very reasonable £14.84 plus shipping. I will certainly order from SK Pang again, by the way, since the item arrived next day via Recorded Delivery – perfect for the tinkerer with a weekend on the way! 🙂

Assembly

Being a novice in the field of electronics, I wasn’t too confident that I’d be able to assemble it – soldering components onto a PCB is a tad intimidating if you’ve never really soldered before (I’d had only limited experience soldering and that wasn’t particularly successful!)! That said though, after reading a few basic tips on how to solder, I gave it a shot and about 30 minutes later the entire PCB was assembled. What a fantastic kit.

Although the kit ships disassembled, SparkFun do not give you assembly instructions. Instead, I referred to Bob Gallop’s excellent tutorial. Bob’s instructions are based on the V1 protoshield, which differs only very slightly from the newer V2 that I’d purchased. His tutorial also offers some great tips on how to keep components in place while you’re trying to solder them – so it was well worth the read, and the photography makes it all the more easier to follow along.

One thing I would add though is that I am incredibly pleased with the pair of Helping Hands I’d purchased from Maplin a few weeks back – a necessity for PCB soldering.

The finished board

Here’s a photo of the Protoshield without the breadboard attached. You could of course leave the breadboard out entirely if you wanted to, and solder directly onto the shield. As I want to retain the flexibility of the shield though, I’ll add the breadboard on in a moment. In this picture, I’ve yet to solder on the single male pins JC1, JC2 and JC3 and also the BlueSMIiRF  female headers:

Protoshield without Breadboard

And when mounted on top of the Arduino, with a mini-breadboard, the remaining pins and BlueSMiRF headers added:

Protoshield with Breadboard

All in all, thoroughly pleased with myself 🙂

Not only did I have fun while assembling the board, I learned a few new skills and have boosted my confidence in the world of tinkering. Still, a long way to go yet! But at least now I have a better way of continuing my Gas Meter project.

What is a Protoshield?

If you’re new to the world of Arduino – as I am – then you might be wondering what a ‘protoshield’ is and what they do. ‘Shields’ are essentially printed circuit boards that are designed to slot on top of your Arduino board, and provide some extra capabilities. There are all kinds of shields that you can buy: some offer radio transmission/reception capabilities and others, like my Protoshield, simply offer you the flexibility to affix a tiny breadboard to the top of your Arduino to make protoyping that much easier.

Part 2a: The Sensor Bar (Arduino Gas Meter project)

Standard

Ok, so here we are a few days later than expected. I’ve been a bit too busy to tinker lately, but I’ve made some interesting progress. I’ll keep this post brief…

Firstly, the phototransistor I ordered from Maplin turned out to be of limited use since it’s ‘detection range’ is a little bit too small to work with the gas meter properly. Also, due to the siting of the gas meter within my property (in a cupboard, with a chair in front of it!), I needed the sensor to be remote to the Arduino. So I ordered a Fairchild Photoreflector from Active Robots. On the surface, it’s everything I need (a photoreflector with wires already attached). Very inexpensive.

Fairchild Photoreflector

Simply dropped this right in place of the existing photoreflector and it works a treat.

Experiments with mounting the sensor to the gas meter

So far, having attempted to mount the sensor on a little bracket in front of the meter, I can’t get an accurate reflection (annoyingly there is a clear plastic cover over the reflective disc which is at a 45 degree angle – sort of – and curved). Having run out of Blu-tac on a previous tinkering experiment, I’ll have to go get some more tomorrow in order to experiment with that.

Reading the meter

I just got my hands on a Nuelectronics LCD Shield so I’ll be setting that up soon to display the meter value.

More to follow soon…

Reading a gas meter with an Arduino, Part 2

Standard

In Part 1 of Reading a Gas Meter with an Arduino, I outlined in brief what I wanted to achieve and how I thought I might go about it.

In this second article, I detail my experiences using the photo-reflector purchased from Maplin. Since I haven’t yet created any circuits more complicated than some basic traffic lights, this would be my first real experience using this type of sensor in a circuit. I had a feeling when starting that I’d be needing to use the pull-down resistor type set-up on one of the sensor pins, and it turns out I was right – special thanks to everyone on the Arduino Forums for assisting.

It turns out, with a little help from the Arduino forums, it was pretty easy (it always is when you know how!). Having first connected the sensor to a digital input, and reading the value from the digital pin in the loop() method, it quickly became clear that trying to count one pass of the reflective disc on the gas meter in this manner wouldn’t work. Adding delay statements wouldn’t work either – I might miss the disc moving past the sensor as the speed it rotates is variable (depending on the amount of gas being consumed). If you’re not sure why this won’t work, think of the Arduino and photo-reflector as a high-speed camera: taking many frames per second the second a reflection is detected. You might receive – for argument’s sake – 1,000 ‘frames’ while the gas meter disc is passing under the sensor, but how do you know – in code – which one to ‘act’ upon and which to ignore? Clearly, you can’t send a thousand ‘pulses’ to your data logger, because your data will all be wrong. This dilemma only came up because in my limited knowledge of the Arduino and the C++ implementation it uses meant that I was trying to stuff all this “detection logic” into the loop() statement.

Enter the Hardware Interrupt… the simplest answer to the problem

In my traditional .NET coding, I’d simply use an ‘event’ to achieve what I wanted, and then write some minimal state-checking code. On the Arduino, however, I have learned that the hardware interrupt is basically very similar (but a bit cooler, actually!). What’s even better is that I don’t even need to bother with any complex code to determine whether or not the sensor is still sensing the same revolution of the disc: when you initialise the interrupt, you state whether you want it to be triggered when a certain condition is met, i.e. any state change, from low to high, from high to low etc. This really nifty feature is exactly what I needed.

Refer to line 7 of the sketch sample below to see how easy it is to register the interrupt. In this example, I simply use the interrupt to turn on an LED whenever the photo-reflector is LOW, i.e. it is detecting a reflection. In later articles, I’ll be changing this to do some logging. The reason for this, is simply that this hardware interrupt is fired only ONCE – and not again until the state changes.

Finding out what’s going on under the hood

I use the Serial library to output ‘debug’ information to help me figure out what’s happening inside the Arduino. It’s not necessary for reading values from the photo-reflector, and at a later stage in this project I’ll probably remove the debug information as I’ll need to be sending the information that is read from the gas meter back to the PC somehow.

The Sketch


int ledPin = 13;                     // select the pin for the LED
volatile int state = LOW;            // remember the current state

void setup() {
  pinMode(ledPin, OUTPUT);            // declare the ledPin as an OUTPUT
  Serial.begin(9600);
  attachInterrupt(0, check, CHANGE);  // attach an interrupt (interrupt 0 = digital pin 2)
}

void loop() {
  digitalWrite(ledPin, state);
}

void check() {    // Checks for feedback from the phototransistor
  state = !state;
  Serial.print("State changed to: ");
  Serial.print(state);
  Serial.print(".\n");
}

Photograph

Schematic

I have attempted to re-create the above in schematic form, using Fritzing. As mentioned in earlier posts, Fritzing is a beta tool and it doesn’t currently have symbols for all the electrical components one might use. Having searched the web for a suitable circuit symbol for my photoreflector, I can’t find one. I suspect that because, although the photo-reflector is an ‘all-in-one’ design, it is actually just comprised of an LED and a photosensitive transistor:

Arduino schematic showing placement of maplin photo-reflector

Next Steps…

Now that I have figured out how to interpret a value from a photo-reflector within the Arduino, what I need to do is build a prototype sensor ‘bar’ that I can mount to the gas meter. The next post on this subject will probably be just that – how I made (or am making!) the sensor bar. One point I’ll be bearing in mind is that I’d like to site the Arduino remotely from the sensor arm, and I’d like to be able to collect data from other sensors attached to the same board. Thinking ‘out loud’ as I go really, but I promise to write a proper ‘How-To’ article when I’m on the other side of the learning curve!

Reading a gas meter with an Arduino, Part 1

Standard

Ok, since I received my Arduino, I have been playing around with some simple projects in an effort to understand more about the way the Arduino board works, and to gather some of the basic electronic skills necessary to work with it. So now, I want to turn my attention to something a little more in tune with my reasons for actually buying the Arduino in the first place: home automation.

After a trip to the gas meter cupboard, unfortunately my gas meter is not the type that outputs a digital ‘pulse’ at regular intervals: it’s an odometer type. However, usefully, I did discover that it has a little reflective disc between the digit ‘6’ and ‘7’, which could presumably be read with some kind of reflection sensor. I did some digging around to see who else on the web has built their own gas-meter readers, and found BWired.nl. This is an awesome site – certainly way beyond what most of us mere mortals could aspire to for our home automation setups! Anyway, on this page, the owner describes using a ‘CNY70 Reflective Optical Sensor with Transistor Output’. After some digging around, I found that Maplin has something which (I think) might be similar here – it’s called “Photoreflector SY-CR102” and it costs £0.79! This neat little device includes a photo-emitting diode and a phototransistor, which should do the trick.

I’ll go down to the store and pickup the Photoreflector later today, and have a go at playing with it over the coming days. Essentially what I need to do is figure out how the sensor responds whenever the reflective disk of the gas meter passes the sensor aperture. In theory then, at least, it’s then going to be a case of simply counting the ‘pulses’ from the sensor. Once I can do that, given that the dial will rotate once a known quantity of gas has been consumed, I should be able to create a very accurate sensor device that is synchronous with the gas meter itself.

Here’s how I imagine the project to unfold:

I’m quite excited about this – it’s such a steep learning curve though! 🙂

/Rich

Arduino Traffic Lights – Take Two

Standard

Ok, so after starting my first project (yesterday: Arduino timer-based traffic lights), I decided to expand upon the concept today. In keeping with the traffic light theme (it seemed like a good idea!), I added a few extra LEDs and a digital switch so that someone can ‘cross the road’ by pressing the button to change the lights to red.

Scenario: traffic and crossing lights

Instead of having the lights cycle from red – amber – green infinitely as in the previous project, this time I wanted the lights to go through a fake initialise sequence (i.e. turn on the Arduino board, have the red LED blink 5 times, then have all three traffic lights switch on, as an example) then to step to amber, then green. To make things a little more interesting, I also added two new LEDs: a ‘Green Man’ and a ‘Red Man’ light – to help our little pedestrians figure out when it’s safe to cross the road. 🙂

The traffic lights should remain on green until a ‘pedestrian’ presses the crossing button, at which point we should go to amber, then red. We should wait a little bit for the pedestrian to cross, blink the green man light a few times to indicate that the pedestrian needs to get out of the road, followed by blinking the Amber light a few times to alert the traffic to get ready, then go back to a green traffic light and a red man light.

The lights and crossing should obey the following rules:

  • When a red traffic light is displayed, the green man light should be on
  • When a green traffic light is displayed, the red man light should be on

Common sense really! 🙂

So our sequence of events needs to resemble the following:

  • Initialise the lights (blink red LED 5 times, then turn on all three traffic lights)
  • Show amber traffic light
  • Show green traffic light and wait on green
    • When the crossing button is pressed:
      • Show the amber traffic light
      • Show the red traffic light
      • Show the green man
      • Wait a preset amount of time to let the pedestrians cross
      • Blink the green man light a few times
      • Blink the amber traffic light a few times
      • Extinguish the green man light and turn on the red man light
    • Resume green traffic light
    • Repeat!

After about a half hour tinkering, I’d stripped the breadboard and added a digital switch and two new LEDs (for the crossing indicators). The Arduino sketch was modified to include controls for the crossing LEDs, and the continuous alternating traffic light sequence was replaced with a solid green LED (which remains green until the switch is pressed).

A quick observation: the digital switch and pull-down resistors

My switch is connected to a digital input. Initially, this resulted in an intermittent switch: picking up HIGH or LOW sometimes worked, and sometimes didn’t. I figured out after a little bit of Googling that I actually needed to connect the switch in a mini ‘pull-down resistor circuit’ which now works brilliantly. I will admit, I have yet to fully understand the principles behind why this works, but that’s something I will continue to look into (besides wanting to just have fun with the Arduino, one of my goals is to learn more about the basic electrical principles regarding physical computing).

The board setup:

Pedestrian Crossing with Traffic Lights

And the wiring diagram:

Now then! The lovely Fritzing program I used to create the above diagram (which I absolutely love), is so new, that it doesn’t (yet) have a switch symbol for the type of switch I have used. Hence, with my very limited knowledge of electrical schematics, I’ve used the standard switch symbol and tried to represent how I’ve actually wired the switch up. Here’s a close-up photograph of what I’ve actually done, in the hopes that someone can correct me:

Switch Detail

I found this technique here.

Video:

The Sketch

This is a bit messy at the moment, but I’ll clean it up tomorrow (I’ve used generic on/off methods to set light states instead of just using a ‘setRed’ or ‘setGreen’ method, which encapsulates the code to extinguish lights that aren’t used by that mode, for instance) and also remove some of the ‘debugging’ serial code I wrote:

// Configure the light pins
int redLed = 2;
int yelLed = 3;
int grnLed = 4;
int redManLed = 8;
int grnManLed = 7;

// Program variables
int lightState = 0;
int crossingRequestSwitchPin = 13; // The pin our crossing button is connected to
int switchStatePin = 6;

// Traffic Light Variables
int timerDelayTime = 3000; // The ms between each light change when running in timer mode
int crossingTime = 5000;

// Setup
void setup() {
pinMode(redLed, OUTPUT);
pinMode(yelLed, OUTPUT);
pinMode(grnLed, OUTPUT);
pinMode(switchStatePin, OUTPUT);
pinMode(grnManLed, OUTPUT);
pinMode(redManLed, OUTPUT);
pinMode(crossingRequestSwitchPin, INPUT);
Serial.begin(9600); // Initialise the serial port for debugging
changeLightState(-1); // Set the light state to “initialise”.
}

void loop() {
if (lightState == -1) {
doBeginSequence();
changeLightState(0); // Now our “initialise” phase is done, increment the light state
}
if (lightState == 0) {
lightOn(redLed);
lightOn(yelLed);
lightOn(grnLed);
delay(1500);
startLightsOnTimer();
}
}

void doBeginSequence() { // Handles the lighting sequence for lights that have just been started.
Serial.println(“Begin sequence was requested.”);
blinkLed(redLed, 5, 500);
}

void startLightsOnTimer() {

Serial.println(“Starting timed-sequence.”);
allLightsOff(); // Turn all lights off

while ( lightState == 0 ) {

lightOn(grnLed);
lightOn(redManLed);

/*lightOn(redLed);
delay(timerDelayTime);
lightOn(yelLed);
delay(timerDelayTime);
lightOff(redLed);
lightOff(yelLed);
lightOn(grnLed);
*/

// When the light is green, allow anyone to signal for a crossing request
int request = checkForCrossingRequest();
if (request > 0) {
Serial.println(“Beginning crossing sequence…\n”);
beginCrossing();
}

}

}

// Switch lights to red for a crossing sequence
void beginCrossing() {

changeLightState(1); // Indicate that we’re beginning a crossing sequence

// Goto Amber…
allLightsOff();
lightOn(yelLed);
delay(timerDelayTime);

// Goto Red, and wait for crossingTime milliseconds before continuing…
lightOff(redManLed);
lightOff(yelLed);
lightOn(redLed);
lightOn(grnManLed);
delay(crossingTime);
blinkLed(grnManLed, 5, 500);

// Blink the Yellow Led…
lightOff(redLed);
blinkLed(yelLed, 5, 500); // Blink the Yellow LED 5 times to signal to drivers to proceed if the way is clear
lightOn(grnLed);
lightOff(grnManLed);
changeLightState(0); // Change back to timer mode lighting

}

// Check for crossing button push
int checkForCrossingRequest() {
int val = digitalRead(crossingRequestSwitchPin);
if (val == HIGH) {
Serial.println(“*** Crossing Request Received ***\n”);
return 1; // Crossing requested
} else {
return 0;
}
}

// Helper functions
void lightOn(int pin) { // Turn an LED on
digitalWrite(pin, HIGH);
Serial.print(“Set pin “);
Serial.print(pin);
Serial.print(” to HIGH.\n”);
}

void allLightsOff() { // Turns all LEDs off
digitalWrite(redLed, LOW);
digitalWrite(yelLed, LOW);
digitalWrite(grnLed, LOW);
Serial.println(“All lights turned OFF.\n”);
}

void lightOff(int pin) { // Turn an LED off
digitalWrite(pin, LOW);
Serial.print(“Set pin “);
Serial.print(pin);
Serial.print(” to LOW.\n”);
}

void blinkLed(int pin, int timesToBlink, int blinkIntervalMs) { // Blinks the LED pin the set number of times
Serial.print(“Beginning blink sequence… Pin “);
Serial.print(pin);
Serial.print(“, “);
Serial.print(timesToBlink);
Serial.print(” times with a “);
Serial.print(blinkIntervalMs);
Serial.println(“ms interval between each blink.\n”);

for (int i=0; i <= timesToBlink; i++) { digitalWrite(pin, HIGH); delay(blinkIntervalMs); digitalWrite(pin, LOW); delay(blinkIntervalMs); } } void changeLightState(int newLightState) { // Changes the current lightState Serial.print("Light state changed from "); Serial.print(lightState); Serial.print(" to "); Serial.print(newLightState); Serial.println(".\n"); lightState = newLightState; } [/sourcecode] So! There you have it... My second Arduino project... or, rather an extension of the first! Thanks for reading! /Rich