Via Air Quality Module, you can control two servo motors attached to the mini pan-tilt kit by choosing one of the given angles - 0, 30, 45, 90, 135, 180 - and adjust the background light in RGB format - 255, 255, 255. And, most importantly, you can monitor the current air quality range generated by the MQ-135 sensor: the GUI notifies you with a warning message if the air quality is deteriorating.
Some of the mentioned products and components in this project are sponsored by digitspace:
- Raspberry Pi 3 Board Case 3B | Inspect
- Raspberry Pi 4B/3B+ SD Card 32GB | Inspect
- Raspberry Pi 3B Micro-HDMI VGA Converter Cables | Inspect
Preview: What You Will Learn
- How to control servo motors using angles with Raspberry Pi
- How to adjust the color of RGB LEDs with Raspberry Pi
- How to read analog sensors with MCP3008 and Raspberry Pi
- How to develop a GUI using the guizero module
- How to program a range function to arrange values like the map function in Arduino
Step 1: Read Analog Sensors with MCP3008
Unfortunately, Raspberry Pi does not have a built-in ADC (analog-to-digital converter) by which we can collect information from analog sensors easily as opposed to Arduino. Hence, we need to implement an external ADC in our circuit to collate data from analog sensors like the MQ-135 Air Quality Sensor. I used an MCP3008 8-channel ADC due to its efficiency and simple usage.
I used the Adafruit CircuitPython MCP3xxx library on Raspberry Pi to read analog sensors with MCP3008.
But, you will need to install the Adafruit_Blinka library that provides the CircuitPython support in Python to use the mentioned library.
sudo pip3 install adafruit-blinka
If it is needed, you have to enable the SPI on Raspberry Pi Configuration Settings before executing the command.
Once that's done, from your command line run the following command to install the Adafruit CircuitPython MCP3xxx library.
sudo pip3 install adafruit-circuitpython-mcp3xxx
Then, make the pin connections as follows. Even though the pin connections are well-explained on the project schematic and in the code, I'll leave the following pin connection schematic for MCP3008 besides them.
- MCP3008 CLK to Pi SCLK
- MCP3008 DOUT to Pi MISO
- MCP3008 DIN to Pi MOSI
- MCP3008 CS to Pi D5
- MCP3008 VDD to Pi 3.3V
- MCP3008 VREF to Pi 3.3V
- MCP3008 AGND to Pi GND
- MCP3008 DGND to Pi GND
- MCP3008 CH0 to Analog Sensor's Signal Pin
CH0 refers to the first input pin on the MCP3008 - from 0 to 7.
Step 2: Developing a GUI (Air Quality Module) and Programming Raspberry Pi
To be able to create a GUI in Python, you need to install the guizero module on Raspberry Pi. Then, you can use all the provided widgets and events by the module.
From your command line run the following command to install the guizero module.
sudo pip3 install guizero
You can inspect all instructions and widget settings from here.
- Import the required libraries and modules.
- Do not forget to include all GUI widgets - App, Box, Text, TextBox, PushButton, ButtonGroup, MenuBar, info, yesno, warn.
- Create the SPI bus, the cs (chip select), and the mcp object.
- Create analog input channels connected to the input pins on the MCP3008 - I used only the channel 0.
- Define GPIO pin settings - GPIO.setmode(GPIO.BCM).
- Define RGB pins and set PWM frequencies - 100.
- Start RGB PWM pins at 100 - OFF.
- Define servo motor pins and set PWM frequencies - 50.
- Start servo base and arm pins at 0 - OFF.
- Define menu bar option commands (functions):
- In the Tutorial() function, open a yesno message to go to the project tutorial page if requested.
- In the Components() function, open an info message to display components.
- In the About() function, open an info message to display the elevator pitch.
- Define the widget commands and features:
- In the _range() function, replicate the Arduino map function to arrange values in a given range in Python.
- In the evaluateSensorValue() function, get the air quality value from the MQ-135...
Please feel free to leave a comment here if you have any questions or concerns regarding this project :)