MQ-2 Smoke Detection Sensor
The MQ-2 smoke sensor uses tin dioxide semiconductor gas-sensitive material and belongs to the surface ion-type N-type semiconductor. At 200~3000 degrees Celsius, the surface of tin dioxide adsorbs oxygen in the air, forming oxygen anion adsorption, which reduces the electron density in the semiconductor, thereby increasing its resistance. When it contacts smoke, if the potential barrier at the grain boundary is changed by the smoke, it will cause a change in surface conductivity. Using this principle, information about the presence of smoke can be obtained. The higher the smoke concentration, the higher the conductivity, the lower the output resistance, and thus the larger the output analog signal.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.12.4cd36a4bho6MgR&id=522572009794 Materials download link: https://pan.baidu.com/s/1ETxqg03p5fEjKS7AZ2kV6w Materials extraction code: dfr1
Specifications
Operating voltage: 5V Operating current: 150MA Output method: DO interface for digital output, AO interface for analog output Read method: ADC Number of pins: 4 Pin (2.54mm pitch header)
Principle Analysis
The MQ-2 smoke sensor has high sensitivity to liquefied gas, natural gas, and city gas. Note: It must be heated for a period of time before use; otherwise, its output resistance and voltage will be inaccurate. Its detection range for combustible gases and smoke is 100~10000ppm (ppm is the volume concentration. 1ppm = 1 cubic centimeter/1 cubic meter). It has dual signal output (analog output AO and digital output DO). When the gas concentration does not exceed the set threshold, the digital interface DO outputs a low level, and the analog interface AO voltage is basically around 0V; when the gas influence exceeds the set threshold, the module digital interface DO outputs a high level, and the analog interface AO output voltage will gradually increase with the gas influence. The threshold is controlled by the adjustable resistor on the module. Its corresponding schematic diagram is shown in Figure 2.2.3.1-2. The AO output is the voltage directly output by the MQ-2 sensor, so it is analog; DO is the high/low level output after voltage comparison by LM393, so it is digital. For the specific principle, please refer to section 2.3.3.1 View Materials in the Photoresistor Light Sensor chapter.
Therefore, the DO pin can be configured as GPIO input mode, and the AO pin needs to be configured as ADC analog input mode.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_mq2.c and bsp_mq2.h, and the folder name to mq2.
Write Code
In the bsp_mq2.c file, write:
#include "bsp_mq2.h"
esp_adc_cal_characteristics_t *adc_chars;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: ADC_DMA_Init
* Function Description: Initialize ADC+DMA function
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void ADC_MQ2_Init(void)
{
adc1_config_width(width);// 12-bit resolution
//ADC_ATTEN_DB_0: indicates reference voltage is 1.1V
//ADC_ATTEN_DB_2_5: indicates reference voltage is 1.5V
//ADC_ATTEN_DB_6: indicates reference voltage is 2.2V
//ADC_ATTEN_DB_11: indicates reference voltage is 3.3V
//adc1_config_channel_atten( channel,atten);// Set channel 0 and 3.3V reference voltage
// Allocate memory
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
// Initialize ADC characteristics so that it can correctly calculate conversion results and compensation factors
esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
}
/******************************************************************
* Function Name: Get_Adc_Dma_Value
* Function Description: Calculate the average value of DMA saved data and output
* Function Parameters: CHx which scan data
* Function Return: Corresponding scanned ADC value
* Author: LC
* Notes: None
******************************************************************/
unsigned int Get_Adc_MQ2_Value(void)
{
unsigned char i = 0;
unsigned int AdcValue = 0;
/* Because it collects SAMPLES times, loop SAMPLES times */
for(i=0; i < SAMPLES; i++)
{
/* Accumulate */
AdcValue += adc1_get_raw(channel);
}
/* Calculate average */
AdcValue = AdcValue / SAMPLES;
return AdcValue;
}
/******************************************************************
* Function Name: Get_MQ2_Percentage_value
* Function Description: Read MQ2 value and return percentage
* Function Parameters: None
* Function Return: Returns percentage
* Author: LC
* Notes: None
******************************************************************/
unsigned int Get_MQ2_Percentage_value(void)
{
int adc_max = 4095;
int adc_new = 0;
int Percentage_value = 0;
adc_new = Get_Adc_MQ2_Value();
Percentage_value = ((float)adc_new/adc_max) * 100;
return Percentage_value;
}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
In the bsp_mq2.h file, write:
#ifndef _BSP_MQ2_H_
#define _BSP_MQ2_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#define DEFAULT_VREF 1100 //Default reference voltage, unit mV
#define channel ADC_CHANNEL_0 // ADC measurement channel
#define width ADC_WIDTH_BIT_12 // ADC resolution
#define atten ADC_ATTEN_DB_11 // ADC attenuation
#define unit ADC_UNIT_1 // ADC1
#define GPIO_AO 1
//Number of samples
#define SAMPLES 30
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void ADC_MQ2_Init(void);
unsigned int Get_Adc_MQ2_Value(void);
unsigned int Get_MQ2_Percentage_value(void);
#endif2
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
The porting is now complete. Please proceed to section 5.5 for porting verification.
Porting Verification
Enter the following code in main.c:
#include <stdio.h>
#include "bsp_mq2.h"
void app_main(void)
{
ADC_MQ2_Init();
printf("Start....\n");
delay_ms(1000);
while(1)
{
printf("MQ2_Value: %d\n",Get_Adc_MQ2_Value());
printf("MQ2_Percentage: %d%%\n",Get_MQ2_Percentage_value());
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (Click to Jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.