From c56186da8c6cc6be6fd88704e218989e6002b576 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 16 Sep 2019 18:33:05 -0500 Subject: [PATCH 1/2] nRF: Change analog reference voltage Datasheet reading explains one reason why readings might have been 10% low. --- ports/nrf/common-hal/analogio/AnalogIn.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/nrf/common-hal/analogio/AnalogIn.c b/ports/nrf/common-hal/analogio/AnalogIn.c index f20802ac98..4fcc65b582 100644 --- a/ports/nrf/common-hal/analogio/AnalogIn.c +++ b/ports/nrf/common-hal/analogio/AnalogIn.c @@ -108,5 +108,9 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { } float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { - return 3.3f; + // With internal reference, single ended input (grounded negative + // input), and a gain of 1/6 the input range will be: + // Input range = (0.6 V)/(1/6) = 3.6 V + // The AIN0-AIN7 inputs cannot exceed VDD, or be lower than VSS. (36.8) + return 3.6f; } From d39c85e3f501a1ca6ff38fb67c4adb2b7546f1c3 Mon Sep 17 00:00:00 2001 From: jepler Date: Mon, 16 Sep 2019 21:43:07 -0500 Subject: [PATCH 2/2] nRF: Change analog reference to VCC(/4) @ladyada says: "having this be adjustable (reference) would be ideal cause you can get absolute voltages but for now, VCC/4 + 4x matches every other chip :)" ... and indeed doing it this way happens to give a much more steady reading when using a VCC-referenced resistance, and so many of the simple things you'd wire up are actually VCC-referenced anyway. --- ports/nrf/common-hal/analogio/AnalogIn.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ports/nrf/common-hal/analogio/AnalogIn.c b/ports/nrf/common-hal/analogio/AnalogIn.c index 4fcc65b582..7cf6a6423b 100644 --- a/ports/nrf/common-hal/analogio/AnalogIn.c +++ b/ports/nrf/common-hal/analogio/AnalogIn.c @@ -67,8 +67,8 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { const nrf_saadc_channel_config_t config = { .resistor_p = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_6, - .reference = NRF_SAADC_REFERENCE_INTERNAL, + .gain = NRF_SAADC_GAIN1_4, + .reference = NRF_SAADC_REFERENCE_VDD4, .acq_time = NRF_SAADC_ACQTIME_3US, .mode = NRF_SAADC_MODE_SINGLE_ENDED, .burst = NRF_SAADC_BURST_DISABLED, @@ -108,9 +108,6 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { } float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { - // With internal reference, single ended input (grounded negative - // input), and a gain of 1/6 the input range will be: - // Input range = (0.6 V)/(1/6) = 3.6 V - // The AIN0-AIN7 inputs cannot exceed VDD, or be lower than VSS. (36.8) - return 3.6f; + // The nominal VCC voltage + return 3.3f; }