Advertisement
pleasedontcode

"Alcohol Sensor" rev_01

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