This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ADC print value accuracy?

Hi,

I am programming the NRF51-DK on mbed compiler. I have compiled some code below for using an analog to digital converter. I was able to print a voltage measurement but I'm not sure if it is accurate. I am trying to use a 10 bit ADC and configure it so it reads the voltage at pin P01 (AnalogInput1), but I get a voltage reading around 2.89 even when there is no voltage supplied.

Thanks.

#include "mbed.h"
DigitalOut led_status(LED1);
Serial device (p9,p11); //(TX,RX)
void my_analogin_init(void)
{
    NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
    NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |                                //Configure Resolution to 10 bits
                      (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |       //Configure ADC input selection and prescalar settings
                                                                                                    //Internal reference voltage of 1.2V. Onethird prescaling -> 3.6V 
                      (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |                            //ADC reference selection to internal 1.2V VBG 
                      (ADC_CONFIG_PSEL_AnalogInput1 << ADC_CONFIG_PSEL_Pos) |                           //Select Pin to be used as ADC input pin -> Disable analog pins
                      (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);                      //Select external reference pin -> No external reference (we use internal reference of 1.2V)
}

uint16_t my_analogin_read_u16(void)
{
                      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if input pin has been selected
                      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput1 << ADC_CONFIG_PSEL_Pos;                         // 
                      NRF_ADC->TASKS_START = 1;
                      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //
                      return (uint16_t) NRF_ADC->RESULT;                                                            //10bit result is returned to main function
                      
}
int main(void)
{
    float value;
    
    device.baud(115200); //Set Baud rate
    my_analogin_init(); //Initialize ADC settings
    
    while(1) {
        led_status = 0;  // Led = off
        wait(0.2);       // wait 200 ms
        led_status = 1;  //led = on
        wait(0.8);       //wait 800ms 
        
        value = (float)my_analogin_read_u16(); //call function to read read voltage returns value from 0 to 1024
        value = (value * 3.6) / 1024;  // returns voltage from range of (0 to 3.6V)
        device.printf("Input Voltage: %f\n\r",value); //Prints input voltage
        }
        
}
Related