digidotcom / xbee-micropython

MIT License
72 stars 30 forks source link

ADC Xbee #78

Closed l6l6 closed 2 years ago

l6l6 commented 2 years ago

Hello,

I have the module Xbee3 and the resolution is 10 bits. Using the ADC module of Micropython, I want to use the "ADC.width(bit)" and set it to 10 bits. But it seems it isn't yet implemented. Will it be included in future releases?

mikewadsten commented 2 years ago

Hi,

From reviewing the recent upstream MicroPython docs, and looking at the related pull requests (such as micropython/micropython#7788), it appears the only MicroPython implementation which supports configurable width is the ESP32.

XBee 3 does not have configurable ADC width -- it is defined by hardware as 12 bits -- so no, the ADC.width method will not be implemented. However, to get a 10-bit value equivalent to setting the width to 10 bits, would it suffice to divide the reading by 4/right-shift by 2? This would downscale the reading to effectively 10 bits.

>>> adc = machine.ADC(1)
>>> reading = adc.read()
>>> reading_10bit = (adc.read() >> 2)
l6l6 commented 2 years ago

Hello,

Thank you for the quick response! I ended up inheriting from the initial class and tweaking the method.