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

MQ-3 > 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-3 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-3 Detected");
10 }
11 delay(500);
12}
แบบที่ 2 ใช้งานแบบ Analog
1.ต่ออุปกรณ์

MQ-3 > 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-3 > Arduino UNO
GND>GNDVCC>5VAO>A0
2.ลงโปรแกรม
Copy โค้ดด้านล่าง
Arduino
1/*******************Demo for MQ-3 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. More
5 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 (5) //define the load resistance on the board, in kilo ohms
12#define RO_CLEAN_AIR_FACTOR (60.0) //RO_CLEAR_AIR_FACTOR for MQ-3, this value may vary. Please refer to the MQ-3 datasheet
13
14/***********************Software Related Macros************************************/
15#define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
16#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval (in milliseconds) between each sample in the calibration phase
17#define READ_SAMPLE_INTERVAL (50) //define the time interval (in milliseconds) between each sample in normal operation
18#define READ_SAMPLE_TIMES (5) //define how many samples you are going to take in normal operation
19
20/**********************Application Related Macros**********************************/
21#define GAS_ALCOHOL (0)
22
23/*****************************Globals***********************************************/
24float AlcoholCurve[3] = {2.3, 0.94, -0.45}; //two points are taken from the curve for MQ-3 alcohol detection
25//data format:{ x, y, slope}; these values are approximated based on the datasheet
26
27float Ro = 10; //Ro is initialized to 10 kilo ohms
28
29void setup()
30{
31 Serial.begin(9600); //UART setup, baudrate = 9600bps
32 Serial.print("Calibrating...\n");
33 Ro = MQCalibration(MQ_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air when you perform the calibration
34 Serial.print("Calibration is done...\n");
35 Serial.print("Ro=");
36 Serial.print(Ro);
37 Serial.print("kohm");
38 Serial.print("\n");
39}
40
41void loop()
42{
43 Serial.print("ALCOHOL:");
44 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_ALCOHOL) );
45 Serial.print( "ppm" );
46 Serial.print("\n");
47 delay(200);
48}
49
50/****************** MQResistanceCalculation ****************************************/
51float MQResistanceCalculation(int raw_adc)
52{
53 return ( ((float)RL_VALUE * (1023 - raw_adc) / raw_adc));
54}
55
56/***************************** MQCalibration ****************************************/
57float MQCalibration(int mq_pin)
58{
59 int i;
60 float val = 0;
61
62 for (i = 0; i < CALIBARAION_SAMPLE_TIMES; i++) { //take multiple samples
63 val += MQResistanceCalculation(analogRead(mq_pin));
64 delay(CALIBRATION_SAMPLE_INTERVAL);
65 }
66 val = val / CALIBARAION_SAMPLE_TIMES; //calculate the average value
67
68 val = val / RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
69
70 return val;
71}
72
73/***************************** MQRead *********************************************/
74float MQRead(int mq_pin)
75{
76 int i;
77 float rs = 0;
78
79 for (i = 0; i < READ_SAMPLE_TIMES; i++) {
80 rs += MQResistanceCalculation(analogRead(mq_pin));
81 delay(READ_SAMPLE_INTERVAL);
82 }
83
84 rs = rs / READ_SAMPLE_TIMES;
85
86 return rs;
87}
88
89/***************************** MQGetGasPercentage **********************************/
90int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
91{
92 if ( gas_id == GAS_ALCOHOL ) {
93 return MQGetPercentage(rs_ro_ratio, AlcoholCurve);
94 }
95
96 return 0;
97}
98
99/***************************** MQGetPercentage **********************************/
100int MQGetPercentage(float rs_ro_ratio, float *pcurve)
101{
102 return (pow(10, ( ((log(rs_ro_ratio) - pcurve[1]) / pcurve[2]) + pcurve[0])));
103}
1041/*******************Demo for MQ-3 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. More
5 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 (5) //define the load resistance on the board, in kilo ohms
12#define RO_CLEAN_AIR_FACTOR (60.0) //RO_CLEAR_AIR_FACTOR for MQ-3, this value may vary. Please refer to the MQ-3 datasheet
13
14/***********************Software Related Macros************************************/
15#define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
16#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval (in milliseconds) between each sample in the calibration phase
17#define READ_SAMPLE_INTERVAL (50) //define the time interval (in milliseconds) between each sample in normal operation
18#define READ_SAMPLE_TIMES (5) //define how many samples you are going to take in normal operation
19
20/**********************Application Related Macros**********************************/
21#define GAS_ALCOHOL (0)
22
23/*****************************Globals***********************************************/
24float AlcoholCurve[3] = {2.3, 0.94, -0.45}; //two points are taken from the curve for MQ-3 alcohol detection
25//data format:{ x, y, slope}; these values are approximated based on the datasheet
26
27float Ro = 10; //Ro is initialized to 10 kilo ohms
28
29void setup()
30{
31 Serial.begin(9600); //UART setup, baudrate = 9600bps
32 Serial.print("Calibrating...\n");
33 Ro = MQCalibration(MQ_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air when you perform the calibration
34 Serial.print("Calibration is done...\n");
35 Serial.print("Ro=");
36 Serial.print(Ro);
37 Serial.print("kohm");
38 Serial.print("\n");
39}
40
41void loop()
42{
43 Serial.print("ALCOHOL:");
44 Serial.print(MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_ALCOHOL) );
45 Serial.print( "ppm" );
46 Serial.print("\n");
47 delay(200);
48}
49
50/****************** MQResistanceCalculation ****************************************/
51float MQResistanceCalculation(int raw_adc)
52{
53 return ( ((float)RL_VALUE * (1023 - raw_adc) / raw_adc));
54}
55
56/***************************** MQCalibration ****************************************/
57float MQCalibration(int mq_pin)
58{
59 int i;
60 float val = 0;
61
62 for (i = 0; i < CALIBARAION_SAMPLE_TIMES; i++) { //take multiple samples
63 val += MQResistanceCalculation(analogRead(mq_pin));
64 delay(CALIBRATION_SAMPLE_INTERVAL);
65 }
66 val = val / CALIBARAION_SAMPLE_TIMES; //calculate the average value
67
68 val = val / RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
69
70 return val;
71}
72
73/***************************** MQRead *********************************************/
74float MQRead(int mq_pin)
75{
76 int i;
77 float rs = 0;
78
79 for (i = 0; i < READ_SAMPLE_TIMES; i++) {
80 rs += MQResistanceCalculation(analogRead(mq_pin));
81 delay(READ_SAMPLE_INTERVAL);
82 }
83
84 rs = rs / READ_SAMPLE_TIMES;
85
86 return rs;
87}
88
89/***************************** MQGetGasPercentage **********************************/
90int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
91{
92 if ( gas_id == GAS_ALCOHOL ) {
93 return MQGetPercentage(rs_ro_ratio, AlcoholCurve);
94 }
95
96 return 0;
97}
98
99/***************************** MQGetPercentage **********************************/
100int MQGetPercentage(float rs_ro_ratio, float *pcurve)
101{
102 return (pow(10, ( ((log(rs_ro_ratio) - pcurve[1]) / pcurve[2]) + pcurve[0])));
103}
104