Air Quality Meter - Step 1: the Display
This is the start of a new project that I’m figuring out as I go. My end goal is to build a handheld air quality meter built off a Raspberry Pi, but there are plenty of steps between here and there that I’ll have to get through first.
Background
As a bit of background, I have pretty bad asthma, that’s been getting worse every year do to California’s worrying trend of having terrible, multi-week forest fires every year now. “Fire season” has always been a familiar phrase here, but growing up, it was more of a threat, and less a promise. I recommend reading about how indigenous peoples in California, including Yurok, Karuk, Hupa, Miwok, Chumash tribes, have a tradition of using controlled burns that managed the fires and fostered the beautiful forests here, before California outlawed the practice because colonialism.
Anyway, I’ve become fairly reliant on the PurpleAir website, where people can buy a fairly affordable ($230 - but this kind of thing used to cost way more) wifi-connected air quality meter that gets shared out to a map, which shares real-time air quality that often shares results that are within a couple blocks from you if a neighbor decided to buy a device. However, that’s not enough for me. I want to be able to wave around a handheld meter like a ghost hunter on TV, divining whether the air outside my apartment is safe, or whether my windows are leaking smoke. So, that’s where we’re kicking off.
Building the display
Parts:
- 16x2 LCD Display
- Specifically the OSEPP-LCD-01 keypad shield.
- Arduino Uno R3
This decision was based on the fact that I had a 16x2 display already lying around, and that I wanted to keep my GPIO pins free for the other parts I’m going to use for this project.
Note - this display is a relatively old model, and it isn’t important to use the exact model. You may have to look up a different set of Arduino drivers from the manufacturer if you are building this project on your own.
Getting started
Assembly is blessedly simple, this time. The keypad/display shield is designed to fit an Arduino, so you can just insert the pins and get rolling.
Now, if you plug the USB cable into your PC, you’ll see the display receive power, but it’s not going to be functional yet.
I don’t currently have the Arduino IDE, so I grabbed it from their Downloads page
I also grabbed the Sample Code from the OSEPP website for my display.
I dropped the buttontest
code in, and it worked with no problems!
So, that solves the generic issue of “write to the screen”, but the rest of this application is going to be written in Python, running on the Raspberry Pi to take advantage of the drivers that the lovely folks at Adafruit have written.
Therefore, I’ll need to set the code up to allow me to communicate back and forth between the Arduino and the Python script, sending messages to be displayed, and passing button inputs from the board back to Python.
Coding for the display
Step 1 - Hello World
I’ve started by modifying the buttontest
script to accept input and output from Serial. Here’s the code to receive a message:
// serial_lcd.pde
LiquidCrystal ;
String inputString = "";
void
void
So, let’s break down what’s happening here:
- I’m setting up the LCD display using the LiquidCrystal drivers, and assigning it to the pins that are currently wired to the display.
- Then, I declare a buffer string for any serial messages that come in.
- Next, I’m setting up the display, opening up a Serial connection, and setting the cursor.
Next, the serialEvent is set up to listen to any messages coming in over the USB connection. It resets the screen and my string buffer, and then concatenates the whole message into a string. I then iterate through the characters and write them to the display, with bonus logic to handle a newline character to write to the second row.
Next, the Python code:
# Arduino.py
=
=
# The sleep is important to let the arduino wake up!
;
Not so bad, right?
Here we set up the serial port, targeted at the port my Arduino is using, and use the io wrapper to support sending simple strings.
After that, all we have to do is write our string and flush the serial io to send our message along!
Sending messages back
Since we have a bunch of convenient buttons already attached, I naturally want to be able to use them to send messages back to the main application.
The buttontest
example gives us a good head start on this boilerplate, but I modified to change the logic to only send a message out on Serial when the most recent message changes. That looks a bit like this:
// serial_lcd.pde
int last_message = 0;
int lcd_key = 0;
int adc_key_in = 0;
int current_line = 0;
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
// read the buttons
int
void
Now we are sending data back over serial as codes ranging from 0-5.
We can program python to listen to these messages with this code:
# Arduino.py
=
global
return
=
=
=
=
Now, we can press buttons on the LCD Shield, process those codes in Python, and respond by updating the screen with the code!
Since we can also pass new line characters, I can now predictably write to all the available spaces using two 16 character strings joined by a \n
newline character.
I’m satisfied with this functionality for now, so stay tuned for part 2 where I’ll wire up the air quality sensor!