← index #6572PR #4213
Related · high · value 0.217
QUERY · ISSUE

NRF52840: ADC is 8 bits, how to set to 12 bits?

openby JPFrancoiaopened 2020-10-25updated 2024-09-13
enhancementport-nrf

Hi,

I have been playing with the latest version of micropython on the nrf52840 USB dongle. The temperature over bluetooth example works perfectly for me (as a proof that micropython is properly flashed on the board).

I wanted to try and read some values from the ADC pins. I wired the pin 0.29 to a potentiometer, and initialized an ADC object:

from machine import ADC
adc = ADC(5)
adc.value()

However, I could only get values between 0 and 255 (0 when the potentiometer was turned to the minimum, 255 when turned to the maximum). This obviously means the ADC has an 8-bits resolution

The specs of the board tell us we can use 8, 10, 12 and even 14 bits of resolution (with oversampling): https://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.0.pdf

I wasn't able to find a method in the ADC class to set the resolution of the ADC. Is it possible in the current state of micropython for the nrf boards?

CANDIDATE · PULL REQUEST

docs: Specify new machine.ADC and machine.ADCBlock classes

mergedby dpgeorgeopened 2018-10-04updated 2022-01-21
docs

Based on the discussion in #3943 here is my proposal for a revised machine.ADC class which is intended to be implemented by all ports to make the ADC class more standardised. The changes here include a new machine.ADCBlock class as well, which is used to allow further configuration of ADC if a port supports it.

In summary the new interface is:

ADC(id, *, sample_ns, gain_mult, gain_div) # create ADC object
ADC.init(sample_ns, gain_mult, gain_div) # initialise
val = ADC.read_u16() # read value between 0-65535
val = ADC.read_v() # read voltage, float
val = ADC.read_mv() # read mV, integer

ADCBlock(id, *, bits) # create ADC peripheral object
ADCBlock.init(bits) # initialise
adc = ADCBlock.connect(channel) # connect to a channel
adc = ADCBlock.connect(source) # connect up a pin or other object
adc = ADCBlock.connect(channel, source) # connect a channel to a pin

The API is split up this way into two classes (ADC and ADCBlock) so that basic and common functionality is easily available in the ADC class, and extended functionality is in ADCBlock. A given port only needs to implement ADC if that is all it can do, and already that gives 80% of the functionality. The ADC class is the one that would be used most of the time.

If a port wants to expose more control, and the user needs to use it, then the ADCBlock class is there for that. It allows to specify the resolution, and which channels are connected to which pins.

All existing ports should be able to fit into this model without a problem. In particular the gain settings can be used by nrf and esp32. stm32 can use the sample time.

It should be possible to update the ports in a backwards-compatible way to this new API because most of the methods are new and don't clash with existing ones. ADCBlock is completely new so won't clash with anything.

There is room for extensions to this API: ADC can have additional methods like read_u24() and read_uv(), and ADCBlock might get methods to read internal channels, like voltage references and temperature.

Note: it might be easier to read the diff of this PR in split view.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied