Skip to content

Instantly share code, notes, and snippets.

@dvsseed
Created October 18, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvsseed/2f6a871fd6320b484d0cb388664dd0fa to your computer and use it in GitHub Desktop.
Save dvsseed/2f6a871fd6320b484d0cb388664dd0fa to your computer and use it in GitHub Desktop.
drag and drop this file to the CIRCUITPY directory
""" Example for using the SGP30 and PM25 with CircuitPython
and the Adafruit library """
import digitalio
import busio
import adafruit_sgp30
import board
import adafruit_pm25
from tool import show_SGP, show_SGP_baseline # for SGP30
from tool import show_PM25 # for PMSA003
from tool import color_chase_1 # for NeoPixel
from tool import play_file # for AudioOut
# 設定LED顏色
RED = (255, 0, 0)
GREEN = (0, 255, 0)
OFF = (0, 0, 0)
i2c_bus = busio.I2C(board.SCL, board.SDA, frequency=100000)
# Create library object on I2C port
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c_bus)
print("SGP30 serial #", [hex(i) for i in sgp30.serial])
sgp30.iaq_init()
# sgp30.set_iaq_baseline(co2eq_base, tvoc_base)
# elapsed_sec = 0
reset_pin = None
uart = busio.UART(board.TX, board.RX, baudrate=9600)
pm25 = adafruit_pm25.PM25_UART(uart, reset_pin)
# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True
# Make the 2 input buttons
buttonA = digitalio.DigitalInOut(board.BUTTON_A)
buttonA.direction = digitalio.Direction.INPUT
buttonA.pull = digitalio.Pull.DOWN
buttonB = digitalio.DigitalInOut(board.BUTTON_B)
buttonB.direction = digitalio.Direction.INPUT
buttonB.pull = digitalio.Pull.DOWN
while True:
# 顯示 SGP30數據
show_SGP(sgp30)
# 依據SGP數據控制LED顯示
color_chase_1(OFF, 0, 0.1)
if sgp30.TVOC < 10:
color_chase_1(RED, 0, 0.1)
else:
color_chase_1(GREEN, 0, 0.1)
# elapsed_sec += 1
# 顯示 SGP30基準數據
# elapsed_sec = show_SGP_baseline(elapsed_sec, sgp30)
# 讀取 PMSA003數據
try:
aqdata = pm25.read()
# print(aqdata)
except RuntimeError:
print("Unable to read from sensor, retrying...")
continue
# for plotter
print((sgp30.eCO2, sgp30.TVOC, # sgp30.H2, sgp30.Ethanol,
aqdata["particles 03um"],
aqdata["particles 05um"],
aqdata["particles 10um"],
aqdata["particles 25um"],
aqdata["particles 50um"],
aqdata["particles 100um"]))
# 顯示 PMSA003數據
show_PM25(aqdata)
# 依據PM25數據控制LED顯示
color_chase_1(OFF, 9, 0.1)
if aqdata["pm25 standard"] < 50:
color_chase_1(RED, 9, 0.1)
else:
color_chase_1(GREEN, 9, 0.1)
# The two files assigned to buttons A & B
audiofiles = ["rimshot.wav", "laugh.wav"]
# 按下按鈕[A/B] to play music
if buttonA.value:
play_file(audiofiles[0])
if buttonB.value:
play_file(audiofiles[1])
# End of Loop
@dvsseed
Copy link
Author

dvsseed commented Oct 18, 2020

drag and drop this file to the CIRCUITPY directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment