Gas leak detector project with free code and circuit diagram
Arduino Code-https://github.com/futuristiciox/gas_leak_detector
#include <Servo.h>
const int gasSensorPin = A0; // Analog pin connected to the MQ2 gas sensor
const int ledPin = 12;
const int servoPin = 3; // Digital pin connected to the servo
const int buzzerPin = 10; // Digital pin connected to the buzzer
const int exhaustFanPin = 9;
const int homeLightInterlockPin = 8;
const int gasThreshold = 400; // Threshold value for gas detection (adjust based on your testing)
Servo myServo; // Create servo object to control a servo
bool alarmFlag = false; // Flag to check if the servo has already moved
void setup() {
myServo.attach(servoPin);
myServo.write(0); // Initial position of the servo
pinMode(gasSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(exhaustFanPin, OUTPUT);
pinMode(homeLightInterlockPin, OUTPUT);
digitalWrite(homeLightInterlockPin, HIGH);
digitalWrite(buzzerPin, LOW);
digitalWrite(exhaustFanPin, LOW);
Serial.begin(9600); // Initialize serial communication for debugging
delay(1000);
}
void loop() {
int gasLevel = analogRead(gasSensorPin); // Read the gas sensor value
Serial.print("Gas Sensor Value= ");
Serial.println(gasLevel); // Print the gas sensor value to the serial monitor for debugging
if (gasLevel > gasThreshold && !alarmFlag) {
myServo.write(90); // Rotate the servo to 90 degrees
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
digitalWrite(exhaustFanPin, HIGH);
digitalWrite(homeLightInterlockPin, LOW);
delay(5000); // Beep for 2 seconds
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(exhaustFanPin, LOW);
alarmFlag = true; // Set the flag to indicate the servo has moved
}
delay(500); // Wait for 1 second before checking the gas sensor value again
}
### DIY Gas Leak Detector with Arduino Uno
#### Introduction
In this blog, we'll walk you through creating a gas leak detector using an Arduino Uno, an MQ-6 gas sensor, and a few additional components. This project is not only fun to build but also practical, as it can help ensure the safety of your home or workspace by detecting harmful gas leaks.
The system we'll build will detect gas levels using the MQ-6 sensor, trigger an alarm, activate an exhaust fan, and control a servo motor that could, for example, shut off a gas valve. Additionally, an LED will indicate the gas detection status, and a buzzer will sound to alert you.
#### Components Needed
- Arduino Uno
- MQ-6 Gas Sensor
- Servo Motor (e.g., SG90)
- Buzzer
- LED (any color)
- Resistor (220 ohms for the LED)
- Exhaust fan (small DC fan)
- Breadboard and jumper wires
- Power supply (USB cable for Arduino or battery pack)
- Home light interlock (optional, could be a relay)
#### Circuit Diagram
To begin, let's wire up the components according to the following connections:
1. **MQ-6 Gas Sensor**:
- VCC to 5V on the Arduino
- GND to GND on the Arduino
- A0 (analog output) to A0 on the Arduino
2. **Servo Motor**:
- Signal pin to Digital Pin 3 on the Arduino
- VCC to 5V on the Arduino
- GND to GND on the Arduino
3. **Buzzer**:
- Positive pin to Digital Pin 10 on the Arduino
- Negative pin to GND on the Arduino
4. **LED**:
- Anode (+) to Digital Pin 12 on the Arduino (through a 220-ohm resistor)
- Cathode (-) to GND on the Arduino
5. **Exhaust Fan**:
- Positive pin to Digital Pin 9 on the Arduino
- Negative pin to GND on the Arduino
6. **Home Light Interlock** (optional):
- Signal pin to Digital Pin 8 on the Arduino
- Connect the other side as per your home light circuit setup.
Explanation of the Code
1. **Initialization**:
- The servo motor is initialized and set to the 0-degree position.
- The pins connected to the gas sensor, LED, buzzer, exhaust fan, and home light interlock are configured as inputs or outputs.
2. **Gas Sensor Reading**:
- In the `loop()` function, the system continuously reads the gas level from the MQ-6 sensor.
- If the gas level exceeds the defined threshold, the system triggers the alarm sequence.
3. **Alarm Sequence**:
- The servo motor rotates to 90 degrees, potentially closing a valve or triggering another mechanism.
- The LED turns on, the buzzer sounds, and the exhaust fan activates.
- The home light interlock pin is set low, which could, for example, turn off the lights as an additional safety measure.
4. **Debouncing and Safety**:
- The alarm sequence is only triggered once until the system is reset, preventing repeated alarms from the same gas leak incident.
#### Testing and Calibration
Before using this system in a real-world application, thoroughly test and calibrate the gas threshold level by exposing the sensor to different gas concentrations and observing the sensor values. Adjust the `gasThreshold` value in the code to a level that best suits your environment.
#### Conclusion
This gas leak detector project is a simple yet effective way to monitor gas levels and respond to leaks automatically. By following this guide, you can build a reliable system that enhances safety in your home, workshop, or any other area where gas is used.
Feel free to modify and expand upon this project. For instance, you could integrate an IoT module to send alerts to your phone or connect the system to a central alarm network. The possibilities are endless!
Comments
Post a Comment