ESP32-S3 ADC2电压偏差
Posted: Tue Jun 28, 2022 3:06 pm
1. 使用ESP32-S3 ADC2 CH7(GPIO18),读取校准后的电压偏低。实际是2V左右,log打印只有1.8V多。没有打开wifi,请问可能是什么问题?
2. 另外如果直接计算adc_raw/4095*Vref,这个Vref是多少
附上ADC2读取函数:
2. 另外如果直接计算adc_raw/4095*Vref,这个Vref是多少
Code: Select all
I (100531) bat_adc: adc_raw= 2238 cali_data= 1803mV
I (102531) bat_adc: adc_raw= 2238 cali_data= 1803mV
I (104531) bat_adc: adc_raw= 2236 cali_data= 1800mV
I (106531) bat_adc: adc_raw= 2236 cali_data= 1800mV
I (108531) bat_adc: adc_raw= 2238 cali_data= 1803mV
I (110531) bat_adc: adc_raw= 2237 cali_data= 1802mV
I (112531) bat_adc: adc_raw= 2237 cali_data= 1802mV
I (114531) bat_adc: adc_raw= 2237 cali_data= 1802mV
I (116531) bat_adc: adc_raw= 2237 cali_data= 1802mV
I (118531) bat_adc: adc_raw= 2237 cali_data= 1802mV
I (120531) bat_adc: adc_raw= 2235 cali_data= 1800mV
I (122531) bat_adc: adc_raw= 2239 cali_data= 1803mV
Code: Select all
//ADC Channels
#define ADC_EXAMPLE_CHAN0 ADC2_CHANNEL_7 // GPIO18
//ADC Attenuation
#define ADC_EXAMPLE_ATTEN ADC_ATTEN_DB_11
//ADC Calibration
#if CONFIG_IDF_TARGET_ESP32
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_VREF
#elif CONFIG_IDF_TARGET_ESP32S2
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
#elif CONFIG_IDF_TARGET_ESP32C3
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
#elif CONFIG_IDF_TARGET_ESP32S3
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP_FIT
#endif
static esp_adc_cal_characteristics_t adc_chars;
static bool adc_calibration_init(void)
{
esp_err_t ret;
bool cali_enable = false;
ret = esp_adc_cal_check_efuse(ADC_EXAMPLE_CALI_SCHEME);
if (ret == ESP_ERR_NOT_SUPPORTED)
{
ESP_LOGW(TAG, "Calibration scheme not supported, skip software calibration");
}
else if (ret == ESP_ERR_INVALID_VERSION)
{
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
}
else if (ret == ESP_OK)
{
cali_enable = true;
esp_adc_cal_characterize(ADC_UNIT_2, ADC_EXAMPLE_ATTEN, ADC_WIDTH_BIT_DEFAULT, 0, &adc_chars);
}
else
{
ESP_LOGE(TAG, "Invalid arg");
}
return cali_enable;
}
esp_err_t adc_init(void)
{
bool cali_enable = adc_calibration_init();
if (true != cali_enable)
{
ESP_LOGI(TAG, "adc init fail!!!");
return ESP_FAIL;
}
//ADC2 config
ESP_ERROR_CHECK(adc2_config_channel_atten(ADC_EXAMPLE_CHAN0, ADC_EXAMPLE_ATTEN));
ESP_LOGI(TAG, "adc init ok");
return ESP_OK;
}
uint16_t adc_get_voltage(void)
{
uint16_t adc_raw;
uint16_t voltage = 0;
adc2_get_raw(ADC_EXAMPLE_CHAN0, ADC_WIDTH_BIT_DEFAULT, &adc_raw);
voltage = esp_adc_cal_raw_to_voltage(adc_raw, &adc_chars);
ESP_LOGI(TAG, "adc_raw= %d cali_data= %dmV", adc_raw, voltage);
return voltage;
}