← all projects
2025-05-27
  • Arduino
  • MQ-2
  • Safety
  • Sensors
  • Beginner

DIY Smoke Detector for $4

Build a working smoke detector with an Arduino, MQ-2 sensor, two LEDs and a buzzer. Green means safe. Red means get out.

DIY Smoke Detector for $4

What we’re building

A smoke detector that actually works — green LED when the air is clean, red LED and buzzer alarm when it detects smoke or gas. Total cost: under $4 in components.

Components

PartQty
Arduino Uno1
MQ-2 Gas Sensor1
Buzzer1
LED (green)1
LED (red)1
220Ω Resistor2
Breadboard1
Jumper wires~10

Wiring

FromTo
MQ-2 VCCArduino 5V
MQ-2 GNDArduino GND
MQ-2 AOArduino A0
MQ-2 DOArduino D7
Green LED + 220ΩArduino D9 → GND
Red LED + 220ΩArduino D10 → GND
BuzzerArduino D11

Code

const int MQ2_AO     = A0;
const int MQ2_DO     = 7;
const int LED_GREEN  = 9;
const int LED_RED    = 10;
const int BUZZER     = 11;


const int SMOKE_THRESHOLD = 400;
const int BUZZER_FREQ     = 2000;
const int READ_INTERVAL   = 500;


bool alarmActive = false;


void setup() {
  Serial.begin(9600);

  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED,   OUTPUT);
  pinMode(BUZZER,    OUTPUT);
  pinMode(MQ2_DO,    INPUT);

  Serial.println("=============================");
  Serial.println("  threeboardslab Smoke Detector v1");
  Serial.println("  Warming up MQ-2 sensor...");
  Serial.println("=============================");

  for (int i = 20; i > 0; i--) {
    digitalWrite(LED_GREEN, HIGH);
    delay(500);
    digitalWrite(LED_GREEN, LOW);
    delay(500);
    Serial.print("Warm-up: ");
    Serial.print(i);
    Serial.println("s remaining...");
  }

  Serial.println("Ready! Monitoring...\n");
  digitalWrite(LED_GREEN, HIGH);
}


void loop() {
  int smokeLevel = analogRead(MQ2_AO);
  int doState    = digitalRead(MQ2_DO);


  Serial.print("Smoke Level: ");
  Serial.print(smokeLevel);
  Serial.print(" / 1023  |  Digital: ");
  Serial.print(doState == LOW ? "TRIGGERED" : "clear");
  Serial.print("  |  Status: ");

  if (smokeLevel > SMOKE_THRESHOLD || doState == LOW) {

    Serial.println("*** SMOKE DETECTED! ***");

    digitalWrite(LED_GREEN, LOW);
    digitalWrite(LED_RED,   HIGH);


    tone(BUZZER, BUZZER_FREQ);
    delay(200);
    noTone(BUZZER);
    delay(100);
    tone(BUZZER, BUZZER_FREQ - 400);
    delay(200);
    noTone(BUZZER);
    delay(100);

    alarmActive = true;

  } else {

    Serial.println("OK");

    if (alarmActive) {
      noTone(BUZZER);
      digitalWrite(LED_RED, LOW);
      alarmActive = false;
    }

    digitalWrite(LED_GREEN, HIGH);
    delay(READ_INTERVAL);
  }
}
View repo on GitHub →