MQ เป็นเซ็นเซอร์ก๊าซชนิดหนึ่งที่ถูกออกแบบมาเพื่อตรวจจับก๊าซที่ติดไฟได้หลายชนิด เช่น ก๊าซแอลกอฮอล์, ก๊าซธรรมชาติ และก๊าซไฮโดรเจน เซ็นเซอร์นี้ทำงานบนหลักการเปลี่ยนแปลงความต้านทานไฟฟ้าเมื่อสัมผัสกับก๊าซที่ต้องการตรวจจับ
แบบที่ 1 ใช้งานแบบ Digital
1.ต่ออุปกรณ์

MQ-135 > Arduino UNO
GND>GNDVCC>5VDO>Pin 7
2.ลงโปรแกรม
Copy โค้ดด้านล่าง
Arduino
1int sensor = 7;
2int val = 0;
3void setup() {
4 Serial.begin(9600);
5}
6void loop() {
7 val = digitalRead(sensor);
8 if (val == 0) {
9 Serial.println("MQ-135 Detected");
10 }
11 delay(500);
12}1int sensor = 7;
2int val = 0;
3void setup() {
4 Serial.begin(9600);
5}
6void loop() {
7 val = digitalRead(sensor);
8 if (val == 0) {
9 Serial.println("MQ-135 Detected");
10 }
11 delay(500);
12}
แบบที่ 2 ใช้งานแบบ Analog
1.ต่ออุปกรณ์

MQ-135 > Arduino UNO
GND>GNDVCC>5VAO>A0
2.ลงโปรแกรม
Copy โค้ดด้านล่าง
Arduino
1int sensor = A0;
2int val = 0;
3void setup() {
4 Serial.begin(9600);
5}
6void loop() {
7 val = analogRead(sensor);
8 Serial.println(val);
9 delay(500);
10}1int sensor = A0;
2int val = 0;
3void setup() {
4 Serial.begin(9600);
5}
6void loop() {
7 val = analogRead(sensor);
8 Serial.println(val);
9 delay(500);
10}
แบบที่ 3 ใช้งานแบบ Analog หน่วย PPM
1.ต่ออุปกรณ์

