Advertisement
pleasedontcode

Sensor Calibration rev_01

Oct 14th, 2024
70
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: Sensor Calibration
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-14 19:43:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Messenger app */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* The messenger app will feature a dashboard */
  23.     /* displaying sensor data, allowing users to */
  24.     /* visualize trends over time. It will support data */
  25.     /* logging for historical analysis and reporting. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <MQUnifiedsensor.h>    //https://github.com/miguel5612/MQSensorsLib
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Msd_MQ6_DOUT_PIN_D2       = 2;
  37. const uint8_t Msd_MQ6_DOUT_PIN_D3       = 3;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t Msd_MQ6_AOUT_PIN_A0       = A0;
  41. const uint8_t Msd_MQ6_AOUT_PIN_A1       = A1;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. // Definitions for the MQ6 sensor
  45. #define placa "Arduino UNO" // Define the board type
  46. #define Voltage_Resolution 5 // Voltage resolution
  47. #define ADC_Bit_Resolution 10 // ADC bit resolution
  48. #define RatioMQ6CleanAir 10 // Ratio for clean air calibration
  49.  
  50. // Declare Sensor using MQUnifiedsensor constructor
  51. MQUnifiedsensor MQ6(placa, Voltage_Resolution, ADC_Bit_Resolution, Msd_MQ6_AOUT_PIN_A0, "MQ-6");
  52.  
  53. // Variables for logging data
  54. float sensorValue = 0; // Variable to store sensor value
  55. unsigned long previousMillis = 0; // Store the last time data was logged
  56. const long interval = 1000; // Interval for logging data (1 second)
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     pinMode(Msd_MQ6_DOUT_PIN_D2, INPUT_PULLUP);
  62.     pinMode(Msd_MQ6_DOUT_PIN_D3, INPUT_PULLUP);
  63.     pinMode(Msd_MQ6_AOUT_PIN_A0, INPUT);
  64.     pinMode(Msd_MQ6_AOUT_PIN_A1, INPUT);
  65.  
  66.     // Initialize the sensor
  67.     MQ6.init(); // Initialize the MQ6 sensor
  68.  
  69.     // Calibration
  70.     Serial.begin(9600); // Start serial communication
  71.     Serial.print("Calibrating please wait.");
  72.     float calcR0 = 0;
  73.     for(int i = 0; i < 10; i++) {
  74.         MQ6.update(); // Update sensor data
  75.         calcR0 += MQ6.calibrate(RatioMQ6CleanAir); // Calibrate and accumulate R0
  76.         Serial.print(".");
  77.     }
  78.     MQ6.setR0(calcR0 / 10); // Set the average R0 value
  79.     Serial.println(" done!");
  80.  
  81.     if (isinf(calcR0) || calcR0 == 0) {
  82.         Serial.println("Warning: Connection issue detected.");
  83.         while (1); // Stop execution if there's an issue
  84.     }
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // put your main code here, to run repeatedly:
  90.     MQ6.update(); // Update sensor data
  91.     MQ6.readSensor(); // Read the sensor value
  92.     MQ6.serialDebug(); // Print debug information to serial
  93.  
  94.     // Log data to Serial every second
  95.     unsigned long currentMillis = millis();
  96.     if (currentMillis - previousMillis >= interval) {
  97.         previousMillis = currentMillis; // Save the last time data was logged
  98.         sensorValue = MQ6.getPPM(); // Get the sensor value in PPM
  99.         Serial.print("Sensor Value (PPM): ");
  100.         Serial.println(sensorValue); // Print the sensor value to Serial
  101.         // Here you could add code to visualize trends over time
  102.         // and support data logging for historical analysis
  103.     }
  104.  
  105.     delay(500); // Delay for sampling frequency
  106. }
  107.  
  108. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement