Chat freely about anything...
User avatar
By yourbro
#73062 I need to use
Code: Select allsystem_adc_read_fast (uint16 *adc_addr, uint16 adc_num, uint8 adc_clk_div)
to get sampling rate close to 100ksps. And I am able to achieve that using adc_clk_div = 32.

However, when I print out the values stored in adc_addr, I always get 1024. Why is this?

I have a NodeMCU v3. I have connected an electret microphone to it. +ve terminal is connected to 3.3V through 10k; GND is connected to GND. +ve terminal is connected to A0 on NodeMCU.
Image

It works fine when I use system_adc_read() (except the sampling rate is then 10ksps which is not desirable).

This is the code I'm using:

Code: Select all// Expose Espressif SDK functionality - wrapped in ifdef so that it still
// compiles on other platforms
#ifdef ESP8266
    extern "C" {
        #include "user_interface.h"
    }
#endif

#define num_samples 256
uint16_t adc_addr[num_samples];
uint16_t adc_num = num_samples;
uint8_t adc_clk_div = 32;

int i = 0;
unsigned long start = 0;
unsigned long total = 0;
unsigned long tim = 0;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
}

void loop() {
    // put your main code here, to run repeatedly:
    #ifdef ESP8266
        //Serial.println(system_get_sdk_version());
        start = micros();
        // uint16 *adc_addr : point to the address of ADC continuously fast sampling output.
        // uint16 adc_num : sampling number of ADC continuously fast sampling, range [1, 65535].
        // uint8 adc_clk_div: ADC working clock = 80M/adc_clk_div, range [8, 32], the recommended value is 8.
        wifi_set_opmode(NULL_MODE);
        system_soft_wdt_stop();
        ets_intr_lock( ); //close interrup
        noInterrupts();

        system_adc_read_fast(adc_addr, adc_num, adc_clk_div);

        unsigned int tot = micros() - start;

        interrupts();
        ets_intr_unlock(); //open interrup
        system_soft_wdt_restart();

        tim += tot;
        total += num_samples * 1000000.0 / tot;
        i++;

        if (i == 100) {
            // Serial.print("Sampling rate: ");
            // Serial.println(total / 100);
            // Serial.print("It lasted: ");
            // Serial.println(tim / 100);

            i = 0;
            tim = 0;
            total = 0;

            for (int j=0; j<adc_num;  j++) {
                Serial.println(adc_addr[j]);
            }
        }
    #endif
}
User avatar
By lethe
#73072 I think your circuit is wrong. The ESP's ADC will always be saturated by the supply voltage, thus it cannot detect the ripple created by the electred microphone.

You need a capacitor between the microphone and the ADC to block the DC supply voltage.
A capacitor with about 1uF should work, for details on to calculate the capacitor see http://www.ti.com/lit/ug/tidu765/tidu765.pdf (Section 2.2).
User avatar
By schufti
#73089 One has to consider the internal circuit on the nodemcu which is a votage divider to accomodate the real ADC pin to 3.2V. It consists of 100k to gnd and 220k to A0 usually.

Did you measure the voltage present on pin A0 with the mic covered?