Air Quality Traffic Light
By David Crookes. Posted
Kyiv in Ukraine is one of Europe’s most polluted cities. It has high smog levels, which can cause health problems. “It also occurred to me that, in summer, the air quality in our area can deteriorate due to peat deposits igniting and smouldering somewhere in the province,” says Dmytro Panin.
Take back control of your online data, with Raspberry Pi.
For that reason, he decided to take a break from a project he was working on – a device which monitors currency prices using Raspberry Pi Zero W – to create an air quality reporting system. Having found a battery-operated toy traffic light, he came up with a plan that was both practical and fun.
“If the wind blows in the direction of the city, you can smell the smoke, which some say resembles whiskey, and it means you don’t want to have the windows open for too long,” Dmytro says. “So, I came up with the idea of having the red light of a traffic light illuminate if this was to happen."

Capturing air quality
Dmytro chose a traffic light made by German toy manufacturer Dickie. “It’s compact, has a button on top, and two additional LEDs accompanying the standard three,” he explains. But before he could connect it to a Raspberry Pi 3B+ computer, he needed to open the toy.
“I had to get rid of the traffic light’s internal components without damaging the LEDs or the internal structure of the toy,” he continues. “I also had to change the resistors so that it would work with Raspberry Pi’s GPIO pins, which produce 3.3 V.”
From there, it was a matter of working out the best way of capturing air quality, starting by experimenting with the MQ-135 gas sensor which monitors benzene, alcohol, and smoke. “I got it working after a few hours by connecting an additional analogue-to-digital chip to the serial port of Raspberry Pi,” he says. “But the measurements were not that precise and I had to write quite a bit of code to account for inaccuracies which varied drastically based on the environment temperature.”

Good to go
Instead, he turned to an MH-Z19B carbon dioxide sensor. “It was way more accurate and it worked, but only temporarily, as the elements in my area can be on the extreme side,” Dmytro says.
The solution, therefore, was to write a short program using the open-source programming language Go. It fetches Air Quality Index data from the website IQAir, which uses information from consumer devices located across Kyiv. It also grabs current data from OpenWeather in parallel.
By storing the data on Raspberry Pi, Dmytro’s script – written in Python – can determine which light to illuminate, controlling the LEDs using the Python library GPIO Zero. Red obviously shows that the air quality is poor, prompting Dmytro to close the windows and take precautions when venturing outside.
Even so, there is room for improvement. “The aggregated data is quite accurate but it covers the entire city,” he laments. “Sometimes it would make more sense for measurements to be more local. Collecting data myself and combining it with the information I get from the API should be the next step.”
Subscribe to Raspberry Pi Official Magazine
Save up to 37% off the cover price and get a FREE Raspberry Pi Pico 2 W with a subscription to Raspberry Pi Official Magazine.
More articles
Take Back Your Clound in Raspberry Pi Official Magazine 165
We’re all-too accustomed to the feling of being watched on the internet. A search for any product leads to adverts, emails, and goodness knows how many bots tracking you. It feels weird and intrusive, and we aren’t being paranoid – they really are watching us. Luckily you don’t have to reply on online services that […]
Read more →
Win one of three Raspberry Pi 4 with 3GB RAM!
Raspberry Pi 4 is still a powerful piece of kit, and this new 3GB version is great for projects and builds that need slightly more than 2GB of RAM but don’t quite need 4GB. We have three to give away and you can enter below. Win 1 of 3 Raspberry Pi 4 3GB
Read more →
Etch A Sketch CNC machine
The software controlling the Etch A Sketch logs the image, turns it into vectors, turns the vectors into G-code, then sends the G-code to the Etch A Sketch – and it was all written by AI
Read more →