Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analogReference function to switch between default VDD/4 reference and internal reference for ADC. #1876

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

stuthedew
Copy link

@stuthedew stuthedew commented Aug 3, 2019

Problem

The default analog reference for nRF52840-based boards (Xenon, Argon, Boron) uses VDD/4. This is fine for stable input voltage (usb), but super not great if you're running off a battery as the input voltage (and therefor reference voltage) decreases over time. The nRF52840 does have an internal 0.6v reference that would solve this problem, but the ability to use it is not currently implemented in the Particle OS. This is pretty annoying in a device that markets itself as a battery-based sensor node.

Solution

I've implented an Arduino-style AnalogReference function, that allows you to select the internal 0.6v reference instead of the default VDD/4 if desired.

Steps to Test

user/tests/hal/adc/test_adc.cpp

Example App

#include "Arduino.h"

const int16_t ANALOG_READ_PIN = A0;
int analog_value;

void setup() {
 pinMode(ANALOG_READ_PIN, INPUT);
 Serial.begin(9600);
}

void loop() {
 analog_value = analogRead(ANALOG_READ_PIN); //get value using default VDD/4 reference.
 Serial.print("Analog Value (VDD/4 reference): ");
 Serial.println(analog_value);

 delay(250);

 analogReference(INTERNAL);  // use INTERNAL 0.6v reference 
 analog_value = analogRead(ANALOG_READ_PIN); //get value using default VDD/4 reference.
 Serial.print("Analog Value (Internal 0.6v reference): ");
 Serial.println(analog_value);

 analogReference(AR_DEFAULT);  // switch back to default VDD/4 ADC reference 

 delay(1000);
}

References

https://community.particle.io/t/internal-voltage-reference-for-accurate-voltage-calibration/6344
https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v12.0.0%2Fgroup__nrf__drv__saadc.html
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/


Completeness

  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

Added Arduino-style AnalogReference() function to change the nRF52840 voltage reference from VDD/4 or the internal 0.6v reference. Takes 1 argument. Either "VDD4" or "INTERNAL."
@m-mcgowan m-mcgowan added the realm/embedded-platform Embedded Platform label Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
realm/embedded-platform Embedded Platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants