Unable to change sample frequency from 20kHz to 10khz in ESP32-WROOM-32E Continuous ADC Mode
Posted: Sat Oct 05, 2024 6:42 am
Hello, I am using ESP32-WROOM-32E in my project. I want to capture 50Hz sine wave at 10khz sample rate in continuous ADC mode. Here is my continuous ADC configuration function.
When I change the dig_cfg.sample_freq_hz frequency from 20kHz to 10kHz, the following error is encountering:
I have checked the sample frequency range of ESP32-WROOM-32E ADC in the soc_caps.h header file. The GitHub link is attached here:
https://github.com/espressif/esp-idf/bl ... soc_caps.h
Also I have calculated the number of samples to capture 1 cycle of 50Hz sine wave, theoretically its about 400 samples @ 20kHz sample rate.
But the 1 cycle is captured in ~327 samples only.
I have used following formula:
1/20000 = 50us ----------> (sampling rate)
1/50 = 20ms
Number of samples = 20ms/50us
Number of samples = 400
Please guide me to achieve and solve the following issues:
How can I achieve 10kHz sample rate in ESP32-WROOM-32E ADC Continuous Mode?
How to calculate number of sample to capture a single cycle of 50Hz sine wave?
Thank You
- void cont_adc_config(void)
- {
- memset(result, 0xcc, EXAMPLE_READ_LEN);
- uint8_t channel_num = sizeof(channel) / sizeof(adc_channel_t);
- adc_continuous_handle_cfg_t adc_config = {
- .max_store_buf_size = EXAMPLE_READ_LEN, //1024,
- .conv_frame_size = EXAMPLE_READ_LEN,
- };
- ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle));
- adc_continuous_config_t dig_cfg = {
- .sample_freq_hz = 20 * 1000,
- .conv_mode = EXAMPLE_ADC_CONV_MODE,
- .format = EXAMPLE_ADC_OUTPUT_TYPE,
- };
- adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
- dig_cfg.pattern_num = channel_num;
- for (int i = 0; i < channel_num; i++) {
- adc_pattern[i].atten = EXAMPLE_ADC_ATTEN;
- adc_pattern[i].channel = channel[i] & 0x7;
- adc_pattern[i].unit = EXAMPLE_ADC_UNIT;
- adc_pattern[i].bit_width = EXAMPLE_ADC_BIT_WIDTH;
- ESP_LOGI(TAG, "adc_pattern[%d].atten is :%"PRIx8, i, adc_pattern[i].atten);
- ESP_LOGI(TAG, "adc_pattern[%d].channel is :%"PRIx8, i, adc_pattern[i].channel);
- ESP_LOGI(TAG, "adc_pattern[%d].unit is :%"PRIx8, i, adc_pattern[i].unit);
- }
- dig_cfg.adc_pattern = adc_pattern;
- ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg));
- adc_continuous_evt_cbs_t cbs = {
- .on_conv_done = s_conv_done_cb,
- };
- esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_BITWIDTH_12, ESP_ADC_CAL_VAL_DEFAULT_VREF, &adc1_chars);
- ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
- }
When I change the dig_cfg.sample_freq_hz frequency from 20kHz to 10kHz, the following error is encountering:
I have checked the sample frequency range of ESP32-WROOM-32E ADC in the soc_caps.h header file. The GitHub link is attached here:
https://github.com/espressif/esp-idf/bl ... soc_caps.h
Code: Select all
#define SOC_ADC_SAMPLE_FREQ_THRES_HIGH (2*1000*1000)
#define SOC_ADC_SAMPLE_FREQ_THRES_LOW (20*1000)
But the 1 cycle is captured in ~327 samples only.
I have used following formula:
1/20000 = 50us ----------> (sampling rate)
1/50 = 20ms
Number of samples = 20ms/50us
Number of samples = 400
Please guide me to achieve and solve the following issues:
How can I achieve 10kHz sample rate in ESP32-WROOM-32E ADC Continuous Mode?
How to calculate number of sample to capture a single cycle of 50Hz sine wave?
Thank You