Advertisement
pleasedontcode

"Alcohol Sensor" rev_02

Jun 9th, 2024
345
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: "Alcohol Sensor"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-09 07:47:41
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read alcohol and print on serial */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <MQUnifiedsensor.h>  // https://github.com/miguel5612/MQSensorsLib
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t sensor_MQ303A_DOUT_PIN_D2 = 2;
  33.  
  34. /***** DEFINITION OF ANALOG INPUT PINS *****/
  35. const uint8_t sensor_MQ303A_AOUT_PIN_A0 = A0;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. #define Board ("Arduino Mega")
  39. #define Type ("MQ-303A")
  40. #define Voltage_Resolution (5)
  41. #define ADC_Bit_Resolution (10)
  42. #define RatioMQ303ACleanAir (60) // RS / R0 = 60 ppm
  43.  
  44. MQUnifiedsensor MQ303A(Board, Voltage_Resolution, ADC_Bit_Resolution, sensor_MQ303A_AOUT_PIN_A0, Type);
  45.  
  46. void setup(void)
  47. {
  48.   // put your setup code here, to run once:
  49.   Serial.begin(9600); // Start serial communication at 9600 baud rate
  50.  
  51.   pinMode(sensor_MQ303A_DOUT_PIN_D2, INPUT_PULLUP); // Set digital pin as input with pull-up resistor
  52.   pinMode(sensor_MQ303A_AOUT_PIN_A0, INPUT); // Set analog pin as input
  53.  
  54.   MQ303A.setRegressionMethod(1); // Set regression method to Exponential
  55.   MQ303A.setA(0.3934); // Set 'a' coefficient
  56.   MQ303A.setB(-1.504); // Set 'b' coefficient
  57.  
  58.   MQ303A.init(); // Initialize the sensor
  59.  
  60.   Serial.print("Calibrating please wait.");
  61.   float calcR0 = 0;
  62.   for (int i = 1; i <= 10; i++) {
  63.     MQ303A.update(); // Update sensor readings
  64.     calcR0 += MQ303A.calibrate(RatioMQ303ACleanAir); // Calibrate sensor
  65.     Serial.print(".");
  66.   }
  67.   MQ303A.setR0(calcR0 / 10); // Set the calibrated R0 value
  68.   Serial.println("  done!.");
  69.  
  70.   if (isinf(calcR0)) {
  71.     Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
  72.     while (1); // Halt the program
  73.   }
  74.   if (calcR0 == 0) {
  75.     Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
  76.     while (1); // Halt the program
  77.   }
  78. }
  79.  
  80. void loop(void)
  81. {
  82.   // put your main code here, to run repeatedly:
  83.   MQ303A.update(); // Update sensor readings
  84.   float alcoholPPM = MQ303A.readSensor(); // Read alcohol concentration in PPM
  85.   Serial.print("Alcohol now (PPM): ");
  86.   Serial.println(alcoholPPM);
  87.   delay(500); // Delay for 500 milliseconds
  88. }
  89.  
  90. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement