How To Build an Air Quality Alert Light with Raspberry Pi Pico
Air quality is a big concern. We take the air we breathe for granted, but sometimes that air is full of pollution. Forest fires in Canada and hill fires in the UK are all recent examples of fires polluting our air, but how can we be sure it’s safe to go out?
In this how-to, we will build a project that uses air quality data. OpenWeather API You can know the air quality before leaving your home. Data is visualized using the Raspberry Pi Pico W, a $6 microcontroller that can go online to retrieve data and display it using strips of NeoPixel RGB LEDs.
circuit construction
The circuit consists of two sections. input and output. The input is a simple push button connected to GPIO14 and the 3V3 pin on the Pico W. The normal state of GPIO14 is pulled to ground (effectively 0V). Pressing the button connects the 3V3 pin to GPIO14 and pulls the pin high (3.3V). This state change is what the code looks for. The output is a NeoPixel strip connected to 3V3, GND, GPIO16. GPIO16 is used to send data to the NeoPixel and change colors as needed.
of GPIO pins on the Raspberry Pi Pico W NeoPixel sticks must be soldered using one of the following: best soldering iron or soldering station It makes my job easier.
What you need for this project
write code
The code for this project is divided into the code needed to connect to the internet and download data from the API, the code that waits for user input, and finally the code that interprets the data via the RGB LED.
1. Do the following Download the latest version of MicroPython for Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and set up your Thonny. The rest are optional.
2. open tony and Click the “Stop” button Refresh the connection. This will ensure that the Python shell will open and work properly.
3. create a new file, Paste the contents of this link. Save the file as neopixel.py in the root of Raspberry Pi Pico W.
Four. Create a new file for your project code.
Five. Import a set of modules essential to your project. The network allows the Pico W to connect to Wi-Fi, time is used to control timings in the project code, and urequests are used to retrieve data from the API. The machine contains ‘pins’ that are used to get and set the status of the GPIO pins, and the NeoPixel allows control of the NeoPixel RGB LED strips.
import network
import time
import urequests
from machine import Pin
from neopixel import Neopixel
6. Create an object and a button, create a connection to GPIO14, and pull the pin down (to GND) using the pin’s internal resistor..
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
7. Create a pixel, an object that establishes a connection between your code and the 8 NeoPixels connected to GPIO16. Note that the pixels in this module are in GRB (green, red, blue) order. If different, change this to RGB.
pixels = Neopixel(8, 0, 16, "GRB")
8. Create an object wlan and use it to connect to a Wi-Fi access point. Change the SSID and password to match your AP. Add a short pause to allow the connection to establish and stabilize.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID", "PASSWORD")
time.sleep(5)
9. Print the connection status to the Python shell.
print(wlan.isconnected())
Ten. Use a conditional statement to set the NeoPixel to blink green 10 times to indicate a successful connection.
if wlan.isconnected() == True:
for i in range(10):
pixels.fill((0, 64, 0))
pixels.show()
time.sleep(0.1)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.1)
11. If it fails, use a for loop to make the NeoPixel blink red 10 times.
else:
for i in range(10):
pixels.fill((64, 0, 0))
pixels.show()
time.sleep(0.1)
pixels.fill((0, 0 , 0))
pixels.show()
time.sleep(0.1)
12. Create an infinite loop and check the state of the button. If not pressed, the code stops here. Press to run the indented part.
while True:
if button.value():
13. Create an object aq to store the latest air quality data. Pressing a button triggers code to download the latest air pollution data from OpenWeather using a free API key.. Note that you should do the following: Sign up for a free accountthen click your username[My API Keys]Choose. Generate a new API key called AirPollution. Copy your API key and replace it with your URL under. Also change the lat (latitude) and Match lon (longitude) with your current location. You can find this on Google Maps. Here it is set in New York City.
aq = urequests.get("http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat=40.714272&lon=-74.005966&appid=YOUR API KEY HERE").json()
14. Extract the air quality index from the returned data and store it in the object air_quality_index. The data returned is in JSON format, much like Python’s dictionary data storage object. This means that the exact data can be read using a specific sequence of keys. These are documented in the OpenWeather API Reference.
air_quality_index = aq['list'][0]['main']['aqi']
15. Prints the Air Quality Index value as a statement to the Python shell. This is for debugging purposes. Values are 1 to 5. 1 = good, 2 = fair, 3 = fair, 4 = bad, 5 = very bad.
print("The Air Quality index is: ",str(air_quality_index))
16. If the air quality is 1, it’s good. Set the LED to blink green 10 times with a 0.5 second delay.
if air_quality_index == 1:
for i in range(10):
pixels.fill((0, 64, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
17. If the air quality is 2, it is normal. Set the LED to blink blue 10 times with a 0.5 second delay.
elif air_quality_index == 2:
for i in range(10):
pixels.fill((51,255,255))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
18. If the air quality is 3, it is moderate. Set the LED to blink purple 10 times with a 0.5 second delay.
elif air_quality_index == 3:
for i in range(10):
pixels.fill((153, 51, 255))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
19. If the air quality is 4, it’s bad. Set the LED to blink orange 10 times with a 0.5 second delay.
elif air_quality_index == 4:
for i in range(10):
pixels.fill((255,153,51))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
20. If the air quality is 5, it is very poor. Set the LED to blink red 10 times with a 0.5 second delay.
elif air_quality_index == 5:
for i in range(10):
pixels.fill((255, 0, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
twenty one.Finally, set the else condition to pass, which will be activated if no air quality data is returned, allowing the loop to return and wait for user input.
else:
pass
twenty two. Save the code as main.py on your Raspberry Pi Pico W. MicroPython runs the main.py file every time Pico starts.
twenty three. Click Run to start the code. NeoPixel blinks green to confirm Wi-Fi connection. If not, make sure the SSID and password are correct.
twenty four. With the push of a button, Pico W goes online and downloads the latest air pollution data. NeoPixel displays data in 5 colors and Python shell displays accurate air quality values.
full code list
import network
import time
import urequests
from machine import Pin
from neopixel import Neopixel
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
pixels = Neopixel(8, 0, 16, "GRB")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID", "PASSWORD")
time.sleep(5)
print(wlan.isconnected())
if wlan.isconnected() == True:
for i in range(10):
pixels.fill((0, 64, 0))
pixels.show()
time.sleep(0.1)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.1)
else:
for i in range(10):
pixels.fill((64, 0, 0))
pixels.show()
time.sleep(0.1)
pixels.fill((0, 0 , 0))
pixels.show()
time.sleep(0.1)
while True:
if button.value():
aq = urequests.get("http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat=40.714272&lon=-74.005966&appid=282f4cee730d75261ae4855ee7719e44").json()
air_quality_index = aq['list'][0]['main']['aqi']
print("The Air Quality index is: ",str(air_quality_index))
if air_quality_index == 1:
for i in range(10):
pixels.fill((0, 64, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
elif air_quality_index == 2:
for i in range(10):
pixels.fill((51,255,255))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
elif air_quality_index == 3:
for i in range(10):
pixels.fill((153, 51, 255))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
elif air_quality_index == 4:
for i in range(10):
pixels.fill((255,153,51))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
elif air_quality_index == 5:
for i in range(10):
pixels.fill((255, 0, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.5)
else:
pass