PlatformIO / Esp32 ADC use a div:2 clock while Arduino IDE use div:1, so ADC becomes too slow

I’ve developped a Morse decoder program that run perfectly well when compiled with ArduinoIDE.
I migrated under VSCode / PlatformIO, and the ADC is much more slow when compiled with PlatformIO.
My board is a NodeMCU-32S, and i noticed that at boot time, the program compiled with PlatformIO says : mode:DIO, clock div:2
while the same program compiled in ArduinoIDE says :mode:DIO, clock div:1

As a result, the ADC conversion are 2 times slower under PlatformIO.

I’ve tried to force the adc_clock_div from inside my setup() function using this:
esp_err_t errCode = adc_set_clk_div(1);
but it seems that this call has no effect at all. i tried div=1, div=4 run this code in setup()
// Measure sampling_freq
int tStartLoop = millis();
int cpt = 0;
while ( (millis() - tStartLoop) < 4000) { testData[0] = analogRead(A0); cpt++;}
sampling_freq = cpt / 4; // Measured at Startup on NodeMCU-32S

but the results is always the same, around 10118 samples / s, and do not change even if div=4 ???

Furthermore, depending on the code i write in the loop(), the setup() function measure from 10118 downto 7500 samples / s ??? I really don’t understand why different code in the loop() (code that is not related to the ADC), make those changes in ADC speed measured in setup() function!!!

How can i get rid of this boot time div:2 set by PlatformIO and get a normal div:1 and no need to try to set adc clock div in my program ???

Is there something that can be changed somewhere so that under PlatformIO, the ADC clock div stays at 1 like on ArduinoIDE ??

Thanks for your help.

We just need two things:

  1. A screenshot of the Arduino IDE → Tools menu where we see your selected board and other settings
  2. Your current platformio.ini.

Arduino IDE menu screenshoot showing NodeMCU-32S used board

And my platform.ini file :

And if you need it, you can download the program on my Github : GitHub - Christian-ALLEGRE/CWDecoder-AI: Morse decoder using AI

In PlatformIO, the default flash frequency for board = nodemcu-32s is

whereas in the Arduino IDE, you have set 80MHz.

As per documentation, add

board_build.f_flash = 80000000L

to the platformio.ini and recheck the boot messages for the divider.

Hi, thanks for your quick reply.
It is a little bit better with this change in platformio.ini file.
I get now 10086 sam/s instead of 9000, but i don’t get the expected 11450 samp/s .

Could you please download my program from github ?

At line 222, I added 2 defines, that show how code, that has nothing to do with ADC, make the number of sample varying from 9000 to 11450, depending on the fact that you compile or not the function clearIfNotChanged.

When the code is not compiled, i get 11496 samp/s, and when it is compiled, i get only 9000 samp/s
And of course, i would like the code in this function de be compiled.

To test it, you just have to compile with one or the other define.

In this program, you can search for “ADC sped problem”. I explain in the setup() function what has changed since i created this topic.

Thanks for your help.

Sorry. I forget to say that now, with your proposed change in platformIO.ini file, i get div:1 at boot time.

Just for info, i recompiled my program in ArduinoIDE, and whatever the #define you activate (compile code or not compile it), i get the same ADC speed able to give me 11246 sams/s. So, ArduinoIDE do not have the problem that i encounter with PlarformIO.

Great. Then the speed at which code and data is fetched from the flash should be on-par now.

What exact Arduino-ESP32 version are you using in the Arduino IDE? Please go to Tools → Boards → Board Manager → ESP32 and read off the version. Is it maybe 2.0.14 or 3.0.0 Alpha?

I’m using V2.0.13

Well PlatformIO only has 2.0.11 or the latest 2.0.14. Explicitly use

platform = espressif32@6.5.0

in the platformio.ini . Does it change anything? Does it change with version @6.4.0?