2 weeks ago

PiTraffic review

Setting up LED lights is a common starter project, so much so that we frequently feature it in our beginner’s guides. Just set up an LED and use a bit of code to turn a light on and off.

It’s common for educators to turn to traffic lights as inspiration for a quick follow-on project: LEDs are readily available in red, yellow (amber) and green. Students can see the relationship between the code they create and physical objects being controlled in the real world.

This article was written by Lucy Hattersley and appears in The MagPi #65.

Setting up traffic lights is a hassle, though. You need at least three LEDs for each light, plus a button if you’re creating a pelican crossing. Setting up more than one set of lights quickly becomes a tangled bird’s nest of wires, resistors, buttons, and LEDs.

The PiTraffic HAT solves this by placing four traffic light sticks on top of the board. It also features a button to one side, and a piezo buzzer in the middle of the traffic lights (so your pelican crossing can beep, just like in the real world).

Setup is simple. Just plug the four traffic light sticks into the four sets of pins on top, and connect the PiTraffic HAT to the GPIO pins on any Raspberry Pi that has a 40-pin header. Then you should download test code from GitHub, which can be done with: git clone https://github.com/sbcshop/PiTraffic

After importing the PiTraffic code, you create instances of Traffic objects for each light, such as SouthRed = Traffic(“SOUTH”, “RED”). Once created, these are controlled using on() and off() methods, such as SouthRed.on() and SouthRed.off().

The only thing missing for us was Scratch support, which SB Components inform us is coming soon. The inclusion of Scratch will make this a compelling starter project for students. In the meantime we enjoyed using the Python API to code our lights.

Last word


A great starter HAT that takes much of the tangle out of a classic project. Students can test out traffic light code and see their results running in a miniature recreation.