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

NRF51822 measure input voltage

Hi,

I am trying to use an nrf51822 with a sensor that outputs value between 0-5V. What I am confused about is how to read input voltage on the beacon. I saw some people using ADC, but there was no clear instruction on how exactly to do it. Maybe you could provide clear instructions on how to configure a pin as an input pin correctly, set up the ADC and read the voltage, let's say, every 100ms? This would be very helpful.

Looking forward to hearing from you. Thanks in advance.

Regards, Robert

  • void ADC_IRQHandler(void)
    {
        if (NRF_ADC->EVENTS_END != 0)
        {
            uint8_t     adc_result;
            uint32_t    err_code;
    
            NRF_ADC->EVENTS_END     = 0;
            adc_result              = NRF_ADC->RESULT;
            NRF_ADC->TASKS_STOP     = 1;
    
    	change_minor(adc_result);
    			
            {
                APP_ERROR_HANDLER(err_code);
            }
    				
        }
    }
    static void adc_start(void)
    {
        uint32_t err_code;
    
        // Configure ADC
        NRF_ADC->INTENSET   = ADC_INTENSET_END_Msk;
        NRF_ADC->CONFIG     = (ADC_CONFIG_RES_8bit                        << ADC_CONFIG_RES_Pos)     |
                              (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)  |
                              (ADC_CONFIG_REFSEL_VBG                      << ADC_CONFIG_REFSEL_Pos)  |
                              (ADC_CONFIG_PSEL_Disabled                   << ADC_CONFIG_PSEL_Pos)    |
                              (ADC_CONFIG_EXTREFSEL_None                  << ADC_CONFIG_EXTREFSEL_Pos);
        NRF_ADC->EVENTS_END = 0;
        NRF_ADC->ENABLE     = ADC_ENABLE_ENABLE_Enabled;
    
        // Enable ADC interrupt
        err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_nvic_EnableIRQ(ADC_IRQn);
        APP_ERROR_CHECK(err_code);
    
        NRF_ADC->EVENTS_END  = 0;    // Stop any running conversions.
        NRF_ADC->TASKS_START = 1;       
    }
    
  • Example you found is really ancient. If you are using keil, download the newest packs, there is a nice nrf_adc driver in "drivers" section. There is also example in SDK that uses that driver, but it uses interrupts which you actually don't need. You can just trigger (by app_timer) blocking single conversions on specified analog input.

    see this

  • Thanks Wojtek, I made it work :) You were very helpful suggesting this. Thanks again and success!

Related