You are on page 1of 4

S

c
h
i
B
e

E
l
e
k
t
r
o
n
i
k


Sample Code for Arduino
/ *******************Demo f or MQ- 2 Gas Sensor Modul e
V1. 0*****************************
Aut hor : Ti equan Shao: t i equan. shao[ at ] sandboxel ect r oni cs. com
Peng Wei : peng. wei [ at ] sandboxel ect r oni cs. com

Li sence: At t r i but i on- NonCommer ci al - Shar eAl i ke 3. 0 Unpor t ed ( CC BY- NC- SA
3. 0)
Not e: Thi s pi ece of sour ce code i s supposed t o be used as a demost r at i on
ONLY. Mor e
sophi st i cat ed cal i br at i on i s r equi r ed f or i ndust r i al f i el d appl i cat i on.

***************************************************************************
*********/
/ ************************Har dwar e Rel at ed
Macr os************************************/
#def i ne MQ_PI N ( 0) / / def i ne whi ch anal og i nput channel you ar e goi ng t o use
#def i ne RL_VALUE ( 5) / / def i ne t he l oad r esi st ance on t he boar d, i n ki l o
ohms
#def i ne RO_CLEAN_AI R_FACTOR ( 9. 83) / / RO_CLEAR_AI R_FACTOR=( Sensor r esi st ance
i n cl ean ai r ) / RO,
/ / whi ch i s der i ved f r omt he char t i n dat asheet
/ ***********************Sof t war e Rel at ed
Macr os************************************/
#def i ne CALI BARAI ON_SAMPLE_TI MES ( 50) / / def i ne how many sampl es you ar e
goi ng t o t ake i n t he cal i br at i on phase
#def i ne CALI BRATI ON_SAMPLE_I NTERVAL ( 500) / / def i ne t he t i me i nt er al ( i n
mi l i second) bet ween each sampl es i n t he
/ / cabl i br at i on phase
#def i ne READ_SAMPLE_I NTERVAL ( 50) / / def i ne how many sampl es you ar e goi ng
t o t ake i n nor mal oper at i on
#def i ne READ_SAMPLE_TI MES ( 5) / / def i ne t he t i me i nt er al ( i n mi l i second)
bet ween each sampl es i n
/ / nor mal oper at i on
/ **********************Appl i cat i on Rel at ed
Macr os**********************************/
#def i ne GAS_LPG ( 0)
#def i ne GAS_CO ( 1)
#def i ne GAS_SMOKE ( 2)
/ *****************************Gl obal s**************************************
*********/
f l oat LPGCur ve[ 3] = {2. 3, 0. 21, - 0. 47}; / / t wo poi nt s ar e t aken f r omt he
cur ve.
S
c
h
i
B
e

E
l
e
k
t
r
o
n
i
k
/ / wi t h t hese t wo poi nt s, a l i ne i s f or med whi ch i s " appr oxi mat el y
equi val ent "
/ / t o t he or i gi nal cur ve.
/ / dat a f or mat : { x, y, sl ope}; poi nt 1: ( l g200, 0. 21) , poi nt 2: ( l g10000, -
0. 59)
f l oat COCur ve[ 3] = {2. 3, 0. 72, - 0. 34}; / / t wo poi nt s ar e t aken f r omt he cur ve.
/ / wi t h t hese t wo poi nt s, a l i ne i s f or med whi ch i s " appr oxi mat el y
equi val ent "
/ / t o t he or i gi nal cur ve.
/ / dat a f or mat : { x, y, sl ope}; poi nt 1: ( l g200, 0. 72) , poi nt 2: ( l g10000,
0. 15)
f l oat SmokeCur ve[ 3] ={2. 3, 0. 53, - 0. 44}; / / t wo poi nt s ar e t aken f r omt he
cur ve.
/ / wi t h t hese t wo poi nt s, a l i ne i s f or med whi ch i s " appr oxi mat el y
equi val ent "
/ / t o t he or i gi nal cur ve.
/ / dat a f or mat : { x, y, sl ope}; poi nt 1: ( l g200, 0. 53) , poi nt 2: ( l g10000, -
0. 22)
f l oat Ro = 10; / / Ro i s i ni t i al i zed t o 10 ki l o ohms
voi d set up( )
{
Ser i al . begi n( 9600) ; / / UART set up, baudr at e = 9600bps
Ser i al . pr i nt ( " Cal i br at i ng. . . \ n" ) ;
Ro = MQCal i br at i on( MQ_PI N) ; / / Cal i br at i ng t he sensor . Pl ease make sur e t he
sensor i s i n cl ean ai r
/ / when you per f or mt he cal i br at i on
Ser i al . pr i nt ( " Cal i br at i on i s done. . . \ n" ) ;
Ser i al . pr i nt ( " Ro=" ) ;
Ser i al . pr i nt ( Ro) ;
Ser i al . pr i nt ( " kohm" ) ;
Ser i al . pr i nt ( " \ n" ) ;
}
voi d l oop( )
{
Ser i al . pr i nt ( " LPG: " ) ;
Ser i al . pr i nt ( MQGet GasPer cent age( MQRead( MQ_PI N) / Ro, GAS_LPG) ) ;
Ser i al . pr i nt ( " ppm" ) ;
Ser i al . pr i nt ( " " ) ;
Ser i al . pr i nt ( " CO: " ) ;
Ser i al . pr i nt ( MQGet GasPer cent age( MQRead( MQ_PI N) / Ro, GAS_CO) ) ;
Ser i al . pr i nt ( " ppm" ) ;
Ser i al . pr i nt ( " " ) ;
Ser i al . pr i nt ( " SMOKE: " ) ;
Ser i al . pr i nt ( MQGet GasPer cent age( MQRead( MQ_PI N) / Ro, GAS_SMOKE) ) ;
Ser i al . pr i nt ( " ppm" ) ;
Ser i al . pr i nt ( " \ n" ) ;
del ay( 200) ;
}
/ ****************** MQResi st anceCal cul at i on
****************************************
I nput : r aw_adc - r aw val ue r ead f r omadc, whi ch r epr esent s t he vol t age
Out put : t he cal cul at ed sensor r esi st ance
Remar ks: The sensor and t he l oad r esi st or f or ms a vol t age di vi der . Gi ven
t he vol t age
acr oss t he l oad r esi st or and i t s r esi st ance, t he r esi st ance of t he sensor
coul d be der i ved.
***************************************************************************
*********/
f l oat MQResi st anceCal cul at i on( i nt r aw_adc)
{
r et ur n ( ( ( f l oat ) RL_VALUE*( 1023- r aw_adc) / r aw_adc) ) ;
}
S
c
h
i
B
e

