make-blog-post_825x310

Christmas Bauble Smoke Detector Prototype

At AnalogFolk, we love building hardware prototypes, especially with the Arduino open source hardware platform. These development boards are quickly getting smaller, lighter and cheaper, so it has become possible for us to build our own smoke detecting smart Christmas bauble.

The brain of our system is the Digispark ATtiny85 microcontroller board which is currently available for only $1.22 with free shipping.

attiny85

Our bauble uses the popular MQ-2 gas sensor. Apart from smoke, this sensor module is also sensitive to carbon-monoxide, methane, propane, butane, LPG and higher concentrates of hydrogen and alcohol fumes. When the alarm goes off the module uses light and sound to warn people that their Christmas tree might be on fire.

mq2 Here’s the complete shopping list for the Christmas Bauble Smoke Alarm:

  • Digispark ATtiny85 Arduino compatible development board
  • MQ-2 gas sensor module
  • Electronic buzzer- LED
  • 2x 220Ω resistors- 1000µF capacitor
  • Wires and breadboard to prototype then a blank board and solder to finalise the module

To build the circuit follow this circuit diagram:

gas-sensor-circuit

The MQ-2 sensor has a small burner inside which needs to heat up before the readings become accurate. For this reason, we start the smoke alarm in calibration mode when it powers up. As the calibration is happening, sensor readings are slowly going down until they finally stop at the accurate value after a minute or so. The Arduino code waits until these values normalise and switches the LED on to let the user know that calibration is in progress. When the LED is off the sensor is active and fully calibrated.

protoandpcb

For writing this application logic we will be using the Arduino language and the official Arduino IDE. To get started with this environment visit: https://www.arduino.cc/en/Guide/HomePage.

The Arduino code that performs the previously outlined behaviour starts with setting up the sensor, the LED and the buzzer and defines their pin numbers:

#define led 10
#define buzzer 8
#define sensor A0

int treshold = 0;
int alarmLength = 40;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(sensor, INPUT);

  while(isCalibrating()){
    digitalWrite(led, HIGH);
  }

  treshold = analogRead(sensor) + 10;

}

After setting up the components the setup block enters a while() loop to perform and check the state of the calibration. This is done in the isCalibrating() function:

bool isCalibrating(){
  int sample1 = analogRead(sensor);
  delay(5000);
  int sample2 = analogRead(sensor);

  if(sample1 < sample2){
    return true;
  } else if(abs(sample1 - sample2) <= 2) {
    return false;
  }
}

As soon as the sensor reading is normalised the function returns false, then we exit the while loop and set the new threshold for the sensor then continue to the loop() block:

void loop() {  
  if(analogRead(sensor) > treshold){
    while(alarmLength >= 0){
      alarmLength--;
      alarmOn(100);
    }
    alarmLength = 40;
  } else {
    alarmOff();
  }
}

Here we simply check wether the current reading from the gas sensor is higher than the threshold then call the alarmOn() function otherwise reset the alarm and call alarmOff(). These alarm functions simply switch the buzzer and the LED back and forth:

void alarmOn(int delayMs){
  digitalWrite(led, HIGH);
  analogWrite(buzzer, 100);
  delay(delayMs);

  digitalWrite(led, LOW);
  analogWrite(buzzer, 0);
  delay(delayMs);
}

void alarmOff(){
  digitalWrite(led, LOW);
  analogWrite(buzzer, 0);
}

Uploading this piece of code onto your Arduino will immediately start the smoke alarm.

Consider this as a little festive DIY project but please don't use it as a life saving device! Always make sure you have a professional and tested smoke alarm in your house.