添加 MQ-2 传感器支持,初始化 ADC 通道并读取气体浓度
This commit is contained in:
3
components/MQ-2/CMakeLists.txt
Normal file
3
components/MQ-2/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "MQ-2.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_adc)
|
||||
67
components/MQ-2/MQ-2.c
Normal file
67
components/MQ-2/MQ-2.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "MQ-2.h"
|
||||
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_check.h"
|
||||
|
||||
#define MQ2_ADC_UNIT ADC_UNIT_1
|
||||
#define MQ2_ADC_CHANNEL ADC_CHANNEL_7
|
||||
#define MQ2_SAMPLE_COUNT 8
|
||||
|
||||
static const char *TAG = "MQ2";
|
||||
static adc_oneshot_unit_handle_t s_adc_handle = NULL;
|
||||
static bool s_inited = false;
|
||||
|
||||
esp_err_t mq2_init(void)
|
||||
{
|
||||
if (s_inited) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
adc_oneshot_unit_init_cfg_t init_cfg = {
|
||||
.unit_id = MQ2_ADC_UNIT,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(adc_oneshot_new_unit(&init_cfg, &s_adc_handle), TAG, "adc new unit failed");
|
||||
|
||||
adc_oneshot_chan_cfg_t chan_cfg = {
|
||||
.atten = ADC_ATTEN_DB_12,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(adc_oneshot_config_channel(s_adc_handle, MQ2_ADC_CHANNEL, &chan_cfg),
|
||||
TAG, "adc channel config failed");
|
||||
|
||||
s_inited = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mq2_read_raw(int *raw_out)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(raw_out != NULL, ESP_ERR_INVALID_ARG, TAG, "raw_out is null");
|
||||
ESP_RETURN_ON_FALSE(s_inited, ESP_ERR_INVALID_STATE, TAG, "mq2 not inited");
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < MQ2_SAMPLE_COUNT; ++i) {
|
||||
int sample = 0;
|
||||
ESP_RETURN_ON_ERROR(adc_oneshot_read(s_adc_handle, MQ2_ADC_CHANNEL, &sample),
|
||||
TAG, "adc read failed");
|
||||
sum += sample;
|
||||
}
|
||||
|
||||
*raw_out = sum / MQ2_SAMPLE_COUNT;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mq2_read_percent(float *percent_out)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(percent_out != NULL, ESP_ERR_INVALID_ARG, TAG, "percent_out is null");
|
||||
|
||||
int raw = 0;
|
||||
ESP_RETURN_ON_ERROR(mq2_read_raw(&raw), TAG, "read raw failed");
|
||||
*percent_out = (raw * 100.0f) / 4095.0f;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool mq2_is_alarm(float percent, float threshold_percent)
|
||||
{
|
||||
return percent >= threshold_percent;
|
||||
}
|
||||
37
components/MQ-2/include/MQ-2.h
Normal file
37
components/MQ-2/include/MQ-2.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ESP32-S3: GPIO8 -> ADC1_CHANNEL_7
|
||||
#define MQ2_ADC_GPIO 8
|
||||
|
||||
/**
|
||||
* @brief 初始化 MQ-2 ADC 通道
|
||||
*/
|
||||
esp_err_t mq2_init(void);
|
||||
|
||||
/**
|
||||
* @brief 读取 MQ-2 原始 ADC 值(0~4095)
|
||||
*/
|
||||
esp_err_t mq2_read_raw(int *raw_out);
|
||||
|
||||
/**
|
||||
* @brief 读取 MQ-2 归一化百分比(0~100)
|
||||
*/
|
||||
esp_err_t mq2_read_percent(float *percent_out);
|
||||
|
||||
/**
|
||||
* @brief 根据阈值判断是否疑似有害气体/烟雾超标
|
||||
*/
|
||||
bool mq2_is_alarm(float percent, float threshold_percent);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user