CCCPaste

MQ2 Setup rev_01

/********* 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: MQ2 Setup
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-08-24 03:13:21

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Develop an Arduino-based air quality monitoring */
	/* project utilizing the MQ2 sensor. The system */
	/* should read both digital and analog signals, */
	/* providing accurate gas concentration levels for */
	/* various applications. */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t ASAP_MQ2_DOUT_PIN_D13 = 13; // Digital output pin for MQ2 sensor

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t ASAP_MQ2_AOUT_PIN_D4 = 36; // Using GPIO 36 for analog input

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Define board type and parameters for MQ2 sensor
#define Board                   ("ESP-32")
#define Type                    ("MQ-2") // MQ2 sensor type
#define Voltage_Resolution      (3.3) // Voltage resolution for ESP32
#define ADC_Bit_Resolution      (12) // 12-bit ADC resolution
#define RatioMQ2CleanAir        (9.83) // RS/R0 ratio for clean air

// Create an instance of the MQUnifiedsensor class for the MQ2 sensor
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, ASAP_MQ2_AOUT_PIN_D4, Type);

void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(9600);

    // Initialize the MQ2 sensor
    MQ2.init();
    
    // Set regression method and coefficients for MQ2
    MQ2.setRegressionMethod(1); //_PPM = a * ratio^b
    MQ2.setA(574.25); 
    MQ2.setB(-2.222); // Configure the equation to calculate LPG concentration

    // Calibrate the sensor
    Serial.print("Calibrating please wait.");
    float calcR0 = 0;
    for (int i = 0; i < 10; i++) {
        MQ2.update(); // Update data, the ESP32 will read the voltage from the analog pin
        calcR0 += MQ2.calibrate(RatioMQ2CleanAir); // Calibrate the sensor based on clean air ratio
        Serial.print(".");
    }
    MQ2.setR0(calcR0 / 10); // Set the R0 value
    Serial.println(" done!");

    // Enable serial debugging for the sensor readings
    MQ2.serialDebug(true);
}

void loop(void)
{
    // Update sensor data
    MQ2.update();
    // Read the sensor and print the results to the serial monitor
    MQ2.readSensor(); // Read the current sensor values
    MQ2.serialDebug(); // Will print the table on the serial port
    delay(500); // Sampling frequency
}

/* END CODE */