PiLights part 1 – kit and test code

At work, we have a system that lots of users interact with, but which sometimes needs some “alone time”. From time to time, we email our users and ask them to close the client application, so that it can do what it needs to do. (Needless to say, this software was not written in-house!) Users being what they are – and emails being what they are – this approach leaves something to be desired.

A colleague and I put our heads together and decided that we needed something obvious, intuitive and highly visible. “What if,” we thought, “we could wire up some traffic lights to a Raspberry Pi?” So that’s what we decided to do. In this multi-post blog series, I’ll outline what parts we used, how we wrote the software, and how we made our little old Pi ready for the enterprise environment. In this first episode, I’ll focus on how we got to a working proof-of-concept.


I’ll admit it: when we started talking about this I think we both thought we were joking. We asked each other “Why not just use an old laptop and monitor?” We’ve used this approach before, but it didn’t feel like a good fit for this situation. A laptop is computational overkill for displaying a simple status and yet another monitor is hardly likely to attract our users’ attention.

Still, we weren’t immediately sold on the Raspberry Pi idea. I remember us thinking it would be expensive, difficult, and difficult to secure. With research, however, we were able to address each of these concerns in turn. We had a Raspberry Pi 2 lying around and the parts seemed relatively cheap. The electronics and programming seemed well within our grasp. We even found a strategy to securely manage the Raspberry Pi within our enterprise infrastructure (more on this later).

The shopping list

First up, we needed a Raspberry Pi. We had one to hand, so we didn’t need to buy one, but they’re pretty cheap if you did need to buy one.

Photo of a Raspberry Pi 3

Raspberry Pi 3

The Raspberry Pi is now at version 3 and is available at The Pi Hut. We’re using version 2B, but there shouldn’t be any functional difference. The 2B might be cheaper if you can still find one somewhere.

Next up, let’s take a look at the traffic lights themselves. We went for this 3 colour tower light:

Photo of 3 colour tower light

3 colour tower light

This model takes 12V, has a bonus buzzer (which, sadly, we won’t be using), and is available from Pimoroni.

The lights are going to need a 12V power supply. We just chopped the barrel connector off the end of a “wall wart” supply. If you’ve not gone one spare, CPC does one that will work.

Then we need some relays to interface between the digital and physical worlds. Since we have three coloured lights we want to control, we’ll need three relays. Luckily, our old friends Pimoroni produce a “hat”, which sports a range of useful interfaces – including three relays.

Photo of an Automation HAT

Automation HAT

Those three white blocks are our relays. We’ll be wiring our power supply and lights into these. NB: Make sure you get some “standoffs” with your order!

Finally, we’ll need some odds and sods:

  • A soldering iron and solder
  • Veroboard
  • Wire
  • Some kind of box to hold your project
  • Optionally a USB WiFi dongle


Putting it all together is pretty simple really. For starters, plop your hat on top of your Pi, using your standoffs to secure it at the correct spacing.

Then you’ll need to do a little light soldering. First determine the polarity of your power pack relative to the lights by simply touching the wires together (as the lights are really LEDs, they’ll only work one way round). Once that’s settled, use a bit of Veroboard to connect the negative end from your supply to the common ground from the lights. Connect the other wire from the supply to three strands of wire. Run each of these into a relay. Finally connect the wires for the red, amber, and green lights (the model I bought had helpful colour coded wires) into the relays in some sort of sensible order.

A circuit diagram

A circuit diagram showing how the LEDs are powered.


Although there’s a Python library built for the Automation Hat, I wanted to do something a bit different. Because this is a work project and we’re a Windows shop, I decided to explore Windows 10 IOT Core, which is a free version of Windows designed for use on small, headless devices; there’s even a build optimised for the Raspberry Pi. If you (like me) are a fan of Visual Studio, you’ll really enjoy the tooling that Microsoft provides. Another bonus is that you can securely manage your devices by enrolling them in the Azure IOT hub.

Anyway, enough of the spiel. The main thing about using IOT Core is that I can develop the way I normally do: writing C# in Visual Studio. Grab the IOT Core templates for Visual Studio 2017 and start a Background Application. Your entry point will be a class inheriting from, IBackgroundTask which specifies a Run method.From here, use the

From here, use the Windows.Devices.Gpio namespace to write to the GPIO pins corresponding to your relays. According to the handy pinout diagram, we’re interested in pins 13, 16 and 19.

I wrote a basic script to loop over the lights and flash them each in turn:

There’s a fair bit of waffle there, but it’s all from me. The actual code needed to open a pin and write to it is very concise.

Quick aside: There’s no Thread.Sleep(); here. Instead (because we’re using .NET Core?) we have to use Task.Delay(1000).Wait();.


As the video below shows, if you’re after some basic disco light, the script shown above is fully complete:


In this post, we’ve seen how to cobble together a traffic light indicator using a Raspberry Pi and Windows 10 IOT Core. We’ve covered the parts, wiring, and coding.

In the next post, I’ll be exploring how we plan to run a basic web server on the Pi to host an API. We’ll use this to change the status of the lights remotely, using other applications on the network.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.