Ensuring air quality and safety is essential, especially in enclosed spaces where gas buildup can occur. In this project, we'll create an automatic gas detection and ventilation system using Arduino, an MQ2 gas sensor, and a 12V fan controlled via an IRF540N MOSFET. This setup can detect gases like methane, propane, and smoke, automatically activating a fan to ventilate the area if gas levels exceed a certain threshold. This guide is perfect for applications in garages, workshops, or even basements, where monitoring air quality is a priority.
Project Overview
The system works by reading the gas concentration from the MQ2 gas sensor and using the Arduino to monitor if levels go beyond a set threshold. When gas levels rise, the Arduino triggers the fan via an IRF540N MOSFET, providing a quick and efficient way to ventilate the space. For a deeper understanding of this process, you can refer to our article on the MQ2 Gas Sensor with Arduino and LCD for additional setup insights.
Components Needed
- Arduino Uno
- MQ2 Gas Sensor
- 12V Fan (or similar DC fan)
- IRF540N MOSFET (to control the fan)
- 10kΩ resistor (for MOSFET gate pull-down)
- Breadboard and jumper wires
- 12V power supply (for the fan)
How the System Works
- The MQ2 sensor detects gas concentrations and outputs an analog signal relative to the concentration.
- The Arduino reads this analog value and checks it against a threshold.
- If the gas concentration exceeds the threshold, the fan is activated through the IRF540N MOSFET. The fan remains on until gas levels drop back below the threshold.
Wiring Diagram
For controlling the fan, we're using an IRF540N MOSFET instead of a relay module, which makes the circuit more efficient and can handle higher currents easily. If you’re new to working with MOSFETs in Arduino projects, you can check out our Ultimate Guide to IRF540N for Arduino Users to understand how to wire and control it.
- MQ2 Gas Sensor: Connect the sensor's VCC to 5V, GND to ground, and A0 to A0 on the Arduino.
- Fan Control with IRF540N MOSFET:
- Connect the drain of the IRF540N to the negative terminal of the fan.
- Connect the source to ground.
- Use a 10kΩ resistor between the gate and source to keep the MOSFET off when no signal is applied.
- Connect the gate to Pin 9 on the Arduino.
- Fan Power: The positive terminal of the fan connects to the 12V supply.
The circuit diagram of the Automatic Gas Detection and Ventilation System with Arduino is shown below.
Code
Here’s the Arduino code that reads the gas sensor and controls the fan:
const int gasSensorPin = A0; // MQ2 analog output pin
const int fanPin = 9; // MOSFET gate pin controlling the fan
int gasThreshold = 400; // Set gas level threshold (adjust as needed)
void setup() {
pinMode(fanPin, OUTPUT);
Serial.begin(9600);
Serial.println("Gas Detection and Ventilation System Initialized");
}
void loop() {
int gasLevel = analogRead(gasSensorPin); // Read gas level from MQ2 sensor
Serial.print("Gas Level: ");
Serial.println(gasLevel);
if (gasLevel > gasThreshold) {
digitalWrite(fanPin, HIGH); // Turn on fan
Serial.println("Gas detected! Turning on the fan.");
} else {
digitalWrite(fanPin, LOW); // Turn off fan
Serial.println("Gas level safe. Fan is off.");
}
delay(1000); // Wait 1 second before the next reading
}
Code Explanation
- The gas sensor's analog output is read by the Arduino on
gasSensorPin
. If the value surpasses the setgasThreshold
, the fan is activated via the IRF540N. - The IRF540N MOSFET makes it possible to control the fan with a lower voltage signal from the Arduino, even though the fan itself is powered by a 12V supply. This is an efficient approach, especially for DC motor control, as explored in our Simplest DC Motor Controller tutorial.
Testing and Calibration
- Adjust the Threshold: Start with a threshold of 400, or adjust it based on your testing environment. To simulate gas, you can use smoke from a lighter.
- Monitor Gas Readings: Use the Serial Monitor to observe gas readings and confirm that the fan activates when the threshold is reached.
Real-World Applications
This gas detection and ventilation control system has a wide range of applications in home and industrial settings, such as:
- Garage or Workshop Ventilation: Detect fumes or combustible gases and ventilate the area automatically, improving air quality and safety.
- Kitchen Safety: Place in proximity to gas stoves to monitor for leaks and activate ventilation if necessary.
- Indoor Air Purification: Automatically activate air circulation systems in enclosed spaces when gas concentrations rise.
This project also makes use of a PID Controller approach, commonly used in temperature control projects. To see how Arduino can be used as a PID controller, check out our Ultimate Guide to Using Arduino as a PID Controller and our article on Building a PID-Controlled Temperature System.
Video Demonstration
See how the automatic gas detection and ventilation control system with Arduino, MQ2 Sensor, and IRF540N MOSFET works.
Conclusion
With just a few components, you can build an effective gas detection and ventilation system. By integrating an MQ2 sensor and IRF540N MOSFET, this setup provides real-time response to changes in gas levels, ensuring safety in any workspace or home environment.
For more on PID-based control, you may also enjoy our tutorial on the Arduino PID Controller for Temperature Control, which dives into managing real-time variables using Arduino. This knowledge will be invaluable in creating automated systems for home, lab, or garage environments.