MS1100 Gas Sensor
The CJMCU-1100 sensor is mainly used to detect VOC gases such as formaldehyde, toluene, and benzene. It is a semiconductor-type sensor product widely used in ventilation equipment, exhaust fans, air filters, wind caps, hoods, and other equipment. It has extremely high sensitivity and stability, can detect gases above 0.1ppm, and is suitable for detecting various volatile organic compounds such as formaldehyde, benzene, and xylene in the air. It is compact in size and inexpensive, and is widely used in various small household appliances.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a21n57.1.0.0.1925523cK81vXF&id=576190105535&ns=1&abbucket=0#detail Materials download link: https://pan.baidu.com/s/16hOBTKXWejzmklAeRxfmTg Extraction code: ixqv
Specifications
Operating voltage: 5V Operating current: <50uA Output method: ADC+GPIO Number of pins: 4 Pin Voltage in clean air <1V
View Materials
- AOUT: Analog output, the voltage value change corresponding to the amount of gas detected by the chip. The microcontroller can directly connect via ADC.
- DOUT: Digital output, the result of comparing the output voltage values of AOUT and the adjustable resistor. The output signal is 0 and 1.
- Adjusting the module's adjustable resistor can modify the sensitivity; the adjustable resistance value is 4K. When the voltage output by the adjustable resistor is less than AOUT, DOUT outputs high level and the LED lights up at the same time; conversely, DOUT outputs low level and the LED turns off. When needed, the module requires 3-5 minutes of preheating before performing related measurements. The correspondence between the collected voltage and formaldehyde/toluene:
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. However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_ms1100.c and bsp_ms1100.h, and rename the folder to MS1100.
Write Code
In the file bsp_ms1100.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#include "bsp_ms1100.h"
#include "stdio.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: MS1100_GPIO_Init
* Function Description: Pin initialization for MS1100 gas module
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: AO pin must have ADC function
******************************************************************/
void MS1100_GPIO_Init(void)
{
gpio_config_t flame_config = {
.pin_bit_mask = (1ULL<<MS1100_DO_PIN), // Configure pin
.mode =GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_DISABLE, // Disable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&flame_config);
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_Value
* Function Description: Calculate the average of ADC values and output
* Function Parameters: num number of samples
* Function Return: Corresponding scanned ADC value
* Author: LC
* Notes: None
******************************************************************/
unsigned int Get_ADC_Value(unsigned int num)
{
unsigned int Data = 0;
int i = 0;
for( i = 0; i < num; i++ )
{
/* Read ADC */
Data += adc1_get_raw(channel);
delay_ms(1);
}
Data = Data/num;
return Data;
}
/******************************************************************
* Function Name: Get_DO_Num
* Function Description: Read sensor detection status
* Function Parameters: None
* Function Return: 1=formaldehyde value normal 0=formaldehyde value too high
* Author: LC
* Notes: The threshold for detecting formaldehyde can be adjusted via the adjustable resistor on the module
******************************************************************/
unsigned char Get_DO_Num(void)
{
if( MS1100_DO == 1 )
{
return 1;
}
else
{
return 0;
}
}
/******************************************************************
* Function Name: Get_Adc_To_Voltage_Value
* Function Description: Convert the ADC read value to voltage value
* Function Parameters: None
* Function Return: Voltage value (unit mV)
* Author: LC
* Notes:
******************************************************************/
unsigned int Get_Adc_To_Voltage_Value(unsigned int Data)
{
return (esp_adc_cal_raw_to_voltage(Data, adc_chars));
}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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
In the file bsp_ms1100.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#ifndef _BSP_MS1100_H_
#define _BSP_MS1100_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 MS1100_AO_PIN 1
#define MS1100_DO_PIN 2
#define MS1100_DO gpio_get_level(MS1100_DO_PIN)
#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
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void MS1100_GPIO_Init(void);// Initialize
unsigned int Get_ADC_Value(unsigned int num);// Read AO value
unsigned char Get_DO_Num(void);// Read DO value
unsigned int Get_Adc_To_Voltage_Value(unsigned int Data);// Convert ADC value to voltage value
#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
45
46
47
48
49
50
51
52
53
54
55
Porting Verification
In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_ms1100.h"
void app_main(void)
{
float Voltage = 0;
float mVoltage = 0;
MS1100_GPIO_Init();
printf("start\r\n");
while(1)
{
// Collect 30 times then convert to voltage
mVoltage = Get_Adc_To_Voltage_Value( Get_ADC_Value(30) );
Voltage = mVoltage / 1000;
printf("voltage = %.3f\r\n", Voltage);
delay_ms(1000);
}
}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
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.