The “Gas Detection – Alarm and Ventilation” project uses an Arduino and an MQ2 gas sensor to detect harmful gases around us. When gas is detected, a servo motor opens a door automatically, and a fan turns on to push the air out of the room. This project is a great and enjoyable way to learn about electronics and how sensors can be used in different applications.
Components used in this project are:
- Arduino UNO with cable
- Servo Motor SG-90
- MQ2 gas sensor
- 12 V DC fan
- Red LED
- Male to Male Jumper Wires x 15
- Male to Female Jumper Wires x 3
- Resistors (10k ohm and 220 ohm)
- N-Channel MOSFET 60V 30A
Featured Image:
Connection Pins with Arduino:
- Servo (Red -> 5V, Black ->GND , Orange -> 2)
- MQ2 (VCC -> 5V, GND ->GND, A0 -> A3)
- DC fan (Red -> Vin, Black -> Mosfet pin2)
- Mosfet (1st pin -> 10k resistor, 2nd pin -> DC fan black wire, 3rd pin -> GND)
- LED ( Anode -> 6, Cathode -> GND)
Circuit Diagram:
Code:
#include <Servo.h>
Servo myservo;
int pos = 0;
#define ledR 6
#define fan 5
#define sensor A3
#define servoPin 2
void setup() {
Serial.begin(9600);
myservo.attach(servoPin);
pinMode(fan, OUTPUT);
pinMode(ledR, OUTPUT);
digitalWrite(sensor, LOW);
myservo.write(0); // initially setting the servo posstion to 0, you can set according to your requirement.
}
void loop() {
int value = analogRead(sensor);
if (value > 380) { //380 is the threshold, change according to your sensor
digitalWrite(ledR, HIGH);
digitalWrite(fan, HIGH);
myservo.write(90);
Serial.println(value);
delay(500);
}
else {
digitalWrite(fan, LOW);
digitalWrite(ledR, LOW);
myservo.write(0);
Serial.println(value);
delay(500);
}
}