Anth's Computer Cave

Use a smoke and gas sensor with Arduino

Today we are using a smoke and gas sensor to allow the AAIMI home-automation system to monitor air-quality.

A smoke and gas sensor and stand made from salvaged parts. Picture: Anthony Hartup
A prototype safety sensor for the AAIMI Home Automation project. Picture: Anthony Hartup.

The sensor

A smoke and gas sensor. Picture: Anthony Hartup

I am using a three-dollar sensor from this ebay seller. They can detect smoke, alcohol and petroleum, as well as a variety of gases including methane, carbon monoxide, hydrogen and propane.

In this article I am only covering general use of the sensor, because I am still working on the more advanced options. The methods I cover today will reliably detect all the dangers listed above, but they will not differentiate between them. Many of these sensors feature a built-in variable resistor which I think is part of the method used to detect specific threats. My unit has no adjustment so I am using it like a traditional analog sensor for now.

Even with that limitation these are amazing devices. Regardless of which particular gas or toxin this sensor detects, a high-enough reading means there is something drastically wrong in your room that requires immediate action.

In a future article we'll refine methods to differentiate between the dangers. This will allow us to code different responses for different hazards, such as turning electrical appliances off during a gas leak or turning sprinklers on in case of smoke.

How it works

From my understanding these devices use a tiny heating coil to sample air. Their resistance is high in clean air and falls as it detects danger. This sends a higher voltage back to the Arduino.

The first time you run the sensor it will take around thirty minutes to burn-in. During that time the return voltage to the Arduino could be up around 3.3V, which in normal operation would represent a dangerously-high reading. By the time the unit is burnt-in the voltage should be down to between .2V and .3V, and it should stay at that level in clean air.

I have found that a heater and oven both operating near the sensor (say four metres away) can lift the reading to about .6V.

It is when you tease the sensor with a little smoke or gas that you can grasp the sensitivity of these devices. A one-second exposure to a cigarette lighter with gas running but no flame sends the voltage level up around 3V instantly. Holding a cigarette nearby has the same effect. A person applying hair spray on the other side of the room lifted the reading to almost 1.4V

I have devised some initial trigger-points based on these observations.

My system will consider any reading below .8V as safe. A reading between .8V and 1.4V will trigger a "watch-event" where the system will register that status and increase the polling frequency for that sensor. For example, if the system normally checks the sensor every five-seconds it could reduce that time to one second until the reading drops. This puts it in a better position to react if the level keep rising.

Any reading above 1.4V and below 1.6V will trigger an "urgent-watch-event". The system will sound a mild audible alert, stop non-essential tasks and divert all other resources to monitor the affected area.

A reading above 1.6V will prompt a "hay-wire event". This will trigger a full-blown alarm and activate any safety equipment linked to the system.

I may need to refine these levels over time, but I think it is a good starting point.


Let's connect the sensor to the Arduino.

The connection diagram for a smoke and gas sensor and an Arduino. Picture: Anthony Hartup

Just like a normal analog sensor, these units have a GND, VCC and signal wire. GND and VCC connect to GND and 5V on the Arduino. Signal connects to an analog Arduino pin, in this case A0.

Judging by how warm these units run I suspect they could draw more power than your average analog sensors. If you already have other devices using 5V power from your Arduino you may need to consider a separate 5V power supply. So far I have ran it alongside a couple of temperature and light-sensors without issues.

The Arduino sketch

The Arduino sketch below will monitor the air-quality levels and set air status accordingly, printing warnings if required.

You can add your own actions within the check_smoke() function (audible alerts, warning lights, etc) to react to this status

The first time you run the sketch it will start in burn-in mode, and merely display the smoke level every five seconds. Once the voltage levels drop to around .3V you can change the burnInStatus variable at the top of the sketch from 1 to 0. Once you re-upload the sketch it will start in normal mode thereafter.

// Smoke and gas sensor
// By Anthony Hartup
// You are welcome to use, modify and share this code.
// This sketch checks for general hazards. It does not differentiate between specific hazards,
//  but a high reading means genuine danger

//Default AD pins for signal wire
int smokePin = A0;

// define five-second pause between sensor-checks
int smokeFrequency = 5000;

// Allow to burn in new sensor on first use without triggering alerts
// 1 = "new", 0 = "burnt-in"
// Leave set to 1 until brand new sensor has burnt-in (usually about 30 minutes)
// Then set permenantly to 0
int burnInStatus = 1;

// Set air status: 0 = "good", 1 = "mild alert", 2 = "medium alert" and 3 = "hay-wire event"
int airStatus = 0;

// Store refined sensor reading
float smokeLevel = 0.0; 

void setup() {
  // Start serial connection and wait for confirmation
  while (!Serial) {
  Serial.println("Serial Connected");

// Function to monitor readings during initial burn-in of new sensor
void burn_in_sensor()
  int rawSmoke = analogRead(smokePin);
  smokeLevel = rawSmoke * (5.0 / 1023.0);

// Check the smoke sensor
void check_smoke() {
  int rawSmoke = analogRead(smokePin);
  smokeLevel = rawSmoke * (5.0 / 1023.0);
  if (smokeLevel > .8 && smokeLevel <= 1.4) // Mild alert
    airStatus = 1;
    smokeFrequency = 1000;
    Serial.println("System is in Watch Mode.");
  else if (smokeLevel > 1.4 && smokeLevel <= 1.6) // Medium alert
    airStatus = 2;
    smokeFrequency = 500;
    Serial.println("System is in Urgent Watch Mode. Investigate now!");
  else if (smokeLevel > 1.6) // Serious problem with air.
    airStatus = 3;
    Serial.println("Danger, Danger, Danger!");
    smokeFrequency = 500;
    // Add your actions here
  else // Air is good
    airStatus = 0;
    smokeFrequency = 5000;    

void loop()
  if (burnInStatus == 1) // monitor without alerts
  else // Full system operation with alerts

So that is all there is to it for now. I'll eventually write a second article with the methods to differentiate between the various types of gas.

I will also have some more-complex programming examples in the up-coming release of AAIMI Home Automation 0.2.



Previous: Temperature Sensor

Next: Panning security camera



Leave a comment on this article

Leave a comment on this article