ADC continuous read sample rate is not what is specified
Posted: Thu Jan 05, 2023 7:32 pm
Hello, I'm trying to use the ADC in the continuous introduced by IDF 5.0, however the sample rate that I specify in the adc_continuous_config_t does not correlate to what I actually get. Here is the code that I'm running:
This is an excerpt from the serial output:
And this is what IDF says when building:
I've tried changing the sample rate, as well as buffer sizes and the max_store_buf_size and conv_frame_size, but no combination makes the sample rates match. How can I make it more precise?
Thanks.
Code: Select all
#include <esp_adc/adc_continuous.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include <string.h>
#include <math.h>
#include <atomic>
#define SAMPLE_RATE 40000
adc_continuous_handle_t adcHandle = NULL;
std::atomic_uint32_t sampleCount;
void printSampleRateTask(void *arg)
{
uint32_t samplesPerSecond = sampleCount.exchange(0, std::memory_order_acq_rel);
printf("Time: %lld, samples per second: %lu\n", esp_timer_get_time(), samplesPerSecond);
}
extern "C" void app_main()
{
adc_continuous_handle_cfg_t handle_cfg = {
.max_store_buf_size = 2000,
.conv_frame_size = 1000,
};
ESP_ERROR_CHECK(adc_continuous_new_handle(&handle_cfg, &adcHandle));
adc_digi_pattern_config_t channel[1] = {};
channel[0].atten = ADC_ATTEN_DB_11;
channel[0].channel = 0;
channel[0].unit = 0;
channel[0].bit_width = SOC_ADC_DIGI_MIN_BITWIDTH;
adc_continuous_config_t cfg = {
.pattern_num = 1,
.adc_pattern = channel,
.sample_freq_hz = SAMPLE_RATE,
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE1,
};
ESP_ERROR_CHECK(adc_continuous_config(adcHandle, &cfg));
ESP_ERROR_CHECK(adc_continuous_start(adcHandle));
const esp_timer_create_args_t periodic_timer_args = {
.callback = &printSampleRateTask,
.name = "sampleRate",
};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1000000));
esp_err_t ret;
uint32_t ret_num = 0;
uint8_t result[1000] = {0};
while (1)
{
ret = adc_continuous_read(adcHandle, result, sizeof(result), &ret_num, 1000);
if (ret == ESP_OK)
{
sampleCount += ret_num / SOC_ADC_DIGI_RESULT_BYTES;
}
else
{
printf("status: %d\n", ret);
}
}
}
Code: Select all
Time: 1028996, samples per second: 32500
Time: 2028961, samples per second: 32500
Time: 3028961, samples per second: 33000
Time: 4028961, samples per second: 32500
Time: 5028961, samples per second: 33000
Time: 6028961, samples per second: 32500
Time: 7028961, samples per second: 33000
Time: 8028961, samples per second: 32500
Time: 9028961, samples per second: 32500
Time: 10028961, samples per second: 33000
Time: 11028961, samples per second: 32500
Time: 12028961, samples per second: 33000
Time: 13028961, samples per second: 32500
Code: Select all
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/wemos_d1_mini32.html
PLATFORM: Espressif 32 (5.3.0+sha.98b1e96) > WEMOS D1 MINI ESP32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-espidf @ 3.50000.0 (5.0.0)
- tool-cmake @ 3.16.4
- tool-esptoolpy @ 1.40400.0 (4.4.0)
- tool-mkfatfs @ 2.0.1
- tool-mklittlefs @ 1.203.210628 (2.3)
- tool-mkspiffs @ 2.230.0 (2.30)
- tool-ninja @ 1.7.1
- toolchain-esp32ulp @ 1.23500.220830 (2.35.0)
- toolchain-xtensa-esp32 @ 11.2.0+2022r1
Thanks.