I’ve been learning the
Go programming language
recently. As an exercise, I decided to revisit a
past article
that looked at working with traffic lights on the Raspberry Pi in Python in order to rewrite it in Go. To make this a
standalone guide, there will be some re-use of content from the prior article here.
Shopping List
To try this out, you will need the following (links here mostly go to
Adafruit
, UK customers may want to consider
Pimoroni
as a UK based alternative, Amazon has most if not all of this stuff too):
A
Raspberry Pi
(I’ll use the Pi 3 Model B here, but any model with GPIO pins will work — if you want to use the Pi Zero you’ll need to
solder some headers onto it). I’m going to assume you have a Pi 2 or 3 with 40 pins
Some sort of
case
is probably a good idea to protect the Pi (but you’ll need to leave the lid off to expose the GPIO pins to connect your
lights to)
A
Micro SD card
to install your operating system on (or get one with the
OS pre-installed
).
If you want to install the operating system yourself, you’ll need a Mac, PC, Linux machine with an SD card reader
The Low Voltage Labs traffic lights connect to the Pi using four pins. One of these needs to be ground, the other three being actual GPIO pins used to control each of the individual LEDs.
Before powering up the Pi, attach the traffic lights so that the pins connect to the GPIO pins highlighted in red:
When you’re done it’s going to look something like this… (an easy way to make sure you have it right is to locate the lights on the left hand row of pins as you look at the Pi with the USB ports to the bottom, then count 8 pins up and attach the
lights there).
Don’t turn the Pi on yet, you’ll need to prepare an operating system image for it first…
Once you’ve got the operating system installed make sure you can login, and have a working wired or wifi internet connection configured.
Now you can go ahead and start turning lights on and off!
Installing Go
Go code can be compiled and distributed as a binary, it can also be cross compiled (where the compiler generates a binary to run on a different operating system / processor architecture than the one it was built on). We’ll look at both options
here so will need to install the Go distribution on the Pi as it’s not included with Raspbian Lite.
Go installation is a simple matter of downloading the distribution (
check here
for latest ARM v6 version that the Pi uses), then expanding it into /usr/local: