Adsense HTML/JavaScript

Sunday, December 6, 2020

nanoESP32-S2/CircuitPython exercise: ADC (Analog to Digital Converter)

This exercise run on nanoESP32-S2/CircuitPython, read analog input from IO1, and convert to voltage. Then display on Mu editor's Plotter.

import time
import os
import microcontroller
import board
import analogio

adc01=analogio.AnalogIn(board.IO1)

print("==============================")
print(os.uname())
print("Hello nanoESP32-S2/CircuitPython ADC example")
print("cpu.temperature: " + str(microcontroller.cpu.temperature))
print()

ref = adc01.reference_voltage
print("reference voltage: " + str(ref))

while True:
    value01=adc01.value
    voltage = ref * value01/65535
    print((ref, voltage))
    time.sleep(0.5)

print("- bye -\n")


remark:
As shown in the video, the ADC measured voltage cannot reached 3.3V.
 
I checked the voltage on IO1, is 3.3V. But the returned value of adc01 (analogio.AnalogIn(board.IO1)) is 51871, correspond 2.6V.
 

To get the reference voltage used by the ADC, e can read the property analogio.AnalogIn.reference_voltage.

For ADC values in CircuitPython you’ll find they’re all put into the range of 16-bit unsigned values. This means the possible values you’ll read from the ADC fall within the range of 0 to 65535 (or 2^16 - 1).

(ref: CircuitPython Basics: Analog Inputs & Outputs > Analog to Digital Converter (Inputs))


Next:

1 comment:

  1. I've noticed this ceiling on the value too on a FeatherS2. There's a lot of discussion about this disappointing 2.6V limitation on the ESP32-S2 in the comments in https://github.com/adafruit/circuitpython/issues/3785 - still unclear if it's a hardware or software limitation.

    ReplyDelete