This blog will discuss the integration of the required materials stated in Blog #2 and Blog #3. Below are the steps for integrating all the materials to develop our Seeeduino SAMD21-based Indoor Caron Dioxide Monitoring Device.
- Prepare the required materials discussed in Blog #2.
- Connect your sensors to the SAMD21 Microcontroller pins. Kindly follow the wiring diagram below.
- Connect your Seeeduino XIAO SAMD21 Microcontroller to your PC or Laptop via a USB Type-C Cable.
- Create a new Arduino Sketch and paste the following program below.
#include "Wire.h" #include <MQ135.h> #include <DHT.h> #include <RGBLed.h> #include <LiquidCrystal_I2C.h> #define PIN_MQ135 A1 #define DHTTYPE DHT20 #define BUZZER 3 #define REDLED 10 #define GREENLED 9 #define BLUELED 8 LiquidCrystal_I2C lcd(0x27, 20, 4); MQ135 mq135_sensor(PIN_MQ135); DHT dht(DHTTYPE); RGBLed led(REDLED, GREENLED, BLUELED, RGBLed::COMMON_ANODE); unsigned long readMillis; unsigned long currentMillis; const unsigned long readPeriod = 2000; int redcolor = 0; int greencolor = 0; int bluecolor = 0; void setup() { analogReadResolution(12); Serial.begin(115200); lcd.init(); Wire.begin(); dht.begin(); lcd.backlight(); lcd.setCursor(1, 0); lcd.print("Indoor Air Quality"); lcd.setCursor(1, 1); lcd.print("Monitoring Device"); lcd.setCursor(0, 2); lcd.print("System Initializing"); lcd.setCursor(0, 3); for (int i = 0; i < 20; i++) { lcd.print("."); delay(100); } lcd.setCursor(0, 3); for (int i = 0; i < 20; i++) { lcd.print(" "); delay(100); } lcd.setCursor(8, 3); lcd.print("DONE"); delay(100); //lcd.setCursor(0, 0); //lcd.print("Real-time Air Status"); readMillis = millis(); } void loop() { currentMillis = millis(); if (currentMillis - readMillis >= readPeriod) { float temp_hum_val[2] = {0}; if (!dht.readTempAndHumidity(temp_hum_val)) { // do nothing } float rzero = mq135_sensor.getRZero(); float correctedRZero = mq135_sensor.getCorrectedRZero(temp_hum_val[1], temp_hum_val[0]); float resistance = mq135_sensor.getResistance(); float ppm = mq135_sensor.getPPM(); float correctedPPM = mq135_sensor.getCorrectedPPM(temp_hum_val[1], temp_hum_val[0]); /* Serial.print("MQ135 RZero: "); Serial.print(rzero); Serial.print("\t Corrected RZero: "); Serial.print(correctedRZero); Serial.print("\t Resistance: "); Serial.print(resistance); Serial.print("\t PPM: "); Serial.print(ppm); Serial.print("\t Corrected PPM: "); Serial.print(correctedPPM); Serial.println("ppm"); */ lcd.clear(); //delay(100); lcd.setCursor(3, 1); lcd.print("PPM: "); lcd.print(correctedPPM); lcd.print(" ppm"); lcd.setCursor(0, 2); lcd.print("Temperature: "); lcd.print(temp_hum_val[1]); lcd.print(" C"); lcd.setCursor(2, 3); lcd.print("Humidity: "); lcd.print(temp_hum_val[0]); lcd.print("%"); float pm_sensor_reading = correctedPPM; if (pm_sensor_reading >= 0.0 && pm_sensor_reading <= 50.0) { lcd.setCursor(7, 0); lcd.print("Good"); //aqi_cat[] = s"Good"; redcolor = 0; greencolor = 228; bluecolor = 0; led.setColor(redcolor, greencolor, bluecolor); //green digitalWrite(BUZZER, LOW); Serial.println(F("Good")); } else if (pm_sensor_reading >= 51.0 && pm_sensor_reading <= 100.0) { lcd.setCursor(6, 0); lcd.print("Moderate"); //aqi_cat = "Moderate"; redcolor = 255; greencolor = 255; bluecolor = 0; led.setColor(redcolor, greencolor, bluecolor); //yellow digitalWrite(BUZZER, LOW); Serial.println(F("Moderate")); } else if (pm_sensor_reading >= 101.0 && pm_sensor_reading <= 150.0) { lcd.setCursor(3, 0); lcd.print("Unhealthy for"); lcd.setCursor(2, 1); lcd.print("Sensitive Groups"); //aqi_cat = "Unhealthy for Sensitive Groups"; redcolor = 255; greencolor = 126; bluecolor = 0; led.setColor(redcolor, greencolor, bluecolor); //orange digitalWrite(BUZZER, LOW); Serial.println(F("Unhealthy for Sensitive Group")); } else if (pm_sensor_reading >= 251.0 && pm_sensor_reading <= 200.0) { lcd.setCursor(5, 0); lcd.print("Unhealthy"); //aqi_cat = "Unhealthy"; redcolor = 255; greencolor = 0; bluecolor = 0; led.setColor(redcolor, greencolor, bluecolor); //red digitalWrite(BUZZER, HIGH); Serial.println(F("Unhealthy")); } else if (pm_sensor_reading >= 201.0 && pm_sensor_reading <= 300.0) { lcd.setCursor(3, 0); lcd.print("Very Unhealthy"); //aqi_cat = "Very Unhealthy"; redcolor = 143; greencolor = 63; bluecolor = 151; led.setColor(redcolor, greencolor, bluecolor); //purple digitalWrite(BUZZER, HIGH); Serial.println(F("Very Unhealthy")); } else if (pm_sensor_reading >= 301.0 && pm_sensor_reading <= 500.0) { lcd.setCursor(5, 0); lcd.print("Hazardous"); //aqi_cat = "Hazardous"; redcolor = 126; greencolor = 0; bluecolor = 35; led.setColor(redcolor, greencolor, bluecolor); //maroon digitalWrite(BUZZER, HIGH); Serial.println(F("Hazardous")); } else if (pm_sensor_reading >= 501.0) { lcd.setCursor(5, 0); lcd.print("Hazardous"); //aqi_cat = "Hazardous"; redcolor = 126; greencolor = 0; bluecolor = 35; led.setColor(redcolor, greencolor, bluecolor); //maroon digitalWrite(BUZZER, HIGH); Serial.println(F("Hazardous")); } else { redcolor = 0; greencolor = 0; bluecolor = 0; led.setColor(redcolor, greencolor, bluecolor); digitalWrite(BUZZER, LOW); lcd.setCursor(8, 0); lcd.print("None"); //aqi_cat = "None"; } readMillis = millis(); } }
- Save the Arduino sketch.
- Upload the demo. If you do not know how to upload the code, please check how to upload the code.
- Open the Serial Monitor of Arduino IDE by clicking Tool-> Serial Monitor. Or tap the CTRL+SHIFT+M keyboard keys at the same time. if everything goes well, you will get the readings.
Top Comments