We’ve learned how to measure the temperature, humidity, and pressure of the air. But what about the actual contents of the air?
In this lesson, we will introduce the MQ-2 Gas Sensor, a module capable of detecting combustible gasses like LPG, Propane, Methane, and—most importantly for home projects—Smoke.

The MQ-2 sensor features a protective metal mesh cage over an internal heater element.
If you look closely at the MQ-2 sensor, you’ll notice it gets warm while running. Inside that silver mesh cage is a tiny built-in heater. The heater warms up a chemical sensor (made of tin dioxide). When clean air hits the sensor, its electrical resistance remains high.
However, when combustible gasses or smoke come into contact with the heated element, the chemical reaction causes the resistance to drop dramatically. By measuring this change in resistance via an Analog pin, the Arduino can determine the concentration of gas in the air!
Need the parts? You can find an MQ-2 Gas Sensor Module on Amazon. They are inexpensive and perfect for air quality experiments.
Most MQ-2 breakout boards have four pins. We will be using the Analog output (A0) to get a continuous reading of gas concentration, rather than a simple digital “yes/no”.
Note: The sensor needs to “burn in” for about 24 hours of continuous power to achieve maximum accuracy, but it will work well enough for a basic smoke alarm straight out of the box after heating up for a few minutes.
Because we are using the Analog pin, the code is very similar to reading a potentiometer or the water level sensor from Lesson 148. We will use analogRead().
// Define the analog pin connected to the MQ-2
int gasPin = A0;
// Set a threshold for when the alarm should trigger
int alarmThreshold = 400;
void setup() {
Serial.begin(9600);
}
void loop() {
// Read the raw analog value (0 to 1023)
int gasLevel = analogRead(gasPin);
Serial.print("Gas Concentration Level: ");
Serial.print(gasLevel);
// Check if the level exceeds our safe threshold
if (gasLevel > alarmThreshold) {
Serial.println(" *** ALARM! SMOKE OR GAS DETECTED! ***");
// You could trigger a piezo buzzer or relay here
} else {
Serial.println(" (Air is clear)");
}
delay(1000); // Read once per second
}
Once uploaded, let the sensor sit for a few minutes to heat up (the readings might be high initially). Once the values stabilize, try blowing out a match or a candle near the sensor. You will see the analog value spike instantly!
Combine this with the Piezo Buzzer from Lesson 124, and you’ve just built a fully functional electronic smoke detector!