analogRead poor performance compared to arduino IDE

Hi guys, quite new to platform.io and to the community, so I hope this is the right place to ask.
I’m working on ESP32 nano board, using VSCode and PIO extension.
I encounter significant performance issues with analogRead.

I have minimized the code sample to the following:

#include <WiFi.h>

const char* ssid = "your_SSID";       // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi password

void setup() {
    Serial.begin(115200);
    pinMode(13, INPUT);

    // Turn on WiFi
    Serial.println("Connecting to WiFi...");
    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    
    Serial.println("\nWiFi Connected!");
}

void loop() {
    unsigned long startTime = micros(); // Use micros() for better precision
    int vv = analogRead(13);
    unsigned long elapsedTime = micros() - startTime;

    Serial.print("ADC Value: ");
    Serial.print(vv);
    Serial.print(" | ADC Time: ");
    Serial.print(elapsedTime);
    Serial.println(" us");
}

In a simple sketch using arduino IDE the performance I observe is about 1-2 micro seconds.
In my pio project, I encounter approximately 6500 micro seconds (x6500!!).

  • I have removed all libraries from my project and copy-pasted the exact same code in my main.cpp file
  • my board version (in board manager):
  • my platformio.ini file:
[env:main-unit]
platform = espressif32
board = arduino_nano_esp32
framework = arduino
platform_packages =
    framework-arduinoespressif32

upload_flags = 
  --serial=DCDA0C22E848 ;

count_tasks_for_parallel_build = 40

monitor_speed = 115200
build_type = release
build_flags =
    -D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000   ; (keep default)
    -D CONFIG_ASYNC_TCP_PRIORITY=10         ; (keep default)
    -D CONFIG_ASYNC_TCP_QUEUE_SIZE=64       ; (keep default)
    -D CONFIG_ASYNC_TCP_RUNNING_CORE=1      ; force async_tcp task to be on same core as Arduino app (default is any core)
    -D CONFIG_ASYNC_TCP_STACK_SIZE=4096     ; reduce the stack size (default is 16K)
    ; -D CORE_DEBUG_LEVEL=5
    ; -g3
    ; -ggdb

lib_compat_mode = strict
lib_ldf_mode = chain

I’d be more than happy to get your inputs as it seems I miss something significant here.

Thanks for the guidance!

How? You can’t use an ADC2 pin while the WiFi is operating.

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

Thanks for the reference, it’s indeed seems the issue :pray: