## Gas Sensor(MQ9)

### Gas Detection : Basic Example

In this example, the sensor is connected to A0 pin. The voltage read from the sensor is displayed. This value can be used as a threshold to detect any increase/decrease in gas concentration.

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

void loop() {
float sensor_volt;
float sensorValue;

sensor_volt = sensorValue/1024*5.0;

Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");
delay(1000);
}```

### Measurement : Approximation

This examples demonstrates a way to know the approximate concentration of Gas. As per the data-sheet of the MQ9 sensors, these equations are tested for standard conditions and are not calibrated. It may vary based on change in temperature or humidity.

1.Keep the Gas Sensor in clean air environment. Upload the below program.

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

void loop() {
float sensor_volt;
float RS_air; //  Get the value of RS via in a clear air
float R0;  // Get the value of R0 via in LPG
float sensorValue;

/*--- Get a average data by testing 100 times ---*/
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; // omit *RL
R0 = RS_air/9.9; // The ratio of RS/R0 is 9.9 in LPG gas from Graph (Found using WebPlotDigitizer)

Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");

Serial.print("R0 = ");
Serial.println(R0);
delay(1000);

}```

2. Then, open the serial monitor of Arduino IDE. Write down the value of R0 and this needs to be used in the next program. Please node down the R0 after the reading stabilizes.
Replace the R0 below with value of R0 tested above . Expose the sensor to any one of the gas listed above.

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

void loop() {

float sensor_volt;
float RS_gas; // Get value of RS in a GAS
float ratio; // Get ratio RS_GAS/RS_air
int sensorValue = analogRead(A0);
sensor_volt=(float)sensorValue/1024*5.0;
RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL

/*-Replace the name "R0" with the value of R0 in the demo of First Test -*/
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");

delay(1000);

}

```

If you need PPM (remember it is not a high quality sensor)

i have written a librarie and some code that will do just that.

librarie : MQ9-Sensor.zip

``` // MQ9-SensorRead Begin int sensorValue = analogRead(sensorPin); sensor_volt=(float)sensorValue/1024*5.0; RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL ratio = RS_gas/Rx0; // ratio = RS/R0 float y = ratio; float b = ya/(pow(xa,a)); float ppm = pow(y/b,(1/a)); // MQ9-SensorRead End ```

and before void setup you put in

``` #include <math.h> #include <MQ9-Sensor.h> #define sensorPin A0       // MQ-9 Sensor or where you put your sensor ```