Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

ESP32 ADC reads random values

edited April 2 in Mongoose OS

Hi, I am trying to read an analog input on my ESP32, but looks like it just reads random values. I am using the following code to read the value.

load('api_config.js');
load('api_timer.js');
load('api_adc.js');

ADC.enable(36);
Timer.set(1000, true, function() {
  print('Voltage:', ADC.read(36));
}, null);

Also looks like by default mongoose is set to ADC_ATTEN_DB_11 so according to ESP32 docs the max input can be VDD, therefore input limitation should not be a problem in theory, although I have tried different values and still get random values. Sometimes it might even read 0.

Comments

  • nliviunliviu Romania
    edited April 3

    I can't reproduce.

    [Apr  3 07:50:46.124] Voltage: 1953 
    [Apr  3 07:50:48.124] Voltage: 1949 
    [Apr  3 07:50:50.125] Voltage: 1954 
    [Apr  3 07:50:52.124] Voltage: 1949 
    [Apr  3 07:50:54.125] Voltage: 0 
    [Apr  3 07:50:56.124] Voltage: 0 
    [Apr  3 07:50:58.125] Voltage: 0 
    [Apr  3 07:51:00.124] Voltage: 0 
    [Apr  3 07:51:02.125] Voltage: 1952 
    [Apr  3 07:51:04.124] Voltage: 1951 
    [Apr  3 07:51:06.125] Voltage: 1953 
    [Apr  3 07:51:08.125] Voltage: 1951 
    

    Pin 36 is connected to a R+R voltage divider between 3.3V and GND.
    The 0 reading is when I disconnected the pin from the divider.

    The device is a Wemos D32 Pro board.

  • StephenPStephenP United Kingdom

    Hi Invincible,

    I can reproduce, I experienced the problem just a few days ago.

    I have a voltage divider circuit

    Vin 12v
    Vout 1.2v
    R1 10k
    R2 1k

    If you just connect the output from R1 to your boards pin , the ADC behaves as a random number generator.
    You have to make a connection from R2 output to ground and your boards ground pin.
    Then the ADC behaves as expected.

    This is not a Mongoose OS issue, I experienced the problem using ESP IDF v3.1.3

    Also mentioned in the ESP IDF Documentation, the output from ADC1 is not reliable.
    They provide an example to take a number of samples, and use the average value.
    I use the mean value.

Sign In or Register to comment.