Advertisement
pleasedontcode

"CO Sensor" rev_01

Jun 9th, 2024
412
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: "CO Sensor"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-09 12:54:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if button is pressed, so read CO data from sensor */
  21.     /* and print on serial. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  26. #include <MQUnifiedsensor.h>  //https://github.com/miguel5612/MQSensorsLib
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void onPressed();  // Prototype for the button press callback function
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t button_PushButton_PIN_D2 = 2;
  35. const uint8_t CO_Sensor_MQ7_DOUT_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF ANALOG INPUT PINS *****/
  38. const uint8_t CO_Sensor_MQ7_AOUT_PIN_A0 = A0;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. EasyButton button(button_PushButton_PIN_D2);  // Instantiate EasyButton object
  42.  
  43. // Hardware Related Macros
  44. #define Board "Arduino Pro Mini"
  45. #define Pin A0  // Analog input pin for the sensor
  46. #define PreheatPin5 3  // Preheat pin for 5V
  47. #define PreheatPin14 4  // Preheat pin for 1.4V
  48.  
  49. // Software Related Macros
  50. #define Type "MQ-7"
  51. #define Voltage_Resolution 5
  52. #define ADC_Bit_Resolution 10
  53. #define RatioMQ7CleanAir 27.5
  54.  
  55. // Declare Sensor
  56. MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
  57.  
  58. void onPressed() {
  59.   Serial.println("Button pressed");
  60.  
  61.   // Read CO data from the sensor
  62.   MQ7.update();
  63.   float CO_concentration = MQ7.readSensor();
  64.  
  65.   // Print CO concentration on serial
  66.   Serial.print("CO Concentration: ");
  67.   Serial.print(CO_concentration);
  68.   Serial.println(" ppm");
  69. }
  70.  
  71. void setup(void) {
  72.   // put your setup code here, to run once:
  73.   Serial.begin(115200);
  74.   Serial.println();
  75.   Serial.println(">>> EasyButton pressed example <<<");
  76.  
  77.   pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  78.   pinMode(CO_Sensor_MQ7_DOUT_PIN_D3, INPUT_PULLUP);
  79.   pinMode(CO_Sensor_MQ7_AOUT_PIN_A0, INPUT);
  80.  
  81.   button.begin();  // Initialize the button
  82.   button.onPressed(onPressed);  // Attach the callback function to the button press event
  83.  
  84.   pinMode(PreheatPin5, OUTPUT);
  85.   pinMode(PreheatPin14, OUTPUT);
  86.  
  87.   // Set regression method and constants for CO concentration
  88.   MQ7.setRegressionMethod(1);
  89.   MQ7.setA(99.042);
  90.   MQ7.setB(-1.518);
  91.  
  92.   // Initialize the sensor
  93.   MQ7.init();
  94.  
  95.   // Preheat cycle
  96.   Serial.println("Preheating, please wait 2.5 minutes");
  97.   digitalWrite(PreheatPin5, HIGH);
  98.   digitalWrite(PreheatPin14, LOW);
  99.   delay(60000);
  100.   digitalWrite(PreheatPin5, LOW);
  101.   digitalWrite(PreheatPin14, HIGH);
  102.   delay(90000);
  103.   digitalWrite(PreheatPin5, HIGH);
  104.   digitalWrite(PreheatPin14, LOW);
  105.   delay(60000);
  106.   digitalWrite(PreheatPin5, LOW);
  107.   digitalWrite(PreheatPin14, HIGH);
  108.   delay(90000);
  109.   digitalWrite(PreheatPin5, HIGH);
  110.   digitalWrite(PreheatPin14, LOW);
  111.  
  112.   // Calibration
  113.   Serial.print("Calibrating, please wait.");
  114.   float calcR0 = 0;
  115.   for (int i = 1; i <= 10; i++) {
  116.     MQ7.update();
  117.     calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
  118.     Serial.print(".");
  119.   }
  120.   MQ7.setR0(calcR0 / 10);
  121.   Serial.println(" done!");
  122.  
  123.   if (isinf(calcR0)) {
  124.     Serial.println("Warning: Connection issue, R0 is infinite. Please check wiring and supply.");
  125.     while (1);
  126.   }
  127.   if (calcR0 == 0) {
  128.     Serial.println("Warning: Connection issue, R0 is zero. Please check wiring and supply.");
  129.     while (1);
  130.   }
  131.  
  132.   MQ7.serialDebug(true);
  133. }
  134.  
  135. void loop(void) {
  136.   // put your main code here, to run repeatedly:
  137.   button.read();  // Continuously read the status of the button
  138.  
  139.   digitalWrite(PreheatPin5, LOW);
  140.   digitalWrite(PreheatPin14, HIGH);
  141.   delay(90000);
  142.   digitalWrite(PreheatPin5, HIGH);
  143.   digitalWrite(PreheatPin14, LOW);
  144.  
  145.   MQ7.update();
  146.   MQ7.readSensor();
  147.   MQ7.serialDebug();
  148. }
  149.  
  150. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement