MQ2 Gas Sensor and Arduino


The MQ2 is one of a series of gas detectors and will detect flammable gasses and smoke. The topic of gas sensors is covered in the Arduino Playground here and you should visit this page for more details. This page also lists the various sensors in the range.

This blog records my efforts with this sensor. The small board I purchased from eBay has four pins, Vcc, Gnd, Analog Output and Digital output. Connect Vcc to 5 volts (do not try to power this from an Arduino Digital Output pin as the current drawn is too great. Connect Gnd to the Arduino Gnd. The sensor outputs a voltage proportional to the concentration of the flammable gas, while the Digital Output switches from High to Low when a certain level is reached. The level at which the DO pin switches is set using a small potentiometer on the board. For the sketch below connect the Analog Output to Arduino Analog Pin A0, and the Digital Output pin to Arduino Pin D2. The sketch will show the value of the voltage output and the state of the Digital Output, on the Serial Monitor (9600 baud).

To test the device I soaked a small piece of cotton wool in a flammable solvent and placed this at the bottom of a glass beaker. After five minutes I placed the sensor into the glass beaker and read the outputs from the Serial Monitor. The board I was using had an LED attached to the Digital Output, that came on when the output was greater than 400 (but this can easily be adjusted).

The sensor will get warm in use and can reach 50 or 60*C. I tested my setup by soaking a small piece of cotton wool in a solvent and placing it at the bottom of a glass beaker. After a few minutes I lowered the sensor into the beaker. I found different solvents gave different voltages, for example Ethanol gave a reading of about 250 to 300, while a low boiling petrol fraction gave a reading in excess of 600.

The manufacturer recommends that the sensor is not exposed to solvent vapour for extended periods.

The following sketch prints out the output voltage and state of the Digital Output, a buzzer could be substituted for the LED attached to the Digital output, but this could become a little annoying after a while.

/* GAS Sensor MQ-2
This sensor detects flammable gasses
the board has four pins
connect AO to Arduino pin A0
connect DO to Arduino pin 2
connect Gnd to Arduino Gnd
connect Vcc to Arduino 5 volts

int sensorPin = A0; // select the input pin for the potentiometer
int DOPin = 2; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int ledPin =13;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(DOPin, INPUT);
pinMode(ledPin, OUTPUT);

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Analog Output = ");
// turn the ledPin on if triggered
if (digitalRead(DOPin) ==HIGH){
digitalWrite(ledPin, LOW);
Serial.println("Digital Output = OFF");
else {
digitalWrite(ledPin, HIGH);
Serial.println("Digital Output = ON");


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: