5.4.16 Project 8.2 Dangerous Gas Alarm
1. Description
When a gas sensor detects a high concentration of dangerous gas, the buzzer will sound an alarm and the display will show dangerous.
2. Component Knowledge
MQ2 Smoke Sensor: It is a gas leak monitoring device for homes and factories, which is suitable for liquefied gas, benzene, alkyl, alcohol, hydrogen as well as smoke detection. Our sensor leads to digital pin D and analog output pin A, which is connected to D as a digital sensor in this project.

3. Test Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize LCD with I2C address 0x27, 16 columns and 2 rows
LiquidCrystal_I2C mylcd(0x27, 16, 2);
#define gasPin 23 // Gas sensor input pin
#define buzPin 25 // Buzzer output pin
// State flags for LCD display updates
boolean dangerDisplayed = 1;
boolean safetyDisplayed = 1;
void setup() {
Serial.begin(9600);
// Initialize LCD
mylcd.init();
mylcd.backlight();
// Set pin modes
pinMode(buzPin, OUTPUT);
pinMode(gasPin, INPUT);
// Display initial message
mylcd.setCursor(0, 0);
mylcd.print("safety");
}
void loop() {
boolean gasVal = digitalRead(gasPin); // Read gas sensor value
Serial.println(gasVal);
if(gasVal == 0) // If dangerous gas detected
{
while(dangerDisplayed == 1) // Update display if needed
{
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("dangerous");
dangerDisplayed = 0;
safetyDisplayed = 1;
}
// Sound alarm buzzer (short pulses)
digitalWrite(buzPin, HIGH);
delay(1);
digitalWrite(buzPin, LOW);
delay(1);
}
else // No dangerous gas detected
{
digitalWrite(buzPin, LOW); // Ensure buzzer is off
while(safetyDisplayed == 1) // Update display if needed
{
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("safety");
dangerDisplayed = 1;
safetyDisplayed = 0;
}
}
}
4. Test Result
The screen displays “safety” in normal state. However, when the gas sensor detects some dangerous gases, such as carbon monoxide, at a certain concentration, the buzzer will sound an alarm and the screen displays “dangerous”.