MQ-135 > Arduino UNO
GND>GNDVCC>5VAO>A0
2.ลงโปรแกรม
Copy โค้ดด้านล่าง
Arduino
1/*******************Demo for MQ-135 Gas Sensor Module*****************************
2 Support: Tiequan Shao: support[at]sandboxelectronics.com
3 Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
4 Note: This piece of source code is supposed to be used as a demonstration ONLY.
5 More sophisticated calibration is required for industrial field application.
6 Sandbox Electronics 2011-04-25
7************************************************************************************/
8
9/************************Hardware Related Macros************************************/
10#define MQ_PIN (0) //define which analog input channel you are going to use
11#define RL_VALUE (1) //define the load resistance on the board, in kilo ohms
12#define RO_CLEAN_AIR_FACTOR (3.59) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
13//which is derived from the chart in datasheet
14
15/***********************Software Related Macros************************************/
16#define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
17#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval(in milliseconds) between each samples in the calibration phase
18#define READ_SAMPLE_INTERVAL (50) //define the time interval(in milliseconds) between each samples in normal operation
19#define READ_SAMPLE_TIMES (5) //define how many samples you are going to take in normal operation
20
21/**********************Application Related Macros**********************************/
22#define GAS_CO2 (0)
23#define GAS_NH3 (1)
24#define GAS_ALCOHOL (2)
25
26/*****************************Globals***********************************************/
27float CO2Curve[3] = {2.3, 0.72, -0.34}; // MQ-135 curve for CO2
28float NH3Curve[3] = {2.3, 0.53, -0.44}; // MQ-135 curve for NH3
29float AlcoholCurve[3] = {2.3, 0.67, -0.33}; // MQ-135 curve for Alcohol
30float Ro = 10; //Ro is initialized to 10 kilo ohms
31
32void setup()
33{
34 Serial.begin(9600); //UART setup, baudrate = 9600bps
35 Serial.print("Calibrating...\n");
36 Ro = MQCalibration(MQ_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air
37 Serial.print("Calibration is done...\n");
38 Serial.print("Ro=");
39 Serial.print(Ro);
40 Serial.print("kohm");
41 Serial.print("\n");
42}
43
44void loop()
45{
46 Serial.print("CO2:");
47 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_CO2) );
48 Serial.print( "ppm" );
49 Serial.print(" ");
50 Serial.print("NH3:");
51 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_NH3) );
52 Serial.print( "ppm" );
53 Serial.print(" ");
54 Serial.print("Alcohol:");
55 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_ALCOHOL) );
56 Serial.print( "ppm" );
57 Serial.print("\n");
58 delay(200);
59}
60
61float MQResistanceCalculation(int raw_adc)
62{
63 return ( ((float)RL_VALUE * (1023 - raw_adc) / raw_adc));
64}
65
66float MQCalibration(int mq_pin)
67{
68 int i;
69 float val = 0;
70
71 for (i = 0; i < CALIBARAION_SAMPLE_TIMES; i++) {
72 val += MQResistanceCalculation(analogRead(mq_pin));
73 delay(CALIBRATION_SAMPLE_INTERVAL);
74 }
75 val = val / CALIBARAION_SAMPLE_TIMES;
76 val = val / RO_CLEAN_AIR_FACTOR;
77 return val;
78}
79
80float MQRead(int mq_pin)
81{
82 int i;
83 float rs = 0;
84
85 for (i = 0; i < READ_SAMPLE_TIMES; i++) {
86 rs += MQResistanceCalculation(analogRead(mq_pin));
87 delay(READ_SAMPLE_INTERVAL);
88 }
89
90 rs = rs / READ_SAMPLE_TIMES;
91 return rs;
92}
93
94int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
95{
96 if ( gas_id == GAS_CO2 ) {
97 return MQGetPercentage(rs_ro_ratio, CO2Curve);
98 } else if ( gas_id == GAS_NH3 ) {
99 return MQGetPercentage(rs_ro_ratio, NH3Curve);
100 } else if ( gas_id == GAS_ALCOHOL ) {
101 return MQGetPercentage(rs_ro_ratio, AlcoholCurve);
102 }
103
104 return 0;
105}
106
107int MQGetPercentage(float rs_ro_ratio, float *pcurve)
108{
109 return (pow(10, ( ((log(rs_ro_ratio) - pcurve[1]) / pcurve[2]) + pcurve[0])));
110}
1111/*******************Demo for MQ-135 Gas Sensor Module*****************************
2 Support: Tiequan Shao: support[at]sandboxelectronics.com
3 Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
4 Note: This piece of source code is supposed to be used as a demonstration ONLY.
5 More sophisticated calibration is required for industrial field application.
6 Sandbox Electronics 2011-04-25
7************************************************************************************/
8
9/************************Hardware Related Macros************************************/
10#define MQ_PIN (0) //define which analog input channel you are going to use
11#define RL_VALUE (1) //define the load resistance on the board, in kilo ohms
12#define RO_CLEAN_AIR_FACTOR (3.59) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
13//which is derived from the chart in datasheet
14
15/***********************Software Related Macros************************************/
16#define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
17#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval(in milliseconds) between each samples in the calibration phase
18#define READ_SAMPLE_INTERVAL (50) //define the time interval(in milliseconds) between each samples in normal operation
19#define READ_SAMPLE_TIMES (5) //define how many samples you are going to take in normal operation
20
21/**********************Application Related Macros**********************************/
22#define GAS_CO2 (0)
23#define GAS_NH3 (1)
24#define GAS_ALCOHOL (2)
25
26/*****************************Globals***********************************************/
27float CO2Curve[3] = {2.3, 0.72, -0.34}; // MQ-135 curve for CO2
28float NH3Curve[3] = {2.3, 0.53, -0.44}; // MQ-135 curve for NH3
29float AlcoholCurve[3] = {2.3, 0.67, -0.33}; // MQ-135 curve for Alcohol
30float Ro = 10; //Ro is initialized to 10 kilo ohms
31
32void setup()
33{
34 Serial.begin(9600); //UART setup, baudrate = 9600bps
35 Serial.print("Calibrating...\n");
36 Ro = MQCalibration(MQ_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air
37 Serial.print("Calibration is done...\n");
38 Serial.print("Ro=");
39 Serial.print(Ro);
40 Serial.print("kohm");
41 Serial.print("\n");
42}
43
44void loop()
45{
46 Serial.print("CO2:");
47 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_CO2) );
48 Serial.print( "ppm" );
49 Serial.print(" ");
50 Serial.print("NH3:");
51 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_NH3) );
52 Serial.print( "ppm" );
53 Serial.print(" ");
54 Serial.print("Alcohol:");
55 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_ALCOHOL) );
56 Serial.print( "ppm" );
57 Serial.print("\n");
58 delay(200);
59}
60
61float MQResistanceCalculation(int raw_adc)
62{
63 return ( ((float)RL_VALUE * (1023 - raw_adc) / raw_adc));
64}
65
66float MQCalibration(int mq_pin)
67{
68 int i;
69 float val = 0;
70
71 for (i = 0; i < CALIBARAION_SAMPLE_TIMES; i++) {
72 val += MQResistanceCalculation(analogRead(mq_pin));
73 delay(CALIBRATION_SAMPLE_INTERVAL);
74 }
75 val = val / CALIBARAION_SAMPLE_TIMES;
76 val = val / RO_CLEAN_AIR_FACTOR;
77 return val;
78}
79
80float MQRead(int mq_pin)
81{
82 int i;
83 float rs = 0;
84
85 for (i = 0; i < READ_SAMPLE_TIMES; i++) {
86 rs += MQResistanceCalculation(analogRead(mq_pin));
87 delay(READ_SAMPLE_INTERVAL);
88 }
89
90 rs = rs / READ_SAMPLE_TIMES;
91 return rs;
92}
93
94int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
95{
96 if ( gas_id == GAS_CO2 ) {
97 return MQGetPercentage(rs_ro_ratio, CO2Curve);
98 } else if ( gas_id == GAS_NH3 ) {
99 return MQGetPercentage(rs_ro_ratio, NH3Curve);
100 } else if ( gas_id == GAS_ALCOHOL ) {
101 return MQGetPercentage(rs_ro_ratio, AlcoholCurve);
102 }
103
104 return 0;
105}
106
107int MQGetPercentage(float rs_ro_ratio, float *pcurve)
108{
109 return (pow(10, ( ((log(rs_ro_ratio) - pcurve[1]) / pcurve[2]) + pcurve[0])));
110}
111