nRF ADC driver when used in single-ended mode can return negative values
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.
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.
Hi @anangl is there anyone at Nordic who can have a look at this?
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
This PR should fix the issue, I am still testing it, but it seems to be functional :) #75420 Pausing work on it now though :)
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.
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 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 :)
PR is tested and ready for review :)