Advertisement
pleasedontcode

Gas Safety rev_01

Oct 11th, 2024
72
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: Gas Safety
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-11 13:21:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* This code will This code will to make lpg gas */
  21.     /* detection using mq2 sensor and servo motor to */
  22.     /* controlled the gas regulator and also include */
  23.     /* flame sensor and water pumb using Extinguish the */
  24.     /* fire and it controlled by relaymoduel and exhuste */
  25.     /* fan and inclu */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <MQUnifiedsensor.h>    //https://github.com/miguel5612/MQSensorsLib
  30. #include <Servo.h>            // Include the Servo library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t my_MQ2_DOUT_PIN_D2 = 2;      // Digital pin for MQ2 sensor
  38. const uint8_t flameSensorPin = 3;          // Digital pin for flame sensor
  39. const uint8_t relayPin = 4;                 // Digital pin for relay module (water pump)
  40. const uint8_t exhaustFanPin = 5;            // Digital pin for exhaust fan
  41. const uint8_t servoPin = 6;                 // Digital pin for servo motor
  42.  
  43. /***** DEFINITION OF ANALOG INPUT PINS *****/
  44. const uint8_t my_MQ2_AOUT_PIN_A0 = A0;      // Analog pin for MQ2 sensor
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  47. #define Board                   ("Arduino UNO")
  48. #define Voltage_Resolution      (5)
  49. #define ADC_Bit_Resolution      (10)
  50. #define RatioMQ2CleanAir       (9.83) // RS / R0 = 9.83 ppm
  51.  
  52. // Create an instance of the MQUnifiedsensor class for the MQ2 sensor
  53. MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, my_MQ2_AOUT_PIN_A0, "MQ-2");
  54. Servo gasRegulator; // Create a Servo object for the gas regulator
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize serial communication for debugging
  59.     Serial.begin(9600);
  60.  
  61.     // Set up the digital and analog pins
  62.     pinMode(my_MQ2_DOUT_PIN_D2, INPUT_PULLUP);
  63.     pinMode(flameSensorPin, INPUT); // Set flame sensor pin as input
  64.     pinMode(relayPin, OUTPUT);       // Set relay pin as output for water pump
  65.     pinMode(exhaustFanPin, OUTPUT);  // Set exhaust fan pin as output
  66.  
  67.     // Attach the servo motor to its pin
  68.     gasRegulator.attach(servoPin);
  69.  
  70.     // Initialize the MQ2 sensor
  71.     MQ2.setRegressionMethod(1); //_PPM =  a*ratio^b
  72.     MQ2.setA(574.25);
  73.     MQ2.setB(-2.222);
  74.     MQ2.init();
  75.  
  76.     // Calibration process
  77.     Serial.print("Calibrating please wait.");
  78.     float calcR0 = 0;
  79.     for(int i = 0; i < 10; i++) // Start from 0 for clarity
  80.     {
  81.         MQ2.update(); // Update data, the Arduino will read the voltage from the analog pin
  82.         calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
  83.         Serial.print(".");
  84.     }
  85.     MQ2.setR0(calcR0 / 10);
  86.     Serial.println(" done!");
  87.  
  88.     MQ2.serialDebug(true); // Enable serial debugging for the sensor
  89. }
  90.  
  91. void loop(void)
  92. {
  93.     MQ2.update(); // Update data, the Arduino will read the voltage from the analog pin
  94.     MQ2.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
  95.     MQ2.serialDebug(); // Will print the table on the serial port
  96.  
  97.     // Check for LPG gas concentration
  98.     if (MQ2.getPPM() > 200) { // Threshold for LPG gas detection
  99.         Serial.println("LPG gas detected!");
  100.         digitalWrite(relayPin, HIGH); // Activate water pump to extinguish fire
  101.         digitalWrite(exhaustFanPin, HIGH); // Turn on exhaust fan
  102.         gasRegulator.write(90); // Open gas regulator (adjust angle as needed)
  103.     } else {
  104.         digitalWrite(relayPin, LOW); // Deactivate water pump
  105.         digitalWrite(exhaustFanPin, LOW); // Turn off exhaust fan
  106.         gasRegulator.write(0); // Close gas regulator
  107.     }
  108.  
  109.     // Check flame sensor
  110.     if (digitalRead(flameSensorPin) == HIGH) {
  111.         Serial.println("Flame detected! Activating safety measures.");
  112.         digitalWrite(relayPin, HIGH); // Activate water pump
  113.         digitalWrite(exhaustFanPin, HIGH); // Turn on exhaust fan
  114.     }
  115.  
  116.     delay(500); // Sampling frequency
  117. }
  118.  
  119. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement