- /* ADC1 Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "driver/adc.h"
- #include "esp_adc_cal.h"
- #define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
- #define NO_OF_SAMPLES 64 //Multisampling
- static esp_adc_cal_characteristics_t *adc_chars;
- #if CONFIG_IDF_TARGET_ESP32
- static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
- static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
- #elif CONFIG_IDF_TARGET_ESP32S2
- static const adc_channel_t channel = ADC_CHANNEL_6; // GPIO7 if ADC1, GPIO17 if ADC2
- static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
- #endif
- static const adc_atten_t atten = ADC_ATTEN_DB_0;
- static const adc_unit_t unit = ADC_UNIT_1;
- static void check_efuse(void)
- {
- #if CONFIG_IDF_TARGET_ESP32
- //Check if TP is burned into eFuse
- if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
- printf("eFuse Two Point: Supported\n");
- } else {
- printf("eFuse Two Point: NOT supported\n");
- }
- //Check Vref is burned into eFuse
- if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
- printf("eFuse Vref: Supported\n");
- } else {
- printf("eFuse Vref: NOT supported\n");
- }
- #elif CONFIG_IDF_TARGET_ESP32S2
- if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
- printf("eFuse Two Point: Supported\n");
- } else {
- printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
- }
- #else
- #error "This example is configured for ESP32/ESP32S2."
- #endif
- }
- static void print_char_val_type(esp_adc_cal_value_t val_type)
- {
- if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
- printf("Characterized using Two Point Value\n");
- } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
- printf("Characterized using eFuse Vref\n");
- } else {
- printf("Characterized using Default Vref\n");
- }
- }
- void app_main(void)
- {
- //Check if Two Point or Vref are burned into eFuse
- check_efuse();
- //Configure ADC
- if (unit == ADC_UNIT_1) {
- adc1_config_width(width);
- adc1_config_channel_atten(channel, atten);
- } else {
- adc2_config_channel_atten((adc2_channel_t)channel, atten);
- }
- //Characterize ADC
- adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
- esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
- print_char_val_type(val_type);
- //Continuously sample ADC1
- while (1) {
- uint32_t adc_reading = 0;
- //Multisampling
- for (int i = 0; i < NO_OF_SAMPLES; i++) {
- if (unit == ADC_UNIT_1) {
- adc_reading += adc1_get_raw((adc1_channel_t)channel);
- } else {
- int raw;
- adc2_get_raw((adc2_channel_t)channel, width, &raw);
- adc_reading += raw;
- }
- }
- adc_reading /= NO_OF_SAMPLES;
- //Convert adc_reading to voltage in mV
- uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
- printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
- vTaskDelay(pdMS_TO_TICKS(1000));
- }
- }
mq2 gas sensor
mq2 gas sensor
Hello all, I am using an example sketch (adc1_example_main.c) from idf framework. I am using `ADC_CHANNEL_0` (not channel 6 like in sketch) which I presume corresponds to the "VP" pin on the esp32 wroom. I am trying to read the analog output from the mq2 sensor but nothing changes in terms of readings. I get a stagnant "Raw: 4095 Voltage 1063mV" reading from the sketch. The levels drop if i change the pin to an incorrect one so I assume everything is connected properly. Please if anyone might have some insight I would be so thankful!
-
- Posts: 101
- Joined: Tue Mar 22, 2022 5:23 am
Re: mq2 gas sensor
Hi, have you tried to write the code in Arduino IDE keeping the wiring same?
-
- Posts: 14
- Joined: Fri Mar 17, 2023 3:29 pm
Re: mq2 gas sensor
Hi Memorek,
Did you finally manage to make the MQ2 sensor working, i.e. outputting coherent voltage values?
I read your post as I am also trying to make the MQ2 sensor working with esp-idf.
i found this post interesting : https://lastminuteengineers.com/mq2-gas ... -tutorial/
it explains that "the sensor must be fully warmed up for 24-48 hours to ensure maximum accuracy. During the warm-up period, the sensor typically reads high and gradually decreases until it stabilizes."
I am currently observing it, at start the sensor was outputting nearly 3V (reading high). 15 minutes later it is near to 2.4V.
I will let you know tomorrow how much I will be reading, as well as if it correctly reacts to smoke.
I used the same sketch as you do and I am using ADC_CHANNEL_3 on ESP32 adafruit feather huzzah, which is pin A3/GPIO39.
Would be nice to hear your experience with this sensor since last year.
Did you finally manage to make the MQ2 sensor working, i.e. outputting coherent voltage values?
I read your post as I am also trying to make the MQ2 sensor working with esp-idf.
i found this post interesting : https://lastminuteengineers.com/mq2-gas ... -tutorial/
it explains that "the sensor must be fully warmed up for 24-48 hours to ensure maximum accuracy. During the warm-up period, the sensor typically reads high and gradually decreases until it stabilizes."
I am currently observing it, at start the sensor was outputting nearly 3V (reading high). 15 minutes later it is near to 2.4V.
I will let you know tomorrow how much I will be reading, as well as if it correctly reacts to smoke.
I used the same sketch as you do and I am using ADC_CHANNEL_3 on ESP32 adafruit feather huzzah, which is pin A3/GPIO39.
Would be nice to hear your experience with this sensor since last year.
-
- Posts: 14
- Joined: Fri Mar 17, 2023 3:29 pm
Re: mq2 gas sensor
Here are my observations with MQ2 gas sensor:
I am using a ESP32 Adafruit huzzah and ESP-IDF v5.01.
Sensor works better at 5V than at lower working voltage. It seems to be much more responsive and sensitive at 5V than when powered with a LiPo battery.
When powered with USB at DC5V lowest ADC raw value I got with this sensor is about 750. It raises easily at 4095 if you release some gas nearby.
In order to calibrate this sensor, having a look at datasheet and at arduino library (MQUnifiedsensor) is very useful.
What I understand from datasheet is when sensor is in fresh air (condition of calibration), the ratio Rair/Ro = 9.83.
Rair is a equivalent measure of the sensor resistance in fresh air and calculated as follows: (MAX ADCvalue - ADCair) / ADCair.
Replacing MAX ADCvalue by 4095 and ADCvalue by 750 in fresh air results in a Ro value of 2.27 Ohm.
Then I calculate in gas (outside of calibration condition) the ratio Rgas / Ro = (MAX ADCvalue - ADCgas) / ADCgas / Ro.
From graph 2 in datasheet, you have the correspondance between this ratio and gas concentration in ppm.
There is power regression correspondance being:
Gas in ppm = a * ratio ^ b
a and b are available in MQUnifiedsensor library. It is also easy (but boring) to perform calculation by yourself, in Excel for instance.
for MQ2 sensor,
/*
power 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
*/
This MQ2 sensor is actually well sensitive to Butane and Propane (LPG gas) and H2, also to CH4, smoke, Carbon Monoxide but to a lesser extent (to my understanding).
It is great to monitor gas leakage at home for instance.
I bought then a MQ7 sensor, very sensitive this time to Carbon Monoxide and H2 and to a far lesser extent to LPG.
I carried the same calibration process, with this time a ratio in fresh air of 27.5.
MQ7
/*
power regression:
GAS | a | b
H2 | 69.014 | -1.374
LPG | 700000000 | -7.703
CH4 | 60000000000000 | -10.54
CO | 99.042 | -1.518
Alcohol | 40000000000000000 | -12.35
*/
Woulb be nice to hear anyone's experience with these sensors.
I am using a ESP32 Adafruit huzzah and ESP-IDF v5.01.
Sensor works better at 5V than at lower working voltage. It seems to be much more responsive and sensitive at 5V than when powered with a LiPo battery.
When powered with USB at DC5V lowest ADC raw value I got with this sensor is about 750. It raises easily at 4095 if you release some gas nearby.
In order to calibrate this sensor, having a look at datasheet and at arduino library (MQUnifiedsensor) is very useful.
What I understand from datasheet is when sensor is in fresh air (condition of calibration), the ratio Rair/Ro = 9.83.
Rair is a equivalent measure of the sensor resistance in fresh air and calculated as follows: (MAX ADCvalue - ADCair) / ADCair.
Replacing MAX ADCvalue by 4095 and ADCvalue by 750 in fresh air results in a Ro value of 2.27 Ohm.
Then I calculate in gas (outside of calibration condition) the ratio Rgas / Ro = (MAX ADCvalue - ADCgas) / ADCgas / Ro.
From graph 2 in datasheet, you have the correspondance between this ratio and gas concentration in ppm.
There is power regression correspondance being:
Gas in ppm = a * ratio ^ b
a and b are available in MQUnifiedsensor library. It is also easy (but boring) to perform calculation by yourself, in Excel for instance.
for MQ2 sensor,
/*
power 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
*/
This MQ2 sensor is actually well sensitive to Butane and Propane (LPG gas) and H2, also to CH4, smoke, Carbon Monoxide but to a lesser extent (to my understanding).
It is great to monitor gas leakage at home for instance.
I bought then a MQ7 sensor, very sensitive this time to Carbon Monoxide and H2 and to a far lesser extent to LPG.
I carried the same calibration process, with this time a ratio in fresh air of 27.5.
MQ7
/*
power regression:
GAS | a | b
H2 | 69.014 | -1.374
LPG | 700000000 | -7.703
CH4 | 60000000000000 | -10.54
CO | 99.042 | -1.518
Alcohol | 40000000000000000 | -12.35
*/
Woulb be nice to hear anyone's experience with these sensors.
Who is online
Users browsing this forum: No registered users and 106 guests