#include <Wire.h>
#define CO2_SENSOR_PIN 36 // Analog pin for MG811 sensor
#define CO2_PUMP_PIN 27 // GPIO pin to control CO2 pump/solenoid valve
float co2Level = 0.0;
const float CO2_LOW_THRESHOLD = 400.0; // Minimum CO2 level (ppm)
const float CO2_HIGH_THRESHOLD = 1000.0; // Maximum CO2 level (ppm)
void setup() {
Serial.begin(9600);
// Initialize the CO2 pump control pin
pinMode(CO2_PUMP_PIN, OUTPUT);
digitalWrite(CO2_PUMP_PIN, LOW); // Turn off the CO2 pump initially
Serial.println("CO2 Control System");
}
void loop() {
co2Level = readCO2Sensor(); // Read the CO2 level from the sensor
// Control the CO2 pump based on CO2 levels
controlCO2Pump(co2Level);
delay(2000); // Adjust delay as per the required sampling rate
}
// Function to read the CO2 sensor
float readCO2Sensor() {
int co2Raw = analogRead(CO2_SENSOR_PIN); // Read raw value from sensor
float co2 = map(co2Raw, 0, 4095, 0, 5000); // Convert to CO2 ppm range
Serial.print("CO2 Level: ");
Serial.print(co2);
Serial.println(" ppm");
return co2;
}
// Function to control the CO2 pump
void controlCO2Pump(float co2) {
if (co2 < CO2_LOW_THRESHOLD) {
// If CO2 level is too low, turn on the CO2 pump
digitalWrite(CO2_PUMP_PIN, HIGH);
Serial.println("Air Pump ON: Increasing CO2 levels...");
} else if (co2 > CO2_HIGH_THRESHOLD) {
// If CO2 level is too high, turn off the CO2 pump
digitalWrite(CO2_PUMP_PIN, LOW);
Serial.println("Air Pump OFF: CO2 levels stable or too high.");
} else {
// Within acceptable range, keep the pump off
Serial.println("CO2 Level Stable: No action needed.");
}
}