Lesson 29 MQ-2 Gas Sensor

Share for us


Gas Sensor MQ-2 is a sensor for flammable gas and smoke by detecting the concentration of combustible gas in the air. They are used in gas detecting equipment for smoke and flammable gasses in household, industry or automobile.


– 1 * Raspberry Pi

– 1 * Active Buzzer module

– 1 * ADC0832

– 1 * MQ-2 Gas Sensor module

– Several jumper wires

Experimental Principle

MQ-2 gas sensor applies SnO2 (an oxygen-deficient n-type semiconductor) which has a lower conductivity in the clear air as a gas-sensing material. In an atmosphere where there may be inflammable gases, the conductivity of the gas sensor raises along with the inflammable gas concentration increases. MQ-2 performs a good detection to different inflammable gases such as natural gas, especially sensitive to liquefied gas, propane and hydrogen. In this experiment, if the harmful gas around the sensor reaches a certain concentration, the buzzer will beep.

Experimental Procedures

Step 1: Build the circuit

Connect pin OUT on MQ-2 gas sensor module to pin CH0 of the ADC0832;

Connect pin S on Buzzer module to GPIO3 of the Raspberry Pi.

Step 2: Edit and save the code (see path/Rpi_SensorKit_Code/28_ MQ-2/mq-2.c)

Step 3: Compile

              gcc  mq-2.c  -lwiringPi

Step 4: Run


Now, ignite a lighter near the module. When the harmful gas around the sensor reaches a certain concentration, the buzzer will beep.


#include <wiringPi.h>
#include <stdio.h>

typedef unsigned char uchar;
typedef unsigned int  uint;

#define     ADC_CS    0
#define     ADC_CLK   1
#define     ADC_DIO   2
#define     BEEP      3

uchar get_ADC_Result(void)
	uchar i;
	uchar dat1=0, dat2=0;

	digitalWrite(ADC_CS, 0);

	digitalWrite(ADC_DIO,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_DIO,1);    delayMicroseconds(2); //CH0 10
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);

	digitalWrite(ADC_DIO,0);	delayMicroseconds(2); //CH0 0
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
		digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);

		pinMode(ADC_DIO, INPUT);
		dat1=dat1<<1 | digitalRead(ADC_DIO);
		dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i);
		digitalWrite(ADC_CLK,1); 	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);


	pinMode(ADC_DIO, OUTPUT);
	return(dat1==dat2) ? dat1 : 0;

int main(void)
	uchar tmp;

	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1; 

	pinMode(ADC_CS,  OUTPUT);
	pinMode(ADC_CLK, OUTPUT);
	pinMode(BEEP,    OUTPUT);

		pinMode(ADC_DIO, OUTPUT);
		tmp = get_ADC_Result();

		if(tmp > 100){
			digitalWrite(BEEP, 0);
			digitalWrite(BEEP, 1);
			digitalWrite(BEEP, 0);

	return 0;

Python Code

#!/usr/bin/env python
#		This is a program for MQ-2 Gas Sensor Module.
#	It could detect danger gas and smokes.
#		This program depend on ADC0832 ADC chip. Follow 
#	the instruction book to connect the module and 
#	ADC0832 to your Raspberry Pi.

import RPi.GPIO as GPIO
import ADC0832
import time

BEEP = 16		# Set buzzer pin
TS   = 100		# You can set the Threshold by yourself (0-255)

def setup():
	GPIO.setmode(GPIO.BOARD)		# Numbers GPIOs by physical location
	GPIO.setup(BEEP, GPIO.OUT)		# Set pins' mode is output
	ADC0832.setup()					# Setup ADC0832

def loop():
	while True:
		tmp = ADC0832.getResult()			# Get analog value from ADC0832
		print tmp							# Print analog value
		if tmp > TS :						# Beep when read value greater than threshold
			print '    ****************'
			print '    * !! DANGER !! *'
			print '    ****************'
			print ''
			GPIO.output(BEEP, GPIO.HIGH)	# (0, means detect danger gas)
			GPIO.output(BEEP, GPIO.LOW)
		else :
			time.sleep(0.5)					# Else delay printing.

def destory():
	GPIO.cleanup()				# Release resource

if __name__ == '__main__':		# Program start from here
	except KeyboardInterrupt:  	# When 'Ctrl+C' is pressed, the child program destroy() will be  executed.