diff --git a/README.md b/README.md index 00bde27..1f95ca7 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,15 @@ float ppmCH4 = MQ4.readSensor(); ### ESP8266-ESP32 ![ESP8266_Wiring_MQSensor](https://raw.githubusercontent.com/miguel5612/MQSensorsLib_Docs/master/static/img/MQ_ESP8266.PNG) +### ESP32 WROOM 32D +The ESP32 WROOM 32D does not need an external power supply. A0 goes to PIN36, Vcc to 3v3 and GND to any GND port on the board. Check the **ESP2/ESP32_WROOM_32** folder to fixing the measuring issue when connecting to wifi. + ### Manuals #### User Manual (v1.0) 12.2019 [Manual](https://drive.google.com/open?id=1BAFInlvqKR7h81zETtjz4_RC2EssvFWX) #### User Manual (v2.0) 04.2020 [Manual](https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/Docs/MQSensorLib_2.0.pdf) - ### Serial debug (optional) If your sensor is an **MQ2** (Same for others sensors): * To enable on setup wrote diff --git a/examples/ESP32/ESP32_WROOM_32D/ESP32_WROOM_32D.ino b/examples/ESP32/ESP32_WROOM_32D/ESP32_WROOM_32D.ino new file mode 100644 index 0000000..f157208 --- /dev/null +++ b/examples/ESP32/ESP32_WROOM_32D/ESP32_WROOM_32D.ino @@ -0,0 +1,136 @@ +/* + * This sketch fixes the MQ2 measuring issue when using Wifi.h + * Wifi.h deactivates pin 25 analogical input on esp32.ino default sketch. + * This is because Wifi.h works only with ADC1 for analogical measurement + * Please check the esp32-wroom-32d.jpg image on ESP32 folder + * + + * ADC1 GPIO + * ADC1_CH0 (GPIO 36) // only tested on this and it works as expected :) + * ADC1_CH1 (GPIO 37) + * ADC1_CH2 (GPIO 38) + * ADC1_CH3 (GPIO 39) + * ADC1_CH4 (GPIO 32) + * ADC1_CH5 (GPIO 33) + * ADC1_CH6 (GPIO 34) + * ADC1_CH7 (GPIO 35) + * + * ADC2 GPIO + * ADC2_CH0 (GPIO 4) + * ADC2_CH1 (GPIO 0) + * ADC2_CH2 (GPIO 2) + * ADC2_CH3 (GPIO 15) + * ADC2_CH4 (GPIO 13) + * ADC2_CH5 (GPIO 12) + * ADC2_CH6 (GPIO 14) + * ADC2_CH7 (GPIO 27) + * ADC2_CH8 (GPIO 25) + * ADC2_CH9 (GPIO 26) + * + */ + + +#include + +//Include the library +#include +/************************Hardware Related Macros************************************/ +#define Board ("ESP-32") // Wemos ESP-32 or other board, whatever have ESP32 core. + +//https://www.amazon.com/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated/dp/B0718T232Z (Although Amazon shows ESP-WROOM-32 ESP32 ESP-32S, the board is the ESP-WROOM-32D) +#define Pin (36) //check the esp32-wroom-32d.jpg image on ESP32 folder + +/***********************Software Related Macros************************************/ +#define Type ("MQ-2") //MQ2 or other MQ Sensor, if change this verify your a and b values. +#define Voltage_Resolution (3.3) // 3V3 <- IMPORTANT. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/ +#define ADC_Bit_Resolution (12) // ESP-32 bit resolution. Source: https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/ +#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm +/*****************************Globals***********************************************/ +MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type); +/*****************************Globals***********************************************/ + +const char* ssid = "your-ssid"; +const char* password = "your-password"; + +void setup() +{ + + //Init the serial port communication - to debug the library + Serial.begin(115200); //Init serial port + delay(10); + + //Set math model to calculate the PPM concentration and the value of constants + MQ2.setRegressionMethod(1); //_PPM = a*ratio^b + MQ2.setA(987.99); MQ2.setB(-2.162); // Configurate the ecuation values to get H2 concentration + +/* + Exponential regression: + Gas | a | b + H2 | 987.99 | -2.162 + LPG | 574.25 | -2.222 + CO | 36974 | -3.109 + Alcohol| 3616.1 | -2.675 + Propane| 658.71 | -2.168 + */ + + /***************************** MQ Init ********************************************/ + //Remarks: Configure the pin of arduino as input. + /************************************************************************************/ + MQ2.init(); + + /* + //If the RL value is different from 10K please assign your RL value with the following method: + MQ2.setRL(10); + */ + /***************************** MQ CAlibration ********************************************/ + // Explanation: + // In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated + // and now is on clean air (Calibration conditions), and it will setup R0 value. + // We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino + // This routine not need to execute to every restart, you can load your R0 if you know the value + // Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor + Serial.print("Calibrating please wait."); + float calcR0 = 0; + for(int i = 1; i<=10; i ++) + { + MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin + calcR0 += MQ2.calibrate(RatioMQ2CleanAir); + Serial.print("."); + } + MQ2.setR0(calcR0/10); + Serial.println(" done!."); + + if(isinf(calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);} + if(calcR0 == 0){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);} + /***************************** MQ CAlibration ********************************************/ + //MQ2.serialDebug(true); uncomment if you want to print the table on the serial port + + // We start by connecting to a WiFi network + + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loop() +{ + MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin + //MQ2.serialDebug(); // Will print the table on the serial port + Serial.print(MQ2.readSensor()); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup + Serial.println(" PPM"); + delay(500); //Sampling frequency +} + diff --git a/examples/ESP32/ESP32_WROOM_32D/images/esp-32-wroom-32d-board.jpg b/examples/ESP32/ESP32_WROOM_32D/images/esp-32-wroom-32d-board.jpg new file mode 100644 index 0000000..00213d0 Binary files /dev/null and b/examples/ESP32/ESP32_WROOM_32D/images/esp-32-wroom-32d-board.jpg differ diff --git a/examples/ESP32/ESP32_WROOM_32D/images/esp32-wroom-32d-pinout.jpg b/examples/ESP32/ESP32_WROOM_32D/images/esp32-wroom-32d-pinout.jpg new file mode 100644 index 0000000..ce92d51 Binary files /dev/null and b/examples/ESP32/ESP32_WROOM_32D/images/esp32-wroom-32d-pinout.jpg differ diff --git a/library.properties b/library.properties index 704a77c..ac5e2e4 100644 --- a/library.properties +++ b/library.properties @@ -6,5 +6,5 @@ sentence= This library allows you to read the MQ sensors very easily. paragraph= This library allows an Arduino/Genuino/ESP8266 board to read MQ Sensors (AIr quality meter) references: MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MQ131, MQ135, MQ303A, MQ309A. category= Sensors url=https://github.com/miguel5612/MQSensorsLib -architectures=avr +architectures=avr,esp8266,esp32 license=MIT