Repository navigation
Expand file tree
/
Copy pathesp32_MQ135.ino
More file actions
52 lines (44 loc) · 1.57 KB
/
Copy pathesp32_MQ135.ino
File metadata and controls
52 lines (44 loc) · 1.57 KB
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
#define RL 47 //The value of resistor RL is 47K
#define m -0.263 //Enter calculated Slope
#define b 0.42 //Enter calculated interceptm
#define Ro 20 //Enter found Ro value
#define MQ_sensor 34 //Sensor is connected to A4
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini myDFPlayer;
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
// variable for storing the potentiometer value
int potValue = 0;
void setup() {
Serial.begin(115200);
Serial2.begin(9600);
if (!myDFPlayer.begin(Serial2)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
myDFPlayer.volume(30); //Set volume value. From 0 to 30
}
void loop() {
boolean play_state = digitalRead(23);
unsigned int Value = 0.0;
float VRL; //Voltage drop across the MQ sensor
float Rs; //Sensor resistance at gas concentration
float ratio; //Define variable for ratio
VRL = analogRead(MQ_sensor)*(3.3/4095.0); //Measure the voltage drop and convert to 0-5V
Rs = ((3.3*RL)/VRL)-RL; //Use formula to get Rs value
ratio = Rs/Ro; // find ratio Rs/Ro
float ppm = pow(10, ((log10(ratio)-b)/m)); //use formula to calculate ppm
Value = ppm;
Serial.print(ppm);
Serial.print(" ppm,");
Serial.print(VRL);
Serial.println(" v");
if (VRL > 1) {
if (play_state == HIGH) {
myDFPlayer.play(1);
}
}
}