Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP8266-Controlled CO2 Monitoring System with Multi-Color LED Indicators and Buzzer Alarm

Image of ESP8266-Controlled CO2 Monitoring System with Multi-Color LED Indicators and Buzzer Alarm

Circuit Documentation

Summary

This circuit is designed to monitor the concentration of CO2 in an environment using a SenseAir S8 CO2 sensor and an ESP-8266 microcontroller. The ESP-8266 reads the CO2 levels and controls a set of LEDs (red, green, yellow) and a buzzer to indicate air quality levels. The microcontroller is also capable of sending the CO2 data to a remote server via Wi-Fi for real-time monitoring. A push switch is included to reset the system.

Component List

  • SenseAir S8 CO2 Sensor: A sensor that measures the CO2 concentration in the air.
  • ESP-8266 Controller: A microcontroller with Wi-Fi capability for processing sensor data and controlling the LEDs and buzzer.
  • LED (Red): Indicates high CO2 levels or an alarm condition.
  • LED (Green): Indicates normal CO2 levels.
  • LED (Yellow): Indicates moderate CO2 levels.
  • Buzzer: Emits an audible alarm when CO2 levels are too high.
  • 2Pin Push Switch: Used to reset the system.

Wiring Details

SenseAir S8 CO2 Sensor

  • G+: Connected to the Vin pin of the ESP-8266 Controller.
  • G0: Connected to the GND pin of the ESP-8266 Controller.
  • RX: Connected to the D7 pin of the ESP-8266 Controller.
  • TX: Connected to the D8 pin of the ESP-8266 Controller.

ESP-8266 Controller

  • D0: Connected to the cathode of the Yellow LED.
  • D1: Connected to the cathode of the Green LED.
  • D5: Connected to the cathode of the Red LED.
  • D6: Connected to the PIN of the Buzzer.
  • RST: Connected to the Input + of the 2Pin Push Switch.
  • GND: Common ground connected to the cathodes of all LEDs, the GND of the Buzzer, and the Output + of the 2Pin Push Switch.

LEDs

  • Anodes: All anodes of the Red, Green, and Yellow LEDs are connected to the common ground.

Buzzer

  • GND: Connected to the common ground.

2Pin Push Switch

  • Output +: Connected to the GND pin of the ESP-8266 Controller.

Documented Code

Microcontroller Code (ESP-8266)

#include <AirGradient.h>
#include <ESP8266WiFi.h>
#include "SoftwareSerial.h"
#include <ThingSpeak.h>

const char* ssid = "INFINITUM010D_2.4";
const char* password ="JrxgMmuYt6";
const char* server = "api.thingspeak.com";
String apiKey = "C11FT8M5PFLKQPRM";
int duration = 60; // Delay between each data measure and uploading

int led1V = D1; // Green LED
int led2A = D0; // Yellow LED
int led3R = D5; // Red LED
int led4Buzzer = D6; // Buzzer

#define RX_PIN D7
#define TX_PIN D8

WiFiClient client;
SoftwareSerial co2SensorSerial(RX_PIN, TX_PIN);
byte co2Response[] = {0, 0, 0, 0, 0, 0, 0};

void setup() {
  pinMode(led1V, OUTPUT);
  pinMode(led2A, OUTPUT);
  pinMode(led3R, OUTPUT);
  pinMode(led4Buzzer, OUTPUT);

  Serial.begin(115200);
  co2SensorSerial.begin(9600);
  Serial.println();
  Serial.print("Wifi connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  Serial.println();
  Serial.print("Connecting");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}

void loop() {
  int co2 = requestCO2();
  Serial.printf("CO2 %d ppm\n", co2);
  delay(5000);

  if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(co2);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    Serial.println("%. Send to Thingspeak.");
  }

  // LED and buzzer logic based on CO2 levels
  if (co2 >= 400 && co2 < 700) {
    digitalWrite(led1V, HIGH);
    digitalWrite(led2A, LOW);
    digitalWrite(led3R, LOW);
  } else if (co2 >= 701 && co2 < 999) {
    digitalWrite(led2A, HIGH);
    digitalWrite(led1V, LOW);
    digitalWrite(led3R, LOW);
    digitalWrite(led4Buzzer, LOW);
  } else if (co2 >= 1000) {
    digitalWrite(led3R, HIGH);
    digitalWrite(led2A, LOW);
    digitalWrite(led1V, LOW);
    delay(5000);
    digitalWrite(led4Buzzer, LOW);
  }

  if (co2 > 1500 && co2 < 65533) {
    digitalWrite(led3R, HIGH);
    digitalWrite(led2A, LOW);
    digitalWrite(led1V, LOW);
    digitalWrite(led4Buzzer, LOW);
    delay(5000);
    digitalWrite(led4Buzzer, LOW);
  }
}

int requestCO2() {
  static byte readCommand[] = {0xFE, 0x44, 0x00, 0x08, 0x02, 0x9F, 0x25};
  byte cmd_s8[]= {0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5};
  byte abc_s8[]= {0xFE, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xA1, 0xC3};
  byte response_s8[7] = {0, 0, 0, 0, 0, 0, 0};

  int readAttempt = 0;
  while (!co2SensorSerial.available()) {
    co2SensorSerial.write(readCommand, 7);
    readAttempt++;
    delay(1000);
    if (readAttempt >= 5) {
      Serial.println(F("Failed to request CO2. Skipping it..."));
      return 0;
    }
  }

  int timeout = 0;
  while (co2SensorSerial.available() < 7) {
    timeout++;
    if (timeout > 10) {
      while (co2SensorSerial.available()) {
        co2SensorSerial.read();
      }
      break;
    }
    delay(50);
  }

  for (int i = 0; i < 7; i++) {
    co2Response[i] = co2SensorSerial.read();
  }

  int high = co2Response[3];
  int low = co2Response[4];

  int val = high * 256 + low;
  return val * 1;

  client.stop();
  Serial.println("Waiting…");
  // thingspeak needs minimum 15 sec delay between updates
  delay(duration * 1000);
}

Additional Documentation

The additional documentation provided in the code input is a detailed explanation of the importance of CO2 monitoring for health and safety, particularly in the context of preventing the spread of COVID-19. It describes how CO2 levels can indicate the quality of ventilation in a space and how this relates to the potential for airborne transmission of the virus. It also explains the operation of CO2 meters, particularly non-dispersive infrared (NDIR) sensors, and how they can be used to ensure a safe environment by alerting when CO2 concentrations reach levels that may indicate poor ventilation.