Inconsistent ADC readings in Micropython and Arduino IDE



  • I have very simply circuit with the M5StickC-Plus reading the divided 3.3V voltage at the mean point between two equal resistors (ie. at ~1.6V) on GPIO pin 36.

    When I read the analogue voltage with code written in the Arduino IDE (using ESP32 Board v1.0.6) I get a reasonable result, but when I attempt to do the same thing in MicroPython (using UIFlow_StickC_Plus-v1.9.3-plus.bin) with the same circuit I get a value of 0.

    Example output:

               Aref   width  val     V
    Arduino:  3.30V 4095.00 1840 1.48V 
    MicroPy:  3.30V 4095.00    0 0.00V
    

    In both instances I have attempted to set the appropriate width and attenuation for the ADC, and to set shared pin 25 to be floating.

    Can anyone suggest what might be the fault in my MicroPython code?

    Arduino IDE code:

    void setup() {
      Serial.begin(9600);
      analogReadResolution(12);
      analogSetWidth(12);
      analogSetAttenuation(ADC_11db);
    }
    
    void loop() {
      float adcVoltage = 3.3;
      float adcMax = 4095.0;
      int value = analogRead(36);
      float voltage = value * (adcVoltage / adcMax);
      Serial.print(adcVoltage);
      Serial.print("V ");
      Serial.print(adcMax);
      Serial.print(" ");
      Serial.print(value);
      Serial.print(" ");
      Serial.print(voltage);
      Serial.print("V ");
      Serial.println("");
      delay(500);
    }
    

    Micropython code:

    import time
    from machine import Pin, ADC
    
    
    def main():
        Pin(25, mode=Pin.IN, pull=None)
        adc = ADC(Pin(36, mode=Pin.IN))
    
        adc.atten(ADC.ATTN_11DB)
        adc.width(ADC.WIDTH_12BIT)
    
        adc_voltage = 3.3
        adc_max = 4095.0
    
        while True:
            value = adc.read()
            voltage = value * (adc_voltage / adc_max)
            print("%4.2fV %7.2f %d %4.2fV" % (
                adc_voltage, adc_max, value, voltage))
            time.sleep(0.5)
    
    
    main()
    


  • Results seem to be very inconsistent between different versions of MicroPython. Running the exact same code in some it works as expected and in others the results are wrong (full results below)

    My guess is either the code to set attenuation is not working correctly in some versions, or there is some other factor to do with the ADC that is set differently by default on some versions.

                                                    Aref   width  val     V
    Arduino IDE
    esp32 Version 1.0.6                            3.30V 4095.00 1840 1.48V 
    
    UIFlow_StickC_Plus-v1.9.3-plus.bin
    MicroPython ae8b2b72a-dirty on 2022-03-04;     3.30V 4095.00    0 0.00V
    M5StickC-Plus with ESP32
    
    UIFlow_StickC_Plus-v1.7.2-plus.bin
    MicroPython 34c92e7c8 on 2021-01-22:           3.30V 4095.00    0 0.00V
    M5StickC-Plus with ESP32
    
    uiflow-b061698-esp32-spiram-4mb-20220304.bin
    MicroPython b061698 on 2022-03-04:             3.30V 4095.00 1849 1.49V
    M5STACK with ESP32(SPIRAM)
    
    github.com/micropython/micropython/
    MicroPython ade2720 - 2022-03-09               3.30V 4095.00 1850 1.49V
    ESP32 module with ESP32
    
    github.com/m5stack/M5Stack_MicroPython
    MicroPython ESP32_LoBo_v3.2.24 - 2018-09-06    3.30V 4095.00  142 0.11V
    ESP32 board with ESP32