Unable to change sample frequency from 20kHz to 10khz in ESP32-WROOM-32E Continuous ADC Mode

aas_2304
Posts: 1
Joined: Sat Oct 05, 2024 5:28 am

Unable to change sample frequency from 20kHz to 10khz in ESP32-WROOM-32E Continuous ADC Mode

Postby aas_2304 » 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.
  1. void cont_adc_config(void)
  2. {
  3. memset(result, 0xcc, EXAMPLE_READ_LEN);
  4.      uint8_t channel_num = sizeof(channel) / sizeof(adc_channel_t);
  5.     adc_continuous_handle_cfg_t adc_config = {
  6.         .max_store_buf_size = EXAMPLE_READ_LEN,         //1024,
  7.         .conv_frame_size = EXAMPLE_READ_LEN,
  8.     };
  9.     ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle));
  10.  
  11.     adc_continuous_config_t dig_cfg = {
  12.         .sample_freq_hz = 20 * 1000,
  13.         .conv_mode = EXAMPLE_ADC_CONV_MODE,
  14.         .format = EXAMPLE_ADC_OUTPUT_TYPE,
  15.     };
  16.  
  17.     adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
  18.     dig_cfg.pattern_num = channel_num;
  19.     for (int i = 0; i < channel_num; i++) {
  20.         adc_pattern[i].atten = EXAMPLE_ADC_ATTEN;
  21.         adc_pattern[i].channel = channel[i] & 0x7;
  22.         adc_pattern[i].unit = EXAMPLE_ADC_UNIT;
  23.         adc_pattern[i].bit_width = EXAMPLE_ADC_BIT_WIDTH;
  24.  
  25.         ESP_LOGI(TAG, "adc_pattern[%d].atten is :%"PRIx8, i, adc_pattern[i].atten);
  26.         ESP_LOGI(TAG, "adc_pattern[%d].channel is :%"PRIx8, i, adc_pattern[i].channel);
  27.         ESP_LOGI(TAG, "adc_pattern[%d].unit is :%"PRIx8, i, adc_pattern[i].unit);
  28.     }
  29.     dig_cfg.adc_pattern = adc_pattern;
  30.     ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg));
  31.  
  32.     adc_continuous_evt_cbs_t cbs = {
  33.         .on_conv_done = s_conv_done_cb,
  34.     };
  35.      esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_BITWIDTH_12, ESP_ADC_CAL_VAL_DEFAULT_VREF, &adc1_chars);
  36.     ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
  37. }

When I change the dig_cfg.sample_freq_hz frequency from 20kHz to 10kHz, the following error is encountering:
ADC Sample Frequency Isuue.png
ADC Sample Frequency Isuue.png (11.92 KiB) Viewed 685 times

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)
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

Who is online

Users browsing this forum: Google [Bot], nopnop2002 and 140 guests