Skip to content

Instantly share code, notes, and snippets.

@andywarburton
Created June 2, 2022 21:15
Show Gist options
  • Save andywarburton/e9b02905cedb231d16d042df98baf7a9 to your computer and use it in GitHub Desktop.
Save andywarburton/e9b02905cedb231d16d042df98baf7a9 to your computer and use it in GitHub Desktop.
Code for Canairi Clone Project by Andy Warburton
import time
import board
import adafruit_sgp40
import pwmio
from adafruit_motor import servo
import adafruit_ahtx0
import neopixel
### you may need to customize these ###
# The larger the value, the worse the air quality.
# 0-100, no need to ventilate, purify
# 100-200, no need to ventilate, purify
# 200-400, ventilate, purify
# 400-500, ventilate, purify intensely
air_boundry = 150 # sets the point at which we declare "bad quality air"
live_angle = 180 # angle at which the bird is "up"
dead_angle = 0 # angle at which the bird is "down"
i2c = board.I2C() # uses board.SCL and board.SDA
# This is our SGP40 air quality sensor
sgp = adafruit_sgp40.SGP40(i2c)
# this is our temp/humidity sensor
aht = adafruit_ahtx0.AHTx0(board.I2C())
# setup the onboard neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.2
pixel.fill((255, 255, 255))
# create a PWMOut object on Pin A0.
pwm = pwmio.PWMOut(board.A0, duty_cycle=2 ** 15, frequency=50)
# Create a servo object, my_servo.
bird = servo.Servo(pwm,min_pulse = 500, max_pulse = 2300)
# don't change these unless you know what you're doing
status = "clean"
old_status = status
# default to "dead" until we have a clear reading
bird.angle = live_angle
# sane defaults
temperature = 21
humidity = 50
print("========================")
print(" THE BIRD IS THE WORD")
print("=======================")
while True:
# getting temp and humidity for more acurate air quality readings
temperature = aht.temperature
humidity = aht.relative_humidity
# return int The VOC index measured, ranged from 0 to 500
compensated_raw_gas = sgp.measure_raw(temperature = temperature, relative_humidity = humidity)
raw_gas = sgp.raw
voc = sgp.measure_index(temperature = temperature, relative_humidity = humidity)
# it takes a minute or so for the sensor to warm up
# so we don't do anything until we get a reading
if(voc > 0):
if(voc >= air_boundry):
status = "DIRTY"
pixel.fill((255, 0, 0))
else:
status = "CLEAN"
pixel.fill((0, 255, 0))
# print for debugging
print("THE AIR IS", status, "! \t", "Raw Gas: ", raw_gas, " \t VOC: ", voc, " \t Temp:", temperature, " \t Humidity: ", humidity)
if(status == "DIRTY" and old_status == "CLEAN"):
# kill the bird
print("murdering the bird")
bird.angle = dead_angle
if(status == "CLEAN" and old_status == "DIRTY"):
# revivive the bird
print("reviving the bird")
bird.angle = live_angle
# save the status for the next loop so we can conpare
old_status = status
else:
print(".")
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment