Unpacking the Arduino: making some traffic lights


My long-awaited Arduino Duemilanove board arrived today. Great! Now I have a use for all the miscellaneous electronic kit I have been randomly purchasing over the past week-and-a-half.

After 10 minutes, it was connected, and the obligatory “Hello World” flashing-LED had been created. Woo! Sure it’s simple, but my entry into the world of physical computing. Just a few minutes later, I’d expanded that solitary LED into a chain of 5 blinking LEDs (I know what you’re thinking: “that’s awesome, Rich”).

My first project: timer-based traffic lights

Given that I don’t have any force-sensors or other sensing gadgetry laying around just yet, I decided to model a simple, timer-based traffic light model comprising of three LEDs, and four combinations of light:

  • Red
  • Red + Amber
  • Green
  • Amber

(In the UK, that’s the sequence of traffic lights we use to dictate: “Stop”, “Proceed if it’s OK”, “Go”, “Get ready to stop”.)

I decided that, for the purposes of my first adventure, I would run the lights on a simple 2.5 second timer (long enough to figure out if the sequence is right, quick enough to prevent instantaneous brain boredom, waiting for them to change).

So, without further ado, the grand unveiling, here is my first Arduino circuit:

Arduino Traffic Lights (Timer version)

Given that’s so messy, here’s a circuit diagram, created using Fritzing (which is awesome, by the way – go get it, it’s free):

Ignore the fact it says “Arduino Diecimila”, I’m actually using the Arduino Duemilanove which I’m told is the newest one. I don’t think Fritzing has been updated with the new part just yet, but of course the bits you’re interested in are the pin connections…

Here’s a video of the traffic light sketch in action:

For posterity, I’ve also shown the sketch used to create this marvellous display of light:

/* Richard's Traffic Light Program */

int pinRedLed = 2;
int pinYelLed = 3;
int pinGrnLed = 4;
int lightState = 0;

void setup() {
    pinMode(pinRedLed, OUTPUT);    /* Set the LED pins to output */
    pinMode(pinYelLed, OUTPUT);
    pinMode(pinGrnLed, OUTPUT);
    digitalWrite(pinRedLed, HIGH);  /* Turn the RED light on, so we don't cause any traffic accidents while we initialise 🙂 */

void loop() {

    if (lightState > 3) {            // If the light state > 3, reset it to 0 (red).
      lightState = 0;


  /* Display correct light sequence */
  if (lightState == 0) { // Red
    digitalWrite(pinRedLed, HIGH);
  if (lightState == 1) { // Red and Amber
    digitalWrite(pinRedLed, HIGH);
    digitalWrite(pinYelLed, HIGH);
  if (lightState == 2) { // Green
    digitalWrite(pinGrnLed, HIGH);
  if (lightState == 3) { // Amber-only
    digitalWrite(pinYelLed, HIGH);


void lightOff(int pin) {
  digitalWrite(pin, LOW);

So what’s next?
Tomorrow, or as soon as I get some more spare time, I’m going to modify this sketch to wait for a button press before changing the light sequence (instead of working on a timer). Actually, thinking about it, it would be good to have the lights operating autonomously on a timer, but as soon as a button is pushed, stop the traffic (go to Amber, then Red), wait for 10 seconds to let our pedestrians cross the road safely, then let the traffic pass again (go to Red + Amber, then Green) and then resume timer functionality. Ooo! That sounds exciting! 🙂



Join the Arduino Diigo group


I started an Arduino group on Diigo a few days ago to share some of my bookmarks on this cool little board. Anyone can join. Please feel free to share anything even remotely related! 🙂

1 x Arduino short of a project


Ok, so I couldn’t wait for Tinker.it’s Arduino Starter Kits to come back into stock – I’m so impatient. I also didn’t want to pay $40 for US shipping (after having to admit I overspend on ‘gadgetry’), so I sourced an Arduino board from coolcomponents.co.uk, and hit Maplin for just about everything else. As this is my first ever foray into ‘physical computing’, I decided that I needed, well, everything. So I went and bought a Jump Wire Kit, a breadboard kit with banana sockets for power, a regulated switchable DC power supply, about a trillion LEDs, resistors, capacitors and anything else that looked ‘electronicy’.

I also bought Physical Computing and Making Things Work – two excellent books.

So all this stuff has arrived, but my Arduino, ordered on the same day, hasn’t. So right now I’m like most kids who are stuck indoors looking out at the snow: I really, really just wanna play! 🙂

Measuring electricity consumption, the geek’s way!


CurrentCost – The Beginning

Having just purchased our first home, and realising for the first time just how expensive everything actually is, I started to go on a ‘cost cutting’ excercise. Fuelled largely by a desire to reduce the overall amount of waste we produce and increase the amount of recycling we do, it wasn’t long before we were able to adapt our lifestyle to meet those goals. What really interests me though is our energy consumption… How much electricity and gas do we use, what are the worst offending appliances in our home over time?

This was back around winter of 2006 and what I really wanted to find was a consumer-targeted product that I could connect to my conventional electricity meter and that also connected to my PC, somehow. The goal was to start logging my energy usage into a database where I could more easily query all the data.

Unfortunately, either because one didn’t exist or because I was hopeless at searching, I couldn’t find one. It wasn’t until Christmas 2007 that I heard about a domestic product called the Owl wireless electricity meter that I had rekindled my interest in this topic once again. I placed my order for the meter in the early part of the year and it has been happily running since then, gently reminding us to turn off our lights when we’re not using them, to be more conscious about using our electric oven more efficiently and just how much it costs to run our dishwasher.

In the mean time, I had spent a lot of time diligently installing X10 home control units into various light fittings, appliances and even our alarm system, all controlled via our HP MediaSmart Windows Home Server.

This is all very well and good, but I still really wanted to start getting some useful data on our energy usage into the computer and I had been reading lots about extremely clever people with very large brains creating all manner of gizmos in their garages that could do just that. Realising that I probably couldn’t do the same, I started to search again for a consumer product that has these capabilities and finally, my long journey looked as though it had come to a successful conclusion: the CurrentCost meter. Not only did it look much better than the Owl meter I was using, but it also had a datalogging facility.

A few minutes on the CurrentCost web site and the CurrentCost wiki convinced me that if I didn’t order it soon I may possibly explode with excitement; and I promptly parted with a very reasonable £28. Later that same hour, more reading on the wiki turned up very interesting links (mostly to site’s owned by very clever people who work for IBM but also share a passion for, uh, gathering data, particularly from this very cool device) and it wasn’t long before I started reading about people who were very curious about the RJ45 socket on the bottom of the meter.

Like early pioneers, the electronics gurus set out with their multimeters and soldering irons and very quickly it seems, somebody had worked out that the CurrentCost unit diligently spits out an XML packet every six or seven seconds. Amazing. There we go: I had struck gold. Now if only I knew how on earth to make me one of these cables (this was my thought process anyway, although right now you have to remember I don’t actually own a CurrentCost yet – I had only just ordered it!).

After reading about breadboards, 9-pin D-SUB sockets and what not, I started to convince myself that actually, with all the clever guides out there, I could probably hash one together of my very own and could be ‘living the dream’, too.

Suddenly though, another article on that fantastic CurrentCost wiki told me that ‘those in the know’ could purchase a ready-made cable direct from CurrentCost themselves, all for the princely sum of £11.12, including VAT and delivery. Kerching! Approximately seven seconds later, my cheque book was flung open and the cheque was in the postbox on it’s merry way to CurrentCost in Surrey.

Ahhhh. So now all I have to do is sit and wait. Which, I’m not very good at.

You see, all this happened yesterday – which as all geeks know, is an eternity – especially when you’ve just ordered some very sexy gadgetry that you cannot wait to get your hands on. So, not wanting to leave the world of CurrentCost, I started to read around about things other people were doing. And then, I came across Arduino and Freeduino.

Both these boards appeal to me because they are specifically aimed at electronics novices, but they seem to be expandable and robust enough to be the gadgets that experienced pro’s love to use as well. So, what is it? Well, in a nutshell, the Arduino is a little electronics prototyping board that has a small processor and a series of digital inputs and outputs, and a connection to your computer. Bingo. Now we have a way of wiring up my whole home to my computer.

And I’m good with computers! As a programmer, I love writing software to do cool things. And what on this earth could be cooler than somehow getting all of this data from the ‘physical world’ of my home into a digital format I can pump into some software?

If you answered ‘lots of things’, then I am simply stunned that you have kept on reading through this geek-oriented babble! But, since you’re here, you might as well read on.

The day before ordering my CurrentCost and data cable, I had also splashed out on a USB 4 PORT CCTV capture card. It had to be USB, because I planned to be using my HP Media Smart Windows Home Server to capture all the imagery.

And that’s where I’m going to leave this post. Thanks to all the wonderful people blogging about the CurrentCost unit (as I have said, they all mainly seem to be IBM or ex-IBM employees!), and the super work they have done so far, they have rekindled my interest in this topic and I plan to write some software of my own, to sit on the MediaSmart Windows Home Server and do all sorts of wonderful things (of course, not quite sure what they’ll be yet).

From the next pay cheque, too, I think I’ll go out and buy all the cool Arduino bits because I really want to find a way to build my own gas meter reader! Watch this space…