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

Simplest way to read an ADC input

Dear All, I would like a simple way to read the voltage on one ADC pin without using any "adc_event_handler"-like solution. I tried

reading=nrf_adc_convert_single(NRF_ADC_CONFIG_INPUT_2);

but without success.

Thanks

Francesco

Parents
  • First of all, event-driven ADC is recommended, so that the CPU can be turned off during inactive periods. But if you don’t want that, here is a very simple way to read the ADC without using any events, using the nrf_drv_adc_sample_convert function:

    #include "nrf.h"
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "nrf_drv_adc.h"
    #include "nordic_common.h"
    #include "boards.h"
    #include "nrf_log.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    
    
    static nrf_adc_value_t       adc_value; /**< ADC buffer. */
    static nrf_drv_adc_channel_t m_channel_config = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2); 
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        UNUSED_RETURN_VALUE(NRF_LOG_INIT());
        NRF_LOG_PRINTF("ADC example\r\n");
    
        while (true)
        {
            nrf_drv_adc_sample_convert(&m_channel_config,&adc_value);
            NRF_LOG_PRINTF("ADC buffer: %d\r\n",adc_value);
            nrf_delay_ms(100);
        }
    }
    /** @} */
    
  • Thanks Sigurd, how long does it take to have a measurement ready? Can I shorten the nrf_delay_ms() to 10ms (of course after removing the UART)?

Reply Children
No Data
Related