Skip to content

Broken ADC readings on esp32c3 with read_raw() and read() #468

@faulesocke

Description

@faulesocke

Test setup

  • esp32c3, connected to wifi (random dev board from aliexpress)
  • Using adc1 and gpio0
  • applying external voltage (~0.9V) to pin using lab PSU, checking voltage with good quality DMM. also have a small (couple nF) capacitor directly on the pin
  • issue reproduced on two out of two tested devices, one fresh from the packaging, so unlikely hardware issue
  • setup code:
let adc = AdcDriver::new(peripherals.adc1).unwrap();
let adc_pin = peripherals.pins.gpio0;
let adc_cfg = AdcChannelConfig::new();
let mut adc_driver = AdcChannelDriver::new(&adc, adc_pin, &adc_cfg).unwrap();

Problem

When I use read_raw() in a loop to collect 16 samples and then averaging over those

loop {
    let mut samples = [0; 16];
    for sample in &mut samples {
        *sample = adc.read(&mut adc_driver).unwrap();
    }

    // uncomment to fix:
    //info!("samples: {samples:?}");
    let sample = samples.into_iter().sum::<u16>() / samples.len() as u16;
    info!("sample: {sample}");

    thread::sleep(Duration::from_secs(1));
}

I get unreliable results. The applied voltage of 0.9V seems to max out the ADC since I see no fluctuation in the values anymore. However, the value I get is not 4095, but instead 3839, which converts to binary 0b111011111111. If I slightly change the code, and, for example log the individual sub-samples in the for-loop, or log the samples array, or add a small 10ms sleep to the for loop, then I suddenly see the correct reading of 4095.

The read() function, however, seems to behave correctly, returning reliable and accurate voltage readings. A user in IRC suggested, this might be some timing issue, that is magically right in read() all the time, but in my read_raw() setup sometimes is wrong.

If you need any more information, please ask.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions