Smoke and Gas Detector
A device which uses the gas sensor to tell if there is a gas leakage in your house or not
Components and supplies
1
LED (generic)
1
Gas Detection Sensor, Flammable Gas
1
Arduino UNO
1
Buzzer
1
Alphanumeric LCD, 16 x 2
Apps and platforms
1
Arduino IDE
Project description
Code
The code
c_cpp
upload on Arduino ide
1#include <LiquidCrystal.h> 2 3//System state 4const char onMessage[] = "Alarm on"; 5const char offMessage[] = "Alarm off"; 6const char activeMessage[][16] = {"Gas Detected", "Please evacuate","Fire Detected"}; 7const int OFF=0, ON=1, ACTIVE=2; 8int state = OFF; 9 10//Pins 11const int tempSensor = A2; 12const int gasSensor = A3; 13const int led = 5; 14const int piezo = 3; 15const int power = 2; 16 17const int rs = 8, en = 9, d4=10, d5=11, d6=12, d7=13; 18LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 19 20int temperature; 21int gas; 22char degree = 176; //ASCII value of Degree 23unsigned long time; 24 25void setup() 26 { 27 Serial.begin(9600); // 28 delay(3000); // 29 Serial.println("Start"); // 30 31 pinMode(led, OUTPUT); 32 pinMode(piezo, OUTPUT); 33 34 pinMode(A3,INPUT); // 35 pinMode(A2,INPUT); // 36 37 lcd.begin(16, 2); 38 setOff(); 39 } 40 41void loop() 42{ 43 int on = digitalRead(power); 44 int Gas_state = analogRead(A3); // 45 46 int tmp = analogRead(A0);//Reading data from the sensor.This voltage is stored as a 10bit number. 47 float voltage = (tmp * 5.0)/1024;//(5*temp)/1024 is to convert the 10 bit number to a voltage reading. 48 float milliVolt = voltage * 1000;//This is multiplied by 1000 to convert it to millivolt. 49 float tmpCel = (milliVolt-500)/10 ;//For TMP36 sensor. Range(−40°C to +125°C) 50 float tmpFer = (((tmpCel*9)/5)+32);//used to convert Celsius -> Fahrenheit 51 52 if(on && (state == OFF)){ 53 setOn(); 54 Serial.println("Gas Leaked at your premises"); 55 }else if(!on && (state != OFF)){ 56 setOff(); 57 } 58 59if(state == ON) { 60 readSensors(); 61 if(gas > 60) { 62 setActive(); 63 }else if(temperature > 100){ 64 setActive1(); 65 } 66 67}else if(state == ACTIVE) { 68 int ledState = digitalRead(led); 69 digitalWrite(led, !ledState); 70 if(ledState == HIGH) { 71 delay(50); 72 }else { 73 delay(20); 74 } 75 } 76 77 time = millis(); 78 Serial.print("Time: "); 79 Serial.println(time); 80 81 /*Serial.print("10bit number(0-1023): "); 82 Serial.println(tmp); 83 84 Serial.print("voltage: "); 85 Serial.print(voltage); 86 Serial.println("V"); 87 88 Serial.print("millivolt: "); 89 Serial.print(milliVolt); 90 Serial.println("mV");*/ 91 92 Serial.print("Celsius: "); 93 Serial.print(tmpCel); 94 Serial.println(degree); 95 96 Serial.print("Fahrenheit: "); 97 Serial.println(tmpFer); 98 Serial.println(""); 99 100 delay(1000); 101} 102 103void setOff() { 104 state = OFF; 105 digitalWrite(led, LOW); 106 noTone(piezo); 107 lcd.clear(); 108 lcd.setCursor(4, 0); 109 lcd.print(offMessage); 110} 111 112void setOn() { 113 state = ON; 114 digitalWrite(led, HIGH); 115 lcd.clear(); 116 lcd.setCursor(4, 0); 117 lcd.print(onMessage); 118} 119 120void setActive(){ 121 state = ACTIVE; 122 tone(piezo, 750); 123 lcd.clear(); 124 lcd.print(activeMessage[0]); 125 lcd.setCursor(0, 1); 126 lcd.print(activeMessage[1]); 127} 128 129void setActive1(){ 130 state = ACTIVE; 131 tone(piezo, 750); 132 lcd.clear(); 133 lcd.print(activeMessage[2]); 134 lcd.setCursor(0, 1); 135 lcd.print(activeMessage[1]); 136} 137 138void readSensors(){ 139 int t = analogRead(tempSensor); 140 temperature = map(t, 20, 358, -40, 125); 141 int q = analogRead(gasSensor); 142 gas = map(q, 157, 600, 0, 100); 143 }
The code
c_cpp
upload on Arduino ide
1#include <LiquidCrystal.h> 2 3//System state 4const char onMessage[] = "Alarm on"; 5const char offMessage[] = "Alarm off"; 6const char activeMessage[][16] = {"Gas Detected", "Please evacuate","Fire Detected"}; 7const int OFF=0, ON=1, ACTIVE=2; 8int state = OFF; 9 10//Pins 11const int tempSensor = A2; 12const int gasSensor = A3; 13const int led = 5; 14const int piezo = 3; 15const int power = 2; 16 17const int rs = 8, en = 9, d4=10, d5=11, d6=12, d7=13; 18LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 19 20int temperature; 21int gas; 22char degree = 176; //ASCII value of Degree 23unsigned long time; 24 25void setup() 26 { 27 Serial.begin(9600); // 28 delay(3000); // 29 Serial.println("Start"); // 30 31 pinMode(led, OUTPUT); 32 pinMode(piezo, OUTPUT); 33 34 pinMode(A3,INPUT); // 35 pinMode(A2,INPUT); // 36 37 lcd.begin(16, 2); 38 setOff(); 39 } 40 41void loop() 42{ 43 int on = digitalRead(power); 44 int Gas_state = analogRead(A3); // 45 46 int tmp = analogRead(A0);//Reading data from the sensor.This voltage is stored as a 10bit number. 47 float voltage = (tmp * 5.0)/1024;//(5*temp)/1024 is to convert the 10 bit number to a voltage reading. 48 float milliVolt = voltage * 1000;//This is multiplied by 1000 to convert it to millivolt. 49 float tmpCel = (milliVolt-500)/10 ;//For TMP36 sensor. Range(−40°C to +125°C) 50 float tmpFer = (((tmpCel*9)/5)+32);//used to convert Celsius -> Fahrenheit 51 52 if(on && (state == OFF)){ 53 setOn(); 54 Serial.println("Gas Leaked at your premises"); 55 }else if(!on && (state != OFF)){ 56 setOff(); 57 } 58 59if(state == ON) { 60 readSensors(); 61 if(gas > 60) { 62 setActive(); 63 }else if(temperature > 100){ 64 setActive1(); 65 } 66 67}else if(state == ACTIVE) { 68 int ledState = digitalRead(led); 69 digitalWrite(led, !ledState); 70 if(ledState == HIGH) { 71 delay(50); 72 }else { 73 delay(20); 74 } 75 } 76 77 time = millis(); 78 Serial.print("Time: "); 79 Serial.println(time); 80 81 /*Serial.print("10bit number(0-1023): "); 82 Serial.println(tmp); 83 84 Serial.print("voltage: "); 85 Serial.print(voltage); 86 Serial.println("V"); 87 88 Serial.print("millivolt: "); 89 Serial.print(milliVolt); 90 Serial.println("mV");*/ 91 92 Serial.print("Celsius: "); 93 Serial.print(tmpCel); 94 Serial.println(degree); 95 96 Serial.print("Fahrenheit: "); 97 Serial.println(tmpFer); 98 Serial.println(""); 99 100 delay(1000); 101} 102 103void setOff() { 104 state = OFF; 105 digitalWrite(led, LOW); 106 noTone(piezo); 107 lcd.clear(); 108 lcd.setCursor(4, 0); 109 lcd.print(offMessage); 110} 111 112void setOn() { 113 state = ON; 114 digitalWrite(led, HIGH); 115 lcd.clear(); 116 lcd.setCursor(4, 0); 117 lcd.print(onMessage); 118} 119 120void setActive(){ 121 state = ACTIVE; 122 tone(piezo, 750); 123 lcd.clear(); 124 lcd.print(activeMessage[0]); 125 lcd.setCursor(0, 1); 126 lcd.print(activeMessage[1]); 127} 128 129void setActive1(){ 130 state = ACTIVE; 131 tone(piezo, 750); 132 lcd.clear(); 133 lcd.print(activeMessage[2]); 134 lcd.setCursor(0, 1); 135 lcd.print(activeMessage[1]); 136} 137 138void readSensors(){ 139 int t = analogRead(tempSensor); 140 temperature = map(t, 20, 358, -40, 125); 141 int q = analogRead(gasSensor); 142 gas = map(q, 157, 600, 0, 100); 143 }
Downloadable files
image
image

Comments
Only logged in users can leave comments