zephyr icon indicating copy to clipboard operation
zephyr copied to clipboard

nRF ADC driver when used in single-ended mode can return negative values

Open nixward opened this issue 1 year ago • 2 comments

Describe the bug When measuring voltages near 0V the nRF ADC driver when used in single-ended mode can return negative values.

See discussion: https://github.com/zephyrproject-rtos/zephyr/pull/70277#discussion_r1551337884

Expected behavior Single-ended mode should only output values >= 0.

nixward avatar Apr 05 '24 07:04 nixward

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

github-actions[bot] avatar Jun 09 '24 00:06 github-actions[bot]

Hi @anangl is there anyone at Nordic who can have a look at this?

nixward avatar Jun 29 '24 12:06 nixward

I'm on the case :) Managed to reproduce the issue quite easily, -10mV on the input, and I see negative values.

The following is near 0V

ADC sequence reading [161]:
- adc@40007000, channel 0, 5 sequence samples:
- - 80 = 0mV
- - 62 = 0mV
- - 101 = 0mV
- - 24 = 0mV
- - 70 = 0mV

The following is near -10mV:

ADC sequence reading [139]:
- adc@40007000, channel 0, 5 sequence samples:
- - 61440 = 60mV
- - 61440 = 60mV
- - 61440 = 60mV
- - 61440 = 60mV
- - 61440 = 60mV

bjarki-andreasen avatar Jul 03 '24 16:07 bjarki-andreasen

This PR should fix the issue, I am still testing it, but it seems to be functional :) #75420 Pausing work on it now though :)

bjarki-andreasen avatar Jul 05 '24 13:07 bjarki-andreasen

Please notice that sample adc_sequence has this bug https://github.com/zephyrproject-rtos/zephyr/issues/75517 which will lead to invalid conversion to millivolts.

nordic-piks avatar Jul 05 '24 13:07 nordic-piks

Let me add some comments here: I checked adc samples: adc_dt and adc_sequence. None of them, nor the driver automatically, do the calibration. Offset is not "removed" before sampling, thus values near 0 can easily flip to the negative side. I did a simple change and enabled calibration at adc_dt sample. After that change, readings were much closer to 0 (if single-ended input was shorted to GND). Still, a flip into negative happens, but it was only one bit. Maybe the approach should be to enable (or force calibration) and only then convert negative values into 0? (converting negatives into 0 without calibration - i.e. without "removing" offset - will make accuracy worse) @anangl @kl-cruz @nika-nordic What do you think?

nordic-piks avatar Jul 05 '24 19:07 nordic-piks

@nordic-piks Calibrating the ADC before reading can help, but noise, or simply applying a negative voltage to the positie input will produce a negative reading, the PR fixing the issue explains why :)

bjarki-andreasen avatar Jul 06 '24 02:07 bjarki-andreasen

PR is tested and ready for review :)

bjarki-andreasen avatar Jul 08 '24 06:07 bjarki-andreasen