Arduino-Based Gas Detection System with Servo, Siren, and Fan
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to detect gas using an MQ2 gas sensor and respond by activating a siren, a fan, and a servo motor. The circuit is controlled by an Arduino UNO microcontroller. When gas is detected, the servo motor moves to 90 degrees, and both the siren and the fan are turned on. If no gas is detected, the servo motor returns to 0 degrees, and both the siren and the fan are turned off.
Component List
MQ2 Gas Sensor
- Pins: VCC, GND, AOUT, DOUT
- Description: Detects the presence of gas in the environment.
- Purpose in Circuit: Provides a digital output signal when gas is detected.
Arduino UNO
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
- Description: Microcontroller board based on the ATmega328P.
- Purpose in Circuit: Controls the entire circuit, reads the gas sensor, and drives the servo, siren, and fan.
Micro Servo 9G
- Pins: GND, +5V, PWM
- Description: Small servo motor.
- Purpose in Circuit: Moves to indicate gas detection.
Siren
- Pins: Positive, Ground
- Description: Emits a loud sound when activated.
- Purpose in Circuit: Alerts when gas is detected.
Fan
- Pins: GND, 5V
- Description: Small fan.
- Purpose in Circuit: Activates to ventilate the area when gas is detected.
Wiring Details
MQ2 Gas Sensor
- VCC connected to 5V on Arduino UNO.
- GND connected to GND on Arduino UNO.
- DOUT connected to D8 on Arduino UNO.
Arduino UNO
- 5V connected to VCC on MQ2 Gas Sensor and +5V on Micro Servo 9G.
- GND connected to GND on MQ2 Gas Sensor, Ground on Siren, GND on Fan, and GND on Micro Servo 9G.
- D8 connected to DOUT on MQ2 Gas Sensor.
- D13 connected to Positive on Siren and 5V on Fan.
- D11 connected to PWM on Micro Servo 9G.
Micro Servo 9G
- +5V connected to 5V on Arduino UNO.
- GND connected to GND on Arduino UNO.
- PWM connected to D11 on Arduino UNO.
Siren
- Positive connected to D13 on Arduino UNO.
- Ground connected to GND on Arduino UNO.
Fan
- 5V connected to D13 on Arduino UNO.
- GND connected to GND on Arduino UNO.
Code Documentation
#include <Servo.h>
// Pin definitions
const int gasSensorPin = 8; // Digital pin connected to MQ2 DOUT
const int servoPin = 11; // PWM pin connected to servo motor
const int sirenPin = 13; // Digital pin connected to siren
const int fanPin = 13; // Digital pin connected to fan (same as siren)
Servo myServo; // Create a servo object
void setup() {
// Initialize the gas sensor pin as an input
pinMode(gasSensorPin, INPUT);
// Initialize the siren and fan pins as outputs
pinMode(sirenPin, OUTPUT);
pinMode(fanPin, OUTPUT);
// Attach the servo object to the servo pin
myServo.attach(servoPin);
// Initialize the servo to 0 degrees
myServo.write(0);
// Ensure siren and fan are off initially
digitalWrite(sirenPin, LOW);
digitalWrite(fanPin, LOW);
}
void loop() {
// Read the gas sensor value
int gasSensorValue = digitalRead(gasSensorPin);
// If gas is detected, move the servo to 90 degrees and turn on siren and fan
if (gasSensorValue == HIGH) {
myServo.write(90);
digitalWrite(sirenPin, HIGH);
digitalWrite(fanPin, HIGH);
} else {
// Otherwise, move the servo back to 0 degrees and turn off siren and fan
myServo.write(0);
digitalWrite(sirenPin, LOW);
digitalWrite(fanPin, LOW);
}
// Small delay to avoid rapid switching
delay(100);
}
This code initializes the gas sensor, servo motor, siren, and fan. In the loop function, it continuously checks the gas sensor's digital output. If gas is detected, the servo motor moves to 90 degrees, and both the siren and the fan are turned on. If no gas is detected, the servo motor returns to 0 degrees, and both the siren and the fan are turned off.