CircuitPlanning:
Arduino UNO R3 --> MQ2 Sensor
Pins--> Vin-->Vcc
Pins--> GND --> GND
Pins--> A0 --> A0
Code:
//Coded and tested by :
// Sheekar Banejee, AI-ML-IOT Solution Engineer and Researcher
int smokeA0 = A0;
// Your threshold value
int sensorThres = 700;
void setup() {
pinMode(smokeA0, INPUT);
Serial.begin(9600); //Streaming 9600 bits of Sensor Data per Second
}
void loop() {
int analogSensor = analogRead(smokeA0);
Serial.println("Sensor Value: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
Serial.println("Alert! Smoke Detected!");
}
else
{
Serial.println("Normal...");
}
delay(3000);
}
Results:
Serialmonitor:
EXPANDED TECHNICAL DETAILS
Electrochemical Gas Detection
The MQ-2 is a versatile sensor for detecting LPG, smoke, alcohol, propane, and hydrogen.
- Heating Element Management: The sensor requires a "pre-heat" phase (typically 24 hours for first use, 60 seconds for daily use) to stabilize the internal heater before readings are accurate.
- Sensitivity Calibration: Features an onboard potentiometer to adjust the baseline trigger level. The Arduino reads the analog output voltage; higher concentrations of gas result in lower resistance and higher output voltage.
Safety Alert Logic
- Visual & Audible Warnings: When the gas concentration exceeds a safe limit, the Arduino triggers a Piezo Buzzer and a high-intensity blinking LED.
- Emergency Action: This setup is often expanded with a relay to automatically shut off gas valves or activate emergency ventilation.