Skip to content

Wrong sample values due to adc_clk_div #4448

Closed
@Armannas

Description

@Armannas

Basic Infos

Hardware

Hardware: ESP8266 - Wemos D1 R2 & mini
Core Version: 2.1.0(deb1901) (obtained through system_get_sdk_version() )

Description

I am trying to investigate the possiblity to use the internal ADC with sampling frequency >10kS/s (preferably 50) based on code found in #2304, for this I am using the system_adc_read_fast() function, as the Arduino AnalogRead is too slow. Using the adc_clk_div at 16, I am getting ~116kSamples, far more than I need, and I wanted to at least get lower sampling frequencies by setting the clock divider to 32. However, this causes all my measurements to have 1024 as value. This only happens when I change the clock divider higher than 16 (at 24 it also does something similar, ~900). Otherwise, at 16, the measurements give something I am 'used to' (~4/5 due to some mysterious offset).

I could also just use this sampling frequency and systematically remove every other sample to artificially reduce it, but it's dirty. Would there be a clean way to get a lower sampling frequency, either by solving the problem in the above paragraph or another method?

Mind you, I want to use this together with WiFi, however, I am only going to grab 100 measurements in bursts with some time in between. At these sampling times well below 50 ms, I would expect that the ADC reads do not interfer with the periodic necessary processes to maintain WiFi.

Settings in IDE

Arduino Version: 1.8.6 Hourly build 2018/01/25
Module: Wemos D1 R2 & mini
Flash Size: 4M(1M SPIFFS)
CPU Frequency: 80Mhz
Flash Mode: ?
Upload speed: 115200
Upload Using: SERIAL
Reset Method: ?

Sketch

#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
int i=0;
long total = 0;
long tim = 0;
//ADC_MODE(ADC_TOUT);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
}

float frequencies[100] = {0};

void loop() {
// put your main code here, to run repeatedly:
#ifdef ESP8266

uint16_t adc_addr[100];
uint16_t adc_num = 100;
uint8_t adc_clk_div = 16;

uint16_t adc_addr_warm[10];

//Serial.println(system_get_sdk_version());
// Warming up for a few iterations, because the first
// few measurements are wrong
system_adc_read_fast(adc_addr_warm, 10, adc_clk_div);
long start= micros();
system_adc_read_fast(adc_addr, adc_num, adc_clk_div);
// Normal ADC read
//for(int iter = 0; iter < adc_num; iter++) {
//system_adc_read();
//}
int tot = micros() - start;
tim += tot;

// Keep track of sampling frequencies for each iteration
// to calculate mean and deviation later
frequencies[i] = 100000000.0/tot;
total += 100000000.0/tot;
i++;

/* Print ADC measurements
for(int m = 0; m<adc_num; m++) {
Serial.print(adc_addr[m]);
Serial.print(", ");
}*/
Serial.println();
delay(10);
if(i == 100){
Serial.print("Sampling frequencies: ");
// Print sampling frequencies
for(int f = 0; f<adc_num; f++) {
Serial.print(frequencies[f]);
Serial.print(", ");
}
Serial.print("Average Sampling rate: ");
Serial.println(total/100);
Serial.print("It lasted: ");
Serial.println(tim/100);
i = 0;
tim = 0;
total = 0;
}
#endif

}

Debug Messages

// Clock divider set at 16
measurements = [4, 4, . . . , 4]

/ Clock divider set at 32
measurements = [1024, 1024, . . . , 1024]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions