Files
arduino/examples/PM2_SIMPLE/PM2_SIMPLE.ino

55 lines
1.8 KiB
Arduino
Raw Normal View History

/*
This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Microcontroller.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
2022-07-29 16:26:06 +02:00
For build instructions please visit https://www.airgradient.com/open-airgradient/instructions/
Compatible with the following sensors:
Plantower PMS5003 (Fine Particle Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
2021-12-08 09:59:32 +07:00
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
2022-07-29 16:26:06 +02:00
https://www.airgradient.com/
2022-07-29 16:26:06 +02:00
Kits with all required components are available at https://www.airgradient.com/open-airgradient/shop/
2021-12-08 09:59:32 +07:00
2023-03-09 07:35:49 +07:00
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
2020-07-24 21:52:28 +08:00
#include <AirGradient.h>
2021-12-08 09:59:32 +07:00
2020-07-24 21:52:28 +08:00
AirGradient ag = AirGradient();
2021-12-08 09:59:32 +07:00
void setup() {
Serial.begin(115200);
2020-07-24 21:52:28 +08:00
ag.PMS_Init();
}
2021-12-08 09:59:32 +07:00
void loop() {
int PM2 = ag.getPM2_Raw();
2020-07-24 21:52:28 +08:00
2021-12-08 09:59:32 +07:00
Serial.print("PM2.5 in ug/m3: ");
Serial.println(String(PM2));
Serial.print("PM2.5 in US AQI: ");
Serial.println(String(PM_TO_AQI_US(PM2)));
delay(5000);
}
2021-12-08 09:59:32 +07:00
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};