ESP32-S3-DEVKITC ADC Pins Error (Question)

Andrew123098
Posts: 3
Joined: Thu Jan 06, 2022 10:50 pm

ESP32-S3-DEVKITC ADC Pins Error (Question)

Postby Andrew123098 » Thu Apr 14, 2022 5:02 pm

Hi All,
I have a few different ESP32-S3-DEVKITC-1-N8 boards and I am trying to read analog data using the 2 SAR ADCs. I have a simple program (pasted below) but for some reason, each ADC channel has different voltage outputs that range from about 50mV to 930 mV when connected to absolutely nothing. As in the pins have no circuit attached to them (should be reading 0 volts). I thought I was crazy until I tried it on a few other boards to the same end. Is there something I am doing wrong in my code? Has anyone else experienced issues with the ESP32-S3 ADC channels behaving erratically? I am programing in VSCode using the ESP-IDF 4.4 CMD to flash my code and using the following commands to flash.
>> idf.py set-target esp32s3
>> idf.py -p COM4 -b 480600 flash monitor

Code: Select all

#include <stdio.h>
#include <driver/adc.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_adc_cal.h"

void app_main(void)
{   
    printf("Hello world!\n");

  
    /* ADC Configuration and Callibration Documentation: |
    ------------------------------------------------------
        VRef of the ESP32-S3 is 1100 mV                  |
                                                         
        Right Channel: GPIO 4 ADC1 Channel 3             |
        Left Channel: GPIO 11 ADC2 Channel 0             |             

        ADC Attenuation Options:                         |
        ADC_ATTEN_DB_0   : 0 - 950 mV                    |
        ADC_ATTEN_DB_2_5 : 0 - 1250 mV                   |
        ADC_ATTEN_DB_6   : 0 - 1750 mV                   |
        ADC_ATTEN_DB_11  : 0 - 3100 mV                   |

        ADC Accuracy Options:                            |
        ADC_WIDTH_9Bit                                   |
        ADC_WIDTH_10Bit                                  |
        ADC_WIDTH_11Bit                                  |
        ADC_WIDTH_12Bit                                  |
        ADC_WIDTH_BIT_DEFAULT (Max Bit Width)            |
    -----------------------------------------------------*/

    // Configure desired precision and attenuation for ADC pins
    adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
    adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_0);
    adc2_config_channel_atten(ADC2_CHANNEL_3,ADC_ATTEN_DB_0);

    // Create ADC channel characteristics structs for use in calibration functions
    
    esp_adc_cal_characteristics_t adc1_chars;
    esp_adc_cal_characteristics_t adc2_chars;
    esp_adc_cal_characterize(ADC_UNIT_1,ADC_ATTEN_DB_0,ADC_WIDTH_BIT_DEFAULT,1100,&adc1_chars);
    esp_adc_cal_characterize(ADC_UNIT_2,ADC_ATTEN_DB_0,ADC_WIDTH_BIT_DEFAULT,1100,&adc2_chars);

    int val1;
    int val2;
    int counter = 0;
    while(true){

        val1 = adc1_get_raw(ADC1_CHANNEL_0);
        adc2_get_raw(ADC2_CHANNEL_3,ADC_WIDTH_BIT_DEFAULT,&val2);


        // printf("%d, %d\n",val1,val2);
            

        int adc1_voltage = esp_adc_cal_raw_to_voltage(val1,&adc1_chars);
        int adc2_voltage = esp_adc_cal_raw_to_voltage(val2,&adc2_chars);
        printf("\n%d, %d",adc1_voltage,adc2_voltage);
        vTaskDelay(10);
        counter++;
    }
}

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-S3-DEVKITC ADC Pins Error (Question)

Postby ESP_Sprite » Fri Apr 15, 2022 2:07 am

An unconnected pin is not 0, it's undefined: it'll pick up any EM floating around and the level will be more-or-less random. If you need a 0, use a pulldown.

TestIt
Posts: 1
Joined: Wed Aug 24, 2022 11:03 pm

Re: ESP32-S3-DEVKITC ADC Pins Error (Question)

Postby TestIt » Wed Aug 24, 2022 11:06 pm

The fact that an unconnected analog input pin has its value set by the ambient environment makes it useful as a random number generator (but not so much for the purpose of security).

Who is online

Users browsing this forum: No registered users and 308 guests