The following code is used to show conversion time and maximum sampling frequency. The code is from this GitHub page for a spectrum analyzer based on the ESP32. The author in his YouTube video shows that his reading is around 10 μs for the conversion time and around 100000 Hz as maximum sampling frequency.
Here is my case:
Conversion time: 93.54 uS
Max sampling frequency: 10690.92 Hz
It is 10 times slower! What is the problem?
#define AUDIO_IN_PIN 35
int analogValue;
unsigned long newTime;
void setup() {
Serial.begin(115200);
Serial.println("START");
}
void loop() {
newTime = micros();
// Do 1 million reads and record time taken
for (int i = 0; i < 1000000; i++) {
analogValue = analogRead(AUDIO_IN_PIN);
}
float conversionTime = (micros() - newTime) / 1000000.0;
Serial.println("END");
Serial.print("Conversion time: ");
Serial.print(conversionTime);
Serial.println(" uS");
Serial.print("Max sampling frequency: ");
Serial.print((1.0 / conversionTime) * 1000000);
Serial.println(" Hz");
}
Edit: Sorry my bad, I added yield() to see what happens, but nothing has changed.
Edit: I have tried the @thebusybee recommandation and the result is the same!
void loop() {
newTime = micros();
//delay(10);
//delay(100);
delay(1000);
// Do 1 million reads and record time taken
for (int i = 0; i < 1000000; i++) {
analogValue = analogRead(AUDIO_IN_PIN);
}
float conversionTime = (micros() - newTime) / 1000000.0;
Serial.println("END");
Serial.print("Conversion time: ");
Serial.print(conversionTime);
Serial.println(" uS");
Serial.print("Max sampling frequency: ");
Serial.print((1.0 / conversionTime) * 1000000);
Serial.println(" Hz");
}
No luck yet! But I have found a better solution for my case esp32_is2_adc_reading
println("START");from setup(). Serial interrupts while transmitting data during the sampling stage might slow it down. – StarCat Feb 21 '24 at 06:53delay(10);to see what happens. -- Please [edit] your question to add your then current code and results. – the busybee Feb 21 '24 at 07:26