Background
A gas sensor is an apparatus that identifies the existence of various gases in the surrounding atmosphere. These sensors are utilized in a wide range of applications, including security systems in refineries, industrial facilities, and homes. They are capable of detecting combustible gases, toxic gases, pollutants, and more.
As part of the pollution detection & control project, this demo is intended to show you how to alert dangerous gas leaks by using the MQ-9 carbon monoxide combustible gas sensor with the buzzer.
Components
The MQ-9 is a semiconductor device for detecting the presence of methane, carbon monoxide, and LPG gases. The sensing component of it is tin dioxide, or Sn02, which the conductivity is impaired in clean air.
The active buzzer from the starter kit is also used to sound the alarm when gas leaks are detected. With a built-in oscillating source, the active buzzer will generate a sound when electrified.
Other components to make this prototype will include:
- ELEGOO UNO R3 Board ATmega328P with USB Cable
- Breadboard
- 1KΩ Resistor
- Jumper Wires
Wiring Diagram
Code
MQ-9 requires calibration before using the module. It includes R0 (sensor resistance in 1000ppm concentration of LPG) and Rs (Internal resistance of the sensor which changes by gas concentration). Run the following code for 15 minutes in clean air before the first use until R0 has a fixed value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
void setup() { Serial.begin(9600); } void loop() { float sensor_volt; float RS_air; // Rs in clean air float R0; // R0 in 1000 ppm LPG float sensorValue; //Average for(int x = 0 ; x < 100 ; x++) { sensorValue = sensorValue + analogRead(A0); } sensorValue = sensorValue/100.0; //-----------------------------------------------/ sensor_volt = (sensorValue/1024)*5.0; RS_air = (5.0-sensor_volt)/sensor_volt; // Depend on RL on yor module R0 = RS_air/9.9; // According to MQ9 datasheet table Serial.print("sensor_volt = "); Serial.print(sensor_volt); Serial.println("V"); Serial.print("R0 = "); Serial.println(R0); delay(1000); } |
After obtaining a fixed R0 value, run the following code with the R0 value for trigging the alarm if there is a gas leak.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
int buzzer = 12; //the pin of the active buzzer const int DO = 8; void setup() { Serial.begin(9600); pinMode(LED, OUTPUT); pinMode(DO, INPUT); } void loop() { int alarm = 0; float sensor_volt; float RS_gas; float ratio; //-Replace the name "R0" with the value of R0 in the demo of First Test -/ float R0 = 0.91; int sensorValue = analogRead(A0); sensor_volt = ((float)sensorValue / 1024) * 5.0; RS_gas = (5.0 - sensor_volt) / sensor_volt; // Depend on RL on yor module ratio = RS_gas / R0; // ratio = RS/R0 //------------------------------------------------------------/ Serial.print("sensor_volt = "); Serial.println(sensor_volt); Serial.print("RS_ratio = "); Serial.println(RS_gas); Serial.print("Rs/R0 = "); Serial.println(ratio); Serial.print("\n\n"); alarm = digitalRead(DO); if (alarm == 1) digitalWrite(buzzer, HIGH); else if (alarm == 0) digitalWrite(buzzer, LOW); delay(1000); } |
Reference
Elegoo Super Starter Kit for UNO. Lesson 6 Active Buzzer
How to interface MQ-9 Gas Sensor with Arduino. https://www.circuits-diy.com/how-to-interface-mq-9-gas-sensor-with-arduino/
MQ-9. Hanwei Eletronics CO., LTD. www.hwsensor.com