E
l
e
k
t
r
o
n
i
k
/ ***************************** MQCal i br at i on
****************************************
I nput : mq_pi n - anal og channel
Out put : Ro of t he sensor
Remar ks: Thi s f unct i on assumes t hat t he sensor i s i n cl ean ai r . I t use
MQResi st anceCal cul at i on t o cal cul at es t he sensor r esi st ance i n cl ean ai r
and t hen di vi des i t wi t h RO_CLEAN_AI R_FACTOR. RO_CLEAN_AI R_FACTOR i s about
10, whi ch di f f er s sl i ght l y bet ween di f f er ent sensor s.
***************************************************************************
*********/
f l oat MQCal i br at i on( i nt mq_pi n)
{
i nt i ;
f l oat val =0;

f or ( i =0; i <CALI BARAI ON_SAMPLE_TI MES; i ++) { / / t ake mul t i pl e sampl es
val += MQResi st anceCal cul at i on( anal ogRead( mq_pi n) ) ;
del ay( CALI BRATI ON_SAMPLE_I NTERVAL) ;
}
val = val / CALI BARAI ON_SAMPLE_TI MES; / / cal cul at e t he aver age val ue

val = val / RO_CLEAN_AI R_FACTOR; / / di vi ded by RO_CLEAN_AI R_FACTOR yi el ds t he
Ro
/ / accor di ng t o t he char t i n t he dat asheet

r et ur n val ;
}
/ ***************************** MQRead
*********************************************
I nput : mq_pi n - anal og channel
Out put : Rs of t he sensor
Remar ks: Thi s f unct i on use MQResi st anceCal cul at i on t o cacul at e t he sensor
r esi st enc ( Rs) .
The Rs changes as t he sensor i s i n t he di f f er ent consent r at i on of t he
t ar get
gas. The sampl e t i mes and t he t i me i nt er val bet ween sampl es coul d be
conf i gur ed
by changi ng t he def i ni t i on of t he macr os.
***************************************************************************
*********/
f l oat MQRead( i nt mq_pi n)
{
i nt i ;
f l oat r s=0;
f or ( i =0; i <READ_SAMPLE_TI MES; i ++) {
r s += MQResi st anceCal cul at i on( anal ogRead( mq_pi n) ) ;
del ay( READ_SAMPLE_I NTERVAL) ;
}

r s = r s/ READ_SAMPLE_TI MES;

r et ur n r s;
}
/ ***************************** MQGet GasPer cent age
**********************************
I nput : r s_r o_r at i o - Rs di vi ded by Ro
gas_i d - t ar get gas t ype
Out put : ppmof t he t ar get gas
Remar ks: Thi s f unct i on passes di f f er ent cur ves t o t he MQGet Per cent age
f unct i on whi ch
cal cul at es t he ppm( par t s per mi l l i on) of t he t ar get gas.
***************************************************************************
*********/
S
c
h
i
B
e

E
l
e
k
t
r
o
n
i
k
i nt MQGet GasPer cent age( f l oat r s_r o_r at i o, i nt gas_i d)
{
i f ( gas_i d == GAS_LPG ) {
r et ur n MQGet Per cent age( r s_r o_r at i o, LPGCur ve) ;
} el se i f ( gas_i d == GAS_CO ) {
r et ur n MQGet Per cent age( r s_r o_r at i o, COCur ve) ;
} el se i f ( gas_i d == GAS_SMOKE ) {
r et ur n MQGet Per cent age( r s_r o_r at i o, SmokeCur ve) ;
}

r et ur n 0;
}
/ ***************************** MQGet Per cent age
**********************************
I nput : r s_r o_r at i o - Rs di vi ded by Ro
pcur ve - poi nt er t o t he cur ve of t he t ar get gas
Out put : ppmof t he t ar get gas
Remar ks: By usi ng t he sl ope and a poi nt of t he l i ne. The x( l ogar i t hmi c
val ue of ppm)
of t he l i ne coul d be der i ved i f y( r s_r o_r at i o) i s pr ovi ded. As i t i s a
l ogar i t hmi c coor di nat e, power of 10 i s used t o conver t t he r esul t t o non-
l ogar i t hmi c
val ue.
***************************************************************************
*********/
i nt MQGet Per cent age( f l oat r s_r o_r at i o, f l oat *pcur ve)
{
r et ur n ( pow( 10, ( ( l og( r s_r o_r at i o- pcur ve[ 1] ) / pcur ve[ 2] ) + pcur ve[ 0] ) ) ) ;
}
Demo Output

You might also like