~bsprague/air-quality

8899cb4deb5b84607ea8fdb2e911e559a71c0e21 — Brandon Sprague 6 months ago 1e9538a
Amalgamate some basic CircuitPython examples
2 files changed, 96 insertions(+), 0 deletions(-)

A .gitignore
A circuitpython/code.py
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
/circuitpython/.venv

A circuitpython/code.py => circuitpython/code.py +95 -0
@@ 0,0 1,95 @@
# From https://github.com/adafruit/Adafruit_CircuitPython_PM25/blob/main/examples/pm25_simpletest.py

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Sketch to connect BME280, PM25, and SGP30 sensors together, based on the example
sketch to connect to PM2.5 sensor with either I2C or UART.
"""

# pylint: disable=unused-import
import time
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_pm25.i2c import PM25_I2C
import adafruit_sgp30

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT


reset_pin = None

# TODO: Wire the pin labelled 'RST' to the D13 pin (it goes BAT, EN, USB, then
# D13) on the Feather ESP32-S2.Relevant diagrams and info:
# https://learn.adafruit.com/adafruit-esp32-s2-feather/pinouts

reset_pin = DigitalInOut(board.D13)
reset_pin.direction = Direction.OUTPUT
reset_pin.value = False

# For using the built-in STEMMA QT connector on a microcontroller
i2c = board.STEMMA_I2C()

# Connect to a PM2.5 sensor over I2C
pm25 = PM25_I2C(i2c, reset_pin)
print("Found PM2.5 sensor, reading data...")

# See https://docs.circuitpython.org/projects/sgp30/en/latest/
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)
print("Found SGP30 sensor, reading data...")

bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
print("Found BME280 sensor, reading data...")

# It seems like this needs to be set but I have **no clue** if I should change
# this for a given location
bme280.sea_level_pressure = 1013.25


led_stat = True
while True:
    time.sleep(2)
    led_stat = not led_stat
    led.value = led_stat

    try:
        aqdata = pm25.read()
        # print(aqdata)
    except RuntimeError:
        print("Unable to read from sensor, retrying...")
        continue

    print()
    print("Concentration Units (standard)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 standard"], aqdata["pm25 standard"], aqdata["pm100 standard"])
    )
    print("Concentration Units (environmental)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 env"], aqdata["pm25 env"], aqdata["pm100 env"])
    )
    print("---------------------------------------")
    print("Particles > 0.3um / 0.1L air:", aqdata["particles 03um"])
    print("Particles > 0.5um / 0.1L air:", aqdata["particles 05um"])
    print("Particles > 1.0um / 0.1L air:", aqdata["particles 10um"])
    print("Particles > 2.5um / 0.1L air:", aqdata["particles 25um"])
    print("Particles > 5.0um / 0.1L air:", aqdata["particles 50um"])
    print("Particles > 10 um / 0.1L air:", aqdata["particles 100um"])
    print("---------------------------------------")

    eCO2, TVOC = sgp30.iaq_measure()
    print("eCO2 = %d ppm \t TVOC = %d ppb" % (eCO2, TVOC))
    print("---------------------------------------")

    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.relative_humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    print("---------------------------------------")