Some problems encountered when using ESP32S3 ADC1

YONPON
Posts: 5
Joined: Mon Sep 09, 2024 6:03 am

Some problems encountered when using ESP32S3 ADC1

Postby YONPON » Tue Sep 17, 2024 2:34 pm

When using ADC1:
① When I configure reading the raw data of ADC_CHANNEL_3, ADC_CHANNEL_4, ADC_CHANNEL_5, ADC_CHANNEL_6, the readings are normal;
② When I configure reading the raw data of ADC_CHANNEL_2, ADC_CHANNEL_7, ADC_CHANNEL_8, ADC_CHANNEL_9, there is an initial value in the first ADC_CHANNEL of the configured channel array. If I input an analog signal on this channel's pin, the value of the analog signal will be added to this initial value;
③ When I configure all eight ADC channels at the same time, the same thing happens. There is an initial value in the first ADC_CHANNEL of the channel array. If I input an analog signal on this channel's pin, the value of the analog signal will be added to this initial value;
④ Additionally, I don't know why, in my tests, ADC_CHANNEL_8 and ADC_CHANNEL_9 cannot read the analog signal I input. I don't know if these two channels can be directly used as AD resources. The official documentation does not mention anything special.

These are my current questions. Please give reasonable suggestions. Thank you very much🙏
Below is my code.

Code: Select all

void MacADC_Process(void *arg)
{
    esp_err_t ret;
    uint32_t ret_num = 0;
    int FF = 0;
    uint8_t result[EXAMPLE_READ_LEN] = {0};
    uint8_t channel_num = sizeof(channel) / sizeof(adc_channel_t);
    memset(result, 0xcc, EXAMPLE_READ_LEN);

    s_task_handle = xTaskGetCurrentTaskHandle();

    adc_continuous_handle_t handle = NULL;
    continuous_adc_init(channel, sizeof(channel) / sizeof(adc_channel_t), &handle);

    adc_continuous_evt_cbs_t cbs = {
        .on_conv_done = s_conv_done_cb,
    };
    ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
    adc_cali_handle_t adc1_cali_handle = NULL;
    bool do_calibration1 = example_adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_2_5, &adc1_cali_handle);

    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(200));
        ESP_ERROR_CHECK(adc_continuous_start(handle));
        vTaskDelay(pdMS_TO_TICKS(200));
        while (1)
        {
            ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0);
            if ((ret == ESP_OK) && (EXAMPLE_READ_LEN == ret_num))
            {
                memset(ADCSum, 0, sizeof(ADCSum));
                for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES)
                {
                    adc_digi_output_data_t *p = (void *)&result[i];
                    uint32_t chan_num = EXAMPLE_ADC_GET_CHANNEL(p);
                    uint32_t data = EXAMPLE_ADC_GET_DATA(p);
                    ADCSum[GetChannelIndex(chan_num)] += data;
                }
                if (do_calibration1)
                {
                    for (int i = 0; i < channel_num; i++)
                    {
                        // ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, ADCSum[i] / FILTERNUM, &AIn_V[i]));
                        ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, ADCSum[i] / FILTERNUM, &AIn_Vv[FF][i]));
                    }
                    FF++;
                    if (FF == MIDNUM)
                    {
                        for (int k = 0; k < channel_num; k++)
                        {
                            AIn_V[k] = 0;
                            for (int i = 0; i < MIDNUM && channel[k] != ADC_CHANNEL_0; i++)
                            {
                                AIn_V[k] += AIn_Vv[i][k];
                            }
                        }
                        FF = 0;
                    }
                }
            }
            else if (ret == ESP_ERR_TIMEOUT)
            {
                ESP_LOGE(TAG, "ADC No Data");
                break;
            }
            vTaskDelay(pdMS_TO_TICKS(20));
        }
        ESP_ERROR_CHECK(adc_continuous_stop(handle));
        ESP_LOGE(TAG, "ADC ReStart");
        vTaskDelay(pdMS_TO_TICKS(10000));
    }
    ESP_ERROR_CHECK(adc_continuous_stop(handle));
    ESP_ERROR_CHECK(adc_continuous_deinit(handle));
    ESP_LOGE(TAG, "ADC End");
}

MicroController
Posts: 1605
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Some problems encountered when using ESP32S3 ADC1

Postby MicroController » Wed Sep 18, 2024 7:29 am

Your code both registers the on_conv_done callback and tries a polling read with 0 timeout. Not completely sure on this, but I'd expect the driver to consider the available data consumed when the callback returns.
Try 1. not registering a callback and 2. calling adc_continuous_read() with a reasonable timeout.

YONPON
Posts: 5
Joined: Mon Sep 09, 2024 6:03 am

Re: Some problems encountered when using ESP32S3 ADC1

Postby YONPON » Thu Sep 19, 2024 12:47 am

MicroController wrote:
Wed Sep 18, 2024 7:29 am
Your code both registers the on_conv_done callback and tries a polling read with 0 timeout. Not completely sure on this, but I'd expect the driver to consider the available data consumed when the callback returns.
Try 1. not registering a callback and 2. calling adc_continuous_read() with a reasonable timeout.
I have an idea why, I used channels 8 and 9 of ADC1, and when these two channels printed the log using the official example, they found that channels 0 and 1 were printed out, and after deleting these two channels in my project, the reading returned to normal and there was no initial value interference.

Who is online

Users browsing this forum: Bing [Bot] and 94 guests