mq2 gas sensor

memorekt
Posts: 1
Joined: Tue Apr 05, 2022 4:01 pm

mq2 gas sensor

Postby memorekt » Tue Apr 05, 2022 4:08 pm

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!
  1. /* ADC1 Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "driver/gpio.h"
  14. #include "driver/adc.h"
  15. #include "esp_adc_cal.h"
  16.  
  17. #define DEFAULT_VREF    1100        //Use adc2_vref_to_gpio() to obtain a better estimate
  18. #define NO_OF_SAMPLES   64          //Multisampling
  19.  
  20. static esp_adc_cal_characteristics_t *adc_chars;
  21. #if CONFIG_IDF_TARGET_ESP32
  22. static const adc_channel_t channel = ADC_CHANNEL_6;     //GPIO34 if ADC1, GPIO14 if ADC2
  23. static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
  24. #elif CONFIG_IDF_TARGET_ESP32S2
  25. static const adc_channel_t channel = ADC_CHANNEL_6;     // GPIO7 if ADC1, GPIO17 if ADC2
  26. static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
  27. #endif
  28. static const adc_atten_t atten = ADC_ATTEN_DB_0;
  29. static const adc_unit_t unit = ADC_UNIT_1;
  30.  
  31.  
  32. static void check_efuse(void)
  33. {
  34. #if CONFIG_IDF_TARGET_ESP32
  35.     //Check if TP is burned into eFuse
  36.     if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
  37.         printf("eFuse Two Point: Supported\n");
  38.     } else {
  39.         printf("eFuse Two Point: NOT supported\n");
  40.     }
  41.     //Check Vref is burned into eFuse
  42.     if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
  43.         printf("eFuse Vref: Supported\n");
  44.     } else {
  45.         printf("eFuse Vref: NOT supported\n");
  46.     }
  47. #elif CONFIG_IDF_TARGET_ESP32S2
  48.     if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
  49.         printf("eFuse Two Point: Supported\n");
  50.     } else {
  51.         printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
  52.     }
  53. #else
  54. #error "This example is configured for ESP32/ESP32S2."
  55. #endif
  56. }
  57.  
  58.  
  59. static void print_char_val_type(esp_adc_cal_value_t val_type)
  60. {
  61.     if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
  62.         printf("Characterized using Two Point Value\n");
  63.     } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
  64.         printf("Characterized using eFuse Vref\n");
  65.     } else {
  66.         printf("Characterized using Default Vref\n");
  67.     }
  68. }
  69.  
  70.  
  71. void app_main(void)
  72. {
  73.     //Check if Two Point or Vref are burned into eFuse
  74.     check_efuse();
  75.  
  76.     //Configure ADC
  77.     if (unit == ADC_UNIT_1) {
  78.         adc1_config_width(width);
  79.         adc1_config_channel_atten(channel, atten);
  80.     } else {
  81.         adc2_config_channel_atten((adc2_channel_t)channel, atten);
  82.     }
  83.  
  84.     //Characterize ADC
  85.     adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
  86.     esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
  87.     print_char_val_type(val_type);
  88.  
  89.     //Continuously sample ADC1
  90.     while (1) {
  91.         uint32_t adc_reading = 0;
  92.         //Multisampling
  93.         for (int i = 0; i < NO_OF_SAMPLES; i++) {
  94.             if (unit == ADC_UNIT_1) {
  95.                 adc_reading += adc1_get_raw((adc1_channel_t)channel);
  96.             } else {
  97.                 int raw;
  98.                 adc2_get_raw((adc2_channel_t)channel, width, &raw);
  99.                 adc_reading += raw;
  100.             }
  101.         }
  102.         adc_reading /= NO_OF_SAMPLES;
  103.         //Convert adc_reading to voltage in mV
  104.         uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
  105.         printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
  106.         vTaskDelay(pdMS_TO_TICKS(1000));
  107.     }
  108. }

rpiloverbd
Posts: 101
Joined: Tue Mar 22, 2022 5:23 am

Re: mq2 gas sensor

Postby rpiloverbd » Wed Apr 06, 2022 1:39 pm

Hi, have you tried to write the code in Arduino IDE keeping the wiring same?

esp32besbar
Posts: 14
Joined: Fri Mar 17, 2023 3:29 pm

Re: mq2 gas sensor

Postby esp32besbar » Wed May 17, 2023 4:21 pm

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.

esp32besbar
Posts: 14
Joined: Fri Mar 17, 2023 3:29 pm

Re: mq2 gas sensor

Postby esp32besbar » Thu May 25, 2023 5:44 pm

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.

Who is online

Users browsing this forum: No registered users and 106 guests