-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreading_mq_sensors_3l_mux16.ino
105 lines (88 loc) · 3.73 KB
/
reading_mq_sensors_3l_mux16.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//Multiplexer definitions
#define S0 D0
#define S1 D1
#define S2 D2
#define S3 D3
#define analogpin A0
///////////////////////////////////////////////////////////////////////////////////
//MQ Gas sensors definitions
int MQ_PIN = analogpin; //MQ Pin designation in calculation
int RL_VALUE = 10; //load resistance on the board, in kilo ohms
float Ro; //Ro is extracted from calibratiom
float GAS[3]; //Targeted gas
int READ_SAMPLE_INTERVAL = 50; //number of samples in normal operation
int READ_SAMPLE_TIMES = 5; //time interval(in millisecond) between each samples in normal operation
/*****************************Globals***********************************************/
float LPGCurve[3] = {2.3,0.20,-0.46}; //calibration curve of LPG from the MQ2 datasheet, Curve data format:{ x, y, slope}
float COCurve[3] = {2.3,0.72,-0.34}; //calibration curve of CO from the MQ2 datasheet
float SmokeCurve[3] = {2.3,0.53,-0.44}; //calibration curve of Smoke from the MQ2 datasheet
float CH4Curve[3] = {3.3, 0, -0.38}; //calibration curve of CH4 from the MQ6 datasheet
float H2Curve[3] = {2.3,0.93,-1.44}; //calibration curve of H2 from the MQ8 datasheet
/****************** MQResistanceCalculation ****************************************/
float MQResistanceCalculation(int raw_adc) {
return (((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
/***************************** MQRead *********************************************/
float MQRead(int mq_pin) {
int i;
float rs=0;
for (i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin));
delay(READ_SAMPLE_INTERVAL);
}
rs = rs/READ_SAMPLE_TIMES;
return rs;
}
/***************************** MQGetPercentage **********************************/
int MQGetPercentage(float rs_ro_ratio, float *pcurve) {
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}
///////////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(analogpin, INPUT);
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(S0,LOW);
digitalWrite(S1,LOW);
digitalWrite(S2,LOW);
digitalWrite(S3,LOW); {
//Serial.print("2#MQ2: ");
Ro = 1.11;
Serial.print( "LPG: " );
Serial.print(MQGetPercentage(MQRead(MQ_PIN)/Ro,LPGCurve));
Serial.println( "ppm" );
Serial.print( "CO: " );
Serial.print(MQGetPercentage(MQRead(MQ_PIN)/Ro,COCurve));
Serial.println( "ppm" );
Serial.print( "Smoke: " );
Serial.print(MQGetPercentage(MQRead(MQ_PIN)/Ro,SmokeCurve));
Serial.println( "ppm" );
}
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
digitalWrite(S2,LOW);
digitalWrite(S3,LOW); {
//Serial.print("2#MQ6: ");
Ro = 0.54;
Serial.print( "CH4: " );
Serial.print(MQGetPercentage(MQRead(MQ_PIN)/Ro,CH4Curve));
Serial.println( "ppm" );
}
digitalWrite(S0,LOW);
digitalWrite(S1,HIGH);
digitalWrite(S2,LOW);
digitalWrite(S3,LOW); {
//Serial.print("3#MQ8: ");
Ro = 0.22;
Serial.print( "Hydrogen: " );
Serial.print(MQGetPercentage(MQRead(MQ_PIN)/Ro,H2Curve));
Serial.println( "ppm" );
}
Serial.println( " " );
delay(2000);
}