Index
Introduction
In this project, we create a gas monitoring system using an MQ-2 gas sensor, OLED display, relay, and fan, controlled by an Arduino. The system detects gas levels and activates a fan if the gas concentration exceeds a threshold, providing a visual and functional response to potential gas leaks.
Materials Needed
- Breadboard and Jumper Wires: For connections
- Power Supply: USB or battery
Circuit Diagram
MQ-2 Gas Sensor Connections:
- VCC to 5V
- GND to GND
- A0 to A0 (analog pin)
OLED Display Connections:
- VCC to 3.3V or 5V
- GND to GND
- SDA to A4
- SCL to A5
Relay Module Connections:
- VCC to 5V
- GND to GND
- IN to digital pin 7
Fan Connections:
- Connect the fan’s power terminals to the relay’s NO (Normally Open) and COM (Common) terminals
Code
Install the necessary libraries via the Arduino Library Manager:
- Adafruit GFX Library
- Adafruit SSD1306 Library
The following code reads the gas concentration from the MQ-2 sensor and displays it on the OLED screen. If the gas concentration exceeds a specified threshold, the relay activates the fan.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define RELAY_PIN 7
#define GAS_SENSOR_PIN A0
#define GAS_THRESHOLD 300 // Define the threshold value
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
while(true);
}
display.clearDisplay();
display.display();
}
void loop() {
int gasValue = analogRead(GAS_SENSOR_PIN);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Gas Level: ");
display.println(gasValue);
display.display();
if(gasValue > GAS_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH);
display.setCursor(0, 20);
display.print("Fan ON");
display.display();
} else {
digitalWrite(RELAY_PIN, LOW);
display.setCursor(0, 20);
display.print("Fan OFF");
display.display();
}
delay(1000); // Adjust delay as needed
}
NOTE :
This project provides a practical demonstration of how to monitor gas levels and control devices like a fan based on sensor data using Arduino. It’s a great way to learn about integrating sensors, displays, and actuators in an embedded system. You can expand this project by adding more sensors, alarms, or connectivity features for remote monitoring.
Additional Resources
For more information about the components used in this project, check out the following links:
- Arduino Uno
- 128×64 I2C OLED Display
- Relay Module
- MQ2 Gas Sensor