Copyright ©

Mongoose OS Forum


How to access Espressiff SDK function system_adc_read_fast ?

I have read that theoretically all functions from Espressiff SDK should work. But how to access system_adc_read_fast ?
Please help


  • nliviunliviu Romania
    edited March 3

    You can try this (tested on Wemos D1 mini)

    #include "mgos.h"
    #ifdef __cplusplus
    extern "C" {
    #if CS_PLATFORM == CS_P_ESP8266
    #include "user_interface.h"
    #ifdef __cplusplus
    #if CS_PLATFORM == CS_P_ESP8266
    #define num_samples 8
    uint16_t adc_addr[num_samples];
    uint16_t adc_num = num_samples;
    uint8_t adc_clk_div = 8;
    static void mainTimerCB(void *arg)
        (void) arg;
        static int reading = 0;
        LOG(LL_INFO, ("Reading %d", reading++));
    #if CS_PLATFORM == CS_P_ESP8266
        system_adc_read_fast(adc_addr, adc_num, adc_clk_div);
        for (size_t i = 0; i < num_samples; ++i) {
            LOG(LL_INFO, ("sample%d=%d", i, adc_addr[i]));
    enum mgos_app_init_result mgos_app_init(void)
        mgos_set_timer(10 * 1000, 1, mainTimerCB, NULL);
        return MGOS_APP_INIT_SUCCESS;


    [Mar  3 16:44:27.888] mainTimerCB          Reading 23
    [Mar  3 16:44:27.891] mainTimerCB          sample0=10
    [Mar  3 16:44:27.894] mainTimerCB          sample1=4
    [Mar  3 16:44:27.894] mainTimerCB          sample2=4
    [Mar  3 16:44:27.898] mainTimerCB          sample3=4
    [Mar  3 16:44:27.901] mainTimerCB          sample4=4
    [Mar  3 16:44:27.903] mainTimerCB          sample5=4
    [Mar  3 16:44:27.906] mainTimerCB          sample6=4
    [Mar  3 16:44:27.909] mainTimerCB          sample7=4

    with the A0 pin connected to GND.

  • Sergey_ISergey_I Moscow

    Thanks, I will try it.
    According to your output, samples was taken about every 3 ms, which is 1/0.003= 333 Hz which is not much different from the regular adc_read.
    I take data from piezo sensor and I need fast reading. During 1 sec of measurement I should take at least 20k samples, better 40 kS to get something meaningful. And 40kSPS is 40 kHz = one sample per 25 us. The good thing is that I can switch off WiFi during measurement and just load data in RAM to send later. Maybe WiFi section interrupted your sample code

  • nliviunliviu Romania
    edited March 7

    The 3-4 ms difference in the timestamps is the time to write to the serial console.
    If you want to time the call of system_adc_read_fast, you have to do something like this:

    double start=mg_time();
    system_adc_read_fast(adc_addr, adc_num, adc_clk_div);
    double end=mg_time();
    double delta=(end-start)*1000000; // microseconds

    PS. I don't think you'll have enough RAM to store 20000*2 bytes.

Sign In or Register to comment.