Skip to content

Instantly share code, notes, and snippets.

@buddylindsey
Created September 18, 2021 22:11
Show Gist options
  • Save buddylindsey/2ff4d382b4a468aea99cb37894881e86 to your computer and use it in GitHub Desktop.
Save buddylindsey/2ff4d382b4a468aea99cb37894881e86 to your computer and use it in GitHub Desktop.
Rough PM 2.5 Rework of code with AQI Calculation for use in displaying lights.
#include "Adafruit_PM25AQI.h"
Adafruit_PM25AQI aqi = Adafruit_PM25AQI();
int redLed=9;
int yellowLed=10;
int greenLed=11;
float LOWER_BOUND=100.00;
float UPPER_BOUND=151.00;
float DANGER=UPPER_BOUND*2.0;
void blinkLights() {
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
delay(250);
}
// https://forum.airnowtech.org/t/aqi-calculations-overview-ozone-pm2-5-and-pm10/168
// https://forum.airnowtech.org/t/the-aqi-equation/169
float chart[6][4] = {
{0.0, 12.0, 0.0, 50},
{12.1, 35.4, 51, 100},
{35.5, 55.4, 101, 150},
{55.5,150.4, 151, 200},
{150.5, 250.4, 201, 300},
{250.5, 500.4, 301, 500}
};
float calculateAQI(float concentration) {
int use = 6;
for (int i = 0; i < 6; i++) {
if (concentration >= chart[i][0] && concentration <= chart[i][1]) {
use = i;
}
}
return (chart[use][3]-chart[use][2])/(chart[use][1]-chart[use][0])*(concentration-chart[use][0])+chart[use][2];
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("Adafruit PMSA003I Air Quality Sensor");
delay(1000);
if (! aqi.begin_I2C()) {
Serial.println("Could not find PM 2.5 sensor!");
while (1) delay(10);
}
Serial.println("PM25 found!");
pinMode(redLed, OUTPUT);
digitalWrite(redLed, HIGH);
pinMode(yellowLed, OUTPUT);
digitalWrite(yellowLed, HIGH);
pinMode(greenLed, OUTPUT);
digitalWrite(greenLed, HIGH);
blinkLights();
blinkLights();
blinkLights();
}
void loop() {
// put your main code here, to run repeatedly:
PM25_AQI_Data data;
if (! aqi.read(&data)) {
Serial.println("Could not read from AQI");
delay(500); // try again in a bit!
return;
}
Serial.println(data.pm25_standard);
float aqi_calculated = calculateAQI(data.pm25_standard);
Serial.println(aqi_calculated);
if (aqi_calculated <= LOWER_BOUND) {
Serial.println("Green");
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
} else if ( aqi_calculated > LOWER_BOUND && aqi_calculated < UPPER_BOUND ) {
Serial.println("Yellow");
digitalWrite(yellowLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, LOW);
} else if (aqi_calculated > UPPER_BOUND && aqi_calculated < DANGER) {
Serial.println("Red");
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
} else if (aqi_calculated > DANGER) {
blinkLights();
blinkLights();
blinkLights();
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment