CCCPaste

MQ-135 Monitoring rev_03

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: MQ-135 Monitoring
	- Source Code NOT compiled for: XIAO ESP32S3
	- Source Code created on: 2025-10-24 00:16:05

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Integrate a gas sensor module (e.g., MQ-135) with */
	/* the XIAO ESP32S3 to enable real-time air quality */
	/* measurement, providing alerts when pollutant */
	/* levels exceed thresholds. */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include \t//https://github.com/miguel5612/MQSensorsLib

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t sensor_MQ135_AOUT_PIN_A0\t\t= A0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Gas sensor MQ-135 connected to analog A0 of the XIAO ESP32S3
// Board string to identify the MCU family
#define BOARD_STRING_MQ135        ("XIAO-ESP32S3")

// ADC and voltage resolution specifics for ESP32
#define VOLTAGE_RESOLUTION         (3.3) // ESP32 typically 3.3V reference
#define ADC_BIT_RESOLUTION         (12)  // 12-bit ADC on ESP32
#define MQ135_SENSOR_TYPE            ("MQ-135")

// Clean air RS/R0 ratio (from datasheet/examples)
#define RatioMQ135CleanAir         (3.6)

// LED alert output (optional, connect a LED with current limiting resistor to this pin)
#define LED_ALERT_PIN              (25)

// MQ-135 object
MQUnifiedsensor MQ135(BOARD_STRING_MQ135, VOLTAGE_RESOLUTION, ADC_BIT_RESOLUTION, sensor_MQ135_AOUT_PIN_A0, MQ135_SENSOR_TYPE);


void setup(void)
{
	// put your setup code here, to run once:

	// Initialize serial communication for debugging
	Serial.begin(115200);
	// Optional: set LED pin mode
	pinMode(LED_ALERT_PIN, OUTPUT);
	digitalWrite(LED_ALERT_PIN, LOW);

	// Gas concentration model for CO2 (common MQ-135 calibration data)
	// You can switch to another gas by setting A/B with MQ135.setA()/setB()
	MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b
	MQ135.setA(110.47); MQ135.setB(-2.862); // Configure CO2 concentration equation

	// Initialize the sensor
	MQ135.init(); 

	// Calibrate in clean air (RS/R0 ratio)
	Serial.print("Calibrating please wait.");
	float calcR0 = 0;
	for(int i = 1; i <= 10; i++)
	{
		MQ135.update(); // read voltage from analog pin
		calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
		Serial.print(".");
		delay(100);
	}
	MQ135.setR0(calcR0/10);
	Serial.println("  done!.");

	if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
	if(calcR0 == 0){Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
	
	// Optional: print debug header information
	MQ135.serialDebug(true);
}

void loop(void)
{
	static uint32_t lastMillis = 0;
	const uint32_t INTERVAL_MS = 1000; // 1 second interval for real-time updates

	uint32_t now = millis();
	if(now - lastMillis >= INTERVAL_MS)
	{
		lastMillis = now;
		MQ135.update();
		float correctionFactor = 0.0f; // can be extended with temperature/humidity compensation
		float co2ppm = MQ135.readSensor(false, correctionFactor); // CO2 ppm estimation using CO2 model
		// Print reading
		Serial.print("CO2 (ppm): "); Serial.println(co2ppm);

		// Simple alert mechanism: alert when CO2 exceeds threshold
		const float CO2_THRESHOLD = 1000.0f; // threshold in ppm
		if(co2ppm > CO2_THRESHOLD)
		{
			// Turn on alert LED (or buzzer)
			digitalWrite(LED_ALERT_PIN, HIGH);
			Serial.println("ALERT: CO2 concentration above threshold!");
		}
		else
		{
			digitalWrite(LED_ALERT_PIN, LOW);
		}
	}
}

/* END CODE */