← index #6572PR #17616
Related · high · value 0.334
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

Zephyr: Add ADC support.

mergedby bikeNomadopened 2025-07-05updated 2025-10-13
port-zephyr

Summary

This PR adds support for ADC peripherals in the Zephyr port.

As is typical for Zephyr, the ADC channel setup is done in the devicetree (typically using an overlay).

This code requires ADC channels to be listed in the io-channels property of the zephyr,user root node like this:

/ {
	zephyr,user {
		io-channels = <&adc 0>, <&adc 1>, <&adc 4>, <&adc 5>, <&adc 7>;
	};
};

&adc {
	#address-cells = <1>;
	#size-cells = <0>;

	channel@0 {
		reg = <0>;
		zephyr,gain = "ADC_GAIN_1_6";
		zephyr,reference = "ADC_REF_INTERNAL";
		zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
		zephyr,input-positive = <NRF_SAADC_AIN0>; /* P0.02 */
		zephyr,resolution = <12>;
	};
       // other channels defined here (1, 4, 5, 7)
};

Testing

I have tested this on the Seeed Studio XIAO BLE NRF52840 SENSE board.

Trade-offs and Alternatives

This code may be enabled and disabled by the usual means in mpconfigport.h:

#define MICROPY_HW_ENABLE_ADC (1)
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/zephyr/machine_adc.c"
#define MICROPY_PY_MACHINE_ADC_READ (1)
#define MICROPY_PY_MACHINE_ADC_READ_UV (1)

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