pleasedontcode

MQ-135 Monitoring rev_03

Oct 23rd, 2025
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: MQ-135 Monitoring
  13.     - Source Code NOT compiled for: XIAO ESP32S3
  14.     - Source Code created on: 2025-10-24 00:16:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Integrate a gas sensor module (e.g., MQ-135) with */
  21.     /* the XIAO ESP32S3 to enable real-time air quality */
  22.     /* measurement, providing alerts when pollutant */
  23.     /* levels exceed thresholds. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <MQUnifiedsensor.h>\t//https://github.com/miguel5612/MQSensorsLib
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF ANALOG INPUT PINS *****/
  37. const uint8_t sensor_MQ135_AOUT_PIN_A0\t\t= A0;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Gas sensor MQ-135 connected to analog A0 of the XIAO ESP32S3
  41. // Board string to identify the MCU family
  42. #define BOARD_STRING_MQ135        ("XIAO-ESP32S3")
  43.  
  44. // ADC and voltage resolution specifics for ESP32
  45. #define VOLTAGE_RESOLUTION         (3.3) // ESP32 typically 3.3V reference
  46. #define ADC_BIT_RESOLUTION         (12)  // 12-bit ADC on ESP32
  47. #define MQ135_SENSOR_TYPE            ("MQ-135")
  48.  
  49. // Clean air RS/R0 ratio (from datasheet/examples)
  50. #define RatioMQ135CleanAir         (3.6)
  51.  
  52. // LED alert output (optional, connect a LED with current limiting resistor to this pin)
  53. #define LED_ALERT_PIN              (25)
  54.  
  55. // MQ-135 object
  56. MQUnifiedsensor MQ135(BOARD_STRING_MQ135, VOLTAGE_RESOLUTION, ADC_BIT_RESOLUTION, sensor_MQ135_AOUT_PIN_A0, MQ135_SENSOR_TYPE);
  57.  
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.  
  63.     // Initialize serial communication for debugging
  64.     Serial.begin(115200);
  65.     // Optional: set LED pin mode
  66.     pinMode(LED_ALERT_PIN, OUTPUT);
  67.     digitalWrite(LED_ALERT_PIN, LOW);
  68.  
  69.     // Gas concentration model for CO2 (common MQ-135 calibration data)
  70.     // You can switch to another gas by setting A/B with MQ135.setA()/setB()
  71.     MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b
  72.     MQ135.setA(110.47); MQ135.setB(-2.862); // Configure CO2 concentration equation
  73.  
  74.     // Initialize the sensor
  75.     MQ135.init();
  76.  
  77.     // Calibrate in clean air (RS/R0 ratio)
  78.     Serial.print("Calibrating please wait.");
  79.     float calcR0 = 0;
  80.     for(int i = 1; i <= 10; i++)
  81.     {
  82.         MQ135.update(); // read voltage from analog pin
  83.         calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
  84.         Serial.print(".");
  85.         delay(100);
  86.     }
  87.     MQ135.setR0(calcR0/10);
  88.     Serial.println("  done!.");
  89.  
  90.     if(isinf(calcR0)) {Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
  91.     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);}
  92.    
  93.     // Optional: print debug header information
  94.     MQ135.serialDebug(true);
  95. }
  96.  
  97. void loop(void)
  98. {
  99.     static uint32_t lastMillis = 0;
  100.     const uint32_t INTERVAL_MS = 1000; // 1 second interval for real-time updates
  101.  
  102.     uint32_t now = millis();
  103.     if(now - lastMillis >= INTERVAL_MS)
  104.     {
  105.         lastMillis = now;
  106.         MQ135.update();
  107.         float correctionFactor = 0.0f; // can be extended with temperature/humidity compensation
  108.         float co2ppm = MQ135.readSensor(false, correctionFactor); // CO2 ppm estimation using CO2 model
  109.         // Print reading
  110.         Serial.print("CO2 (ppm): "); Serial.println(co2ppm);
  111.  
  112.         // Simple alert mechanism: alert when CO2 exceeds threshold
  113.         const float CO2_THRESHOLD = 1000.0f; // threshold in ppm
  114.         if(co2ppm > CO2_THRESHOLD)
  115.         {
  116.             // Turn on alert LED (or buzzer)
  117.             digitalWrite(LED_ALERT_PIN, HIGH);
  118.             Serial.println("ALERT: CO2 concentration above threshold!");
  119.         }
  120.         else
  121.         {
  122.             digitalWrite(LED_ALERT_PIN, LOW);
  123.         }
  124.     }
  125. }
  126.  
  127. /* END CODE */
  128.  
Advertisement
Add Comment
Please, Sign In to add comment