ElectroPeak
Published © GPL3+

How to Calibrate & Use MQ9 Gas Sensor w/ Arduino

In this tutorial, you will learn how to calibrate and use MQ9 gas sensor with an Arduino board.

BeginnerProtip1 hour14,197

Things used in this project

Hardware components

ElectroPeak MQ-9 Carbon Monoxide & Flammable Gas Sensor Module
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

code1

Arduino
/* 
 
MQ9 Calibration 
 
modified on 19 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
 
*/ 
 
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); 

}

code2

Arduino
/* 
 
  MQ9 
 
  modified on 19 Feb 2019 
  by Saeed Hosseini 
  https://electropeak.com/learn/ 
 
*/ 
 
const int LED = 2; 
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(LED, HIGH); 
  else if (alarm == 0) digitalWrite(LED, LOW); 
 
  delay(1000); 
 
}

Credits

ElectroPeak

ElectroPeak

48 projects • 393 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.

Comments