Advertisement
pleasedontcode

MQ2 Setup rev_01

Aug 23rd, 2024
162
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: MQ2 Setup
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-24 03:13:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop an Arduino-based air quality monitoring */
  21.     /* project utilizing the MQ2 sensor. The system */
  22.     /* should read both digital and analog signals, */
  23.     /* providing accurate gas concentration levels for */
  24.     /* various applications. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <MQUnifiedsensor.h> // https://github.com/miguel5612/MQSensorsLib
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t ASAP_MQ2_DOUT_PIN_D13 = 13; // Digital output pin for MQ2 sensor
  36.  
  37. /***** DEFINITION OF ANALOG INPUT PINS *****/
  38. const uint8_t ASAP_MQ2_AOUT_PIN_D4 = 36; // Using GPIO 36 for analog input
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. // Define board type and parameters for MQ2 sensor
  42. #define Board                   ("ESP-32")
  43. #define Type                    ("MQ-2") // MQ2 sensor type
  44. #define Voltage_Resolution      (3.3) // Voltage resolution for ESP32
  45. #define ADC_Bit_Resolution      (12) // 12-bit ADC resolution
  46. #define RatioMQ2CleanAir        (9.83) // RS/R0 ratio for clean air
  47.  
  48. // Create an instance of the MQUnifiedsensor class for the MQ2 sensor
  49. MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, ASAP_MQ2_AOUT_PIN_D4, Type);
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize serial communication for debugging
  54.     Serial.begin(9600);
  55.  
  56.     // Initialize the MQ2 sensor
  57.     MQ2.init();
  58.    
  59.     // Set regression method and coefficients for MQ2
  60.     MQ2.setRegressionMethod(1); //_PPM = a * ratio^b
  61.     MQ2.setA(574.25);
  62.     MQ2.setB(-2.222); // Configure the equation to calculate LPG concentration
  63.  
  64.     // Calibrate the sensor
  65.     Serial.print("Calibrating please wait.");
  66.     float calcR0 = 0;
  67.     for (int i = 0; i < 10; i++) {
  68.         MQ2.update(); // Update data, the ESP32 will read the voltage from the analog pin
  69.         calcR0 += MQ2.calibrate(RatioMQ2CleanAir); // Calibrate the sensor based on clean air ratio
  70.         Serial.print(".");
  71.     }
  72.     MQ2.setR0(calcR0 / 10); // Set the R0 value
  73.     Serial.println(" done!");
  74.  
  75.     // Enable serial debugging for the sensor readings
  76.     MQ2.serialDebug(true);
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // Update sensor data
  82.     MQ2.update();
  83.     // Read the sensor and print the results to the serial monitor
  84.     MQ2.readSensor(); // Read the current sensor values
  85.     MQ2.serialDebug(); // Will print the table on the serial port
  86.     delay(500); // Sampling frequency
  87. }
  88.  
  89. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement