https://github.com/arkadip2007/gas-sensor-iot
gas-sensor-iot
https://github.com/arkadip2007/gas-sensor-iot
gas-sensor-iot iot
Last synced: about 1 year ago
JSON representation
gas-sensor-iot
- Host: GitHub
- URL: https://github.com/arkadip2007/gas-sensor-iot
- Owner: Arkadip2007
- Created: 2025-04-01T07:07:59.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-01T11:07:06.000Z (over 1 year ago)
- Last Synced: 2025-05-15T18:15:44.892Z (about 1 year ago)
- Topics: gas-sensor-iot, iot
- Language: C++
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gas-sensor : **MQ-5**, which is used to detect **Natural Gas, LPG, Coal Gas, and Methane (CH4)**.
---
### **π οΈ Components Required:**
- **MQ-5 Gas Sensor**
- **Arduino Uno** (or NANO)
- **Jumper Wires**
- **Power Supply (5V from Arduino)**
---
### **π Wiring Connections:**
| **MQ-5 Sensor Pin** | **Arduino Pin** |
|--------------------|---------------|
| **VCC** | **5V** |
| **GND** | **GND** |
| **A0 (Analog Output)** | **A0** |
| **D0 (Digital Output, Optional)** | **D7** (if needed) |
---
### Code -->
```cpp
int gasSensorPin = A0; // Analog pin connected to MQ-5
int digitalPin = 7; // Digital pin (optional)
int sensorValue;
int threshold = 400; // Set a reasonable threshold
void setup() {
Serial.begin(9600);
pinMode(digitalPin, INPUT);
Serial.println("MQ-5 Gas Sensor Test");
Serial.println("Warming up... Please wait 2-3 minutes.");
delay(3000); // Allow sensor to stabilize (increase to 2-3 min for real use)
}
void loop() {
sensorValue = analogRead(gasSensorPin);
Serial.print("Gas Level (Analog): ");
Serial.println(sensorValue);
if (sensorValue > threshold) { // Use a stable threshold
Serial.println("π¨ Gas detected!");
} else {
Serial.println("β
No gas detected.");
}
delay(2000); // Increase delay for more stable readings
}
```
[
](https://www.linkedin.com/posts/arkadip2008_gas-detection-system-activity-7312738420567535616--Y2n)
### Output & Video
```go
β
No gas detected.
Gas Level (Analog): 117
β
No gas detected.
Gas Level (Analog): 247
β
No gas detected.
Gas Level (Analog): 738
π¨ Gas detected!
Gas Level (Analog): 762
π¨ Gas detected!
Gas Level (Analog): 687
π¨ Gas detected!
Gas Level (Analog): 132
β
No gas detected.
Gas Level (Analog): 104
β
No gas detected.
Gas Level (Analog): 88
β
No gas detected.
Gas Level (Analog): 57
β
No gas detected.
Gas Level (Analog): 47
β
No gas detected.
Gas Level (Analog): 50
β
No gas detected.
Gas Level (Analog): 50
β
No gas detected.
Gas Level (Analog): 569
π¨ Gas detected!
Gas Level (Analog): 142
β
No gas detected.
Gas Level (Analog): 61
```
---
# add Display π
---
β
π LED Display Check


## Check the Display working or Not?
```cpp
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include
#include
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Hello, world!");
lcd.setCursor(2,1);
lcd.print("I am ARKADIP");
lcd.setCursor(0,2);
lcd.print("MAHAPATRA");
lcd.setCursor(2,3);
lcd.print("04 MARCH, 2025");
}
void loop()
{
}
```
| π [More ways to Text in LCD display](https://github.com/Arkadip2007/LCD-iot/blob/main/README.md) |
| --- |
---
Add a **JHD162A (16x2 LCD) with an I2C module** to display **live gas levels** from MQ-5 Gass sensor.
---
### **π οΈ Components Required:**
- **Arduino Uno/Nano**
- **MQ-5 Gas Sensor**
- **JHD162A (16x2 LCD) with I2C Module**
- **Jumper Wires**
---
### **π Connections:**
| **Component** | **Arduino Pin** |
|-------------|---------------|
| **MQ-5 VCC** | **5V** |
| **MQ-5 GND** | **GND** |
| **MQ-5 A0** | **A0** |
| **I2C LCD VCC** | **5V** |
| **I2C LCD GND** | **GND** |
| **I2C LCD SDA** | **A4 (SDA)** |
| **I2C LCD SCL** | **A5 (SCL)** |
---
### **π Arduino Code with I2C LCD Display**
## β Code 01 with LCD
```cpp
#include
#include
// LCD setup (default I2C address 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
int gasSensorPin = A0; // MQ-5 Analog Output
int threshold = 400; // Gas detection threshold
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on LCD backlight
Serial.println("MQ-5 Gas Sensor Test");
lcd.setCursor(0, 0);
lcd.print("MQ-5 Sensor Test");
lcd.setCursor(0, 1);
lcd.print("Warming up...");
delay(3000); // Warm-up time
}
void loop() {
int sensorValue = analogRead(gasSensorPin);
// Print values to Serial Monitor
Serial.print("Gas Level: ");
Serial.println(sensorValue);
// Display gas level on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Level: ");
lcd.print(sensorValue);
// Gas detection message
lcd.setCursor(0, 1);
if (sensorValue > threshold) {
Serial.println("π¨ Gas Detected!");
lcd.print("π¨ Gas Detected!");
} else {
Serial.println("β
Safe");
lcd.print("β
Safe");
}
delay(2000); // Delay for stability
}
```
---
### **π οΈ How It Works ?**
- The **gas level is displayed live** on the LCD screen.
- If gas exceeds the **threshold (400)**, it shows **"π¨ Gas Detected!"**.
- If the environment is **safe**, it shows **"β
Safe"**.
- The Serial Monitor also prints the same information.
---
### **π LCD Display Output:**
#### **Normal Air (Safe Condition)**
```
Gas Level: 180
β
Safe
```
#### **When Gas is Detected (Lighter near sensor)**
```
Gas Level: 650
π¨ Gas Detected!
```
---
### **πΉ Points to know:**
- If the LCD **doesnβt display** anything, try changing `0x27` to `0x3F` in `LiquidCrystal_I2C lcd(0x27, 16, 2);`.
- The **threshold (`400`)** can be adjusted based on your sensor's response.
- Always give the sensor **2-3 minutes of warm-up** for accurate readings.
---
# β code 02 with LCD
```cpp
#include
#include
// Initialize LCD with I2C address (Change 0x27 to 0x3F if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);
int gasSensorPin = A0; // MQ-5 Analog Output
int threshold = 400; // Gas detection threshold
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on LCD backlight
Serial.println("MQ-5 Gas Sensor Test");
lcd.setCursor(0, 0);
lcd.print("MQ-5 Sensor Test");
delay(5000); // Warm-up time for MQ-5 sensor
lcd.clear();
}
void loop() {
int sensorValue = analogRead(gasSensorPin); // Read the value from the gas sensor
// Print values to Serial Monitor for debugging
Serial.print("Gas Level: ");
Serial.println(sensorValue);
// Display gas status on LCD
lcd.clear(); // Clear the LCD screen before printing new data
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Gas Status:"); // Display static text
lcd.setCursor(0, 1); // Move cursor to the first column of the second row
if (sensorValue > threshold) { // Check if gas level exceeds the threshold
Serial.println("π¨ GAS DETECTED!");
lcd.print("π¨ GAS DETECTED!"); // Display gas detected message
} else {
Serial.println("β
SAFE");
lcd.print("β
SAFE"); // Display safe message
}
delay(2000); // Delay for stability before taking another reading
}
```