<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Inconsistent ADC readings in Micropython and Arduino IDE]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto"><em>Example output:</em></p>
<pre><code class="language-csv">           Aref   width  val     V
Arduino:  3.30V 4095.00 1840 1.48V 
MicroPy:  3.30V 4095.00    0 0.00V
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">Can anyone suggest what might be the fault in my MicroPython code?</p>
<p dir="auto">Arduino IDE code:</p>
<pre><code class="language-c">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);
}
</code></pre>
<p dir="auto">Micropython code:</p>
<pre><code class="language-python">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()
</code></pre>
]]></description><link>https://community.m5stack.com/topic/4113/inconsistent-adc-readings-in-micropython-and-arduino-ide</link><generator>RSS for Node</generator><lastBuildDate>Tue, 05 May 2026 21:27:50 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4113.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 08 Mar 2022 18:25:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Inconsistent ADC readings in Micropython and Arduino IDE on Wed, 09 Mar 2022 15:38:05 GMT]]></title><description><![CDATA[<p dir="auto">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)</p>
<p dir="auto">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.</p>
<pre><code>                                                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

</code></pre>
]]></description><link>https://community.m5stack.com/post/16891</link><guid isPermaLink="true">https://community.m5stack.com/post/16891</guid><dc:creator><![CDATA[ianmackinnon]]></dc:creator><pubDate>Wed, 09 Mar 2022 15:38:05 GMT</pubDate></item></channel></rss>