Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created November 3, 2023 02:06
Show Gist options
  • Save kakopappa/941db2518842c85b95a388592ad6cd3b to your computer and use it in GitHub Desktop.
Save kakopappa/941db2518842c85b95a388592ad6cd3b to your computer and use it in GitHub Desktop.
Detecting poor Air Quality using MQ135. Complete tutorial : https://help.sinric.pro/pages/tutorials/air-quality-sensors/mq135.html
#include <TroykaMQ.h> // https://github.com/amperka/TroykaMQ
#if defined(ESP8266)
#define SENSOR_PIN A0
#elif defined(ESP32)
#define SENSOR_PIN 34
#endif
MQ135 mq135(SENSOR_PIN);
const int AQI_THRESHOLD_SEVERE = 700;
const int AQI_THRESHOLD_VERY_POOR = 600;
const int AQI_THRESHOLD_POOR = 500;
const int AQI_THRESHOLD_MODERATE = 400;
int ppm = 0;
void setup() {
Serial.begin(9600);
// before calibrating the sensor, warm it up for 60 seconds calibrate the sensor in clean air
mq135.calibrate();
// if you know the resistance of the sensor in clean air
// you can specify it manually, eg: 160 mq135.calibrate(160);
Serial.print("Ro = ");
Serial.println(mq135.getRo());
}
void loop() {
ppm = mq135.readCO2();
if (ppm > AQI_THRESHOLD_SEVERE) {
Serial.print("Air quality is SEVERE!");
} else if (ppm > AQI_THRESHOLD_VERY_POOR) {
Serial.print("Air quality is VERY POOR!");
} else if (ppm > AQI_THRESHOLD_POOR) {
Serial.print("Air quality is VERY POOR!");
} else if (ppm > AQI_THRESHOLD_MODERATE) {
Serial.print("Air quality is MODERATE");
} else {
Serial.print("Air quality is GOOD");
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment