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

Battery level detect in nRF52832

Hi,

Does nRF52832 use normal ADC input to detect battery level (ex. AIN0, AIN1...)? or nRF52832 have other method to do this job?

Thank you,

Chianglin

Parents Reply Children
  • Hi,

    Thank you for your explain.

    VDD is define in "H7" and "A7" pin of nRF52832, it is used as power input of MCU. Does it also can detect battery level?

    Thank you,

  • chianglin said:
    VDD is define in "H7" and "A7" pin of nRF52832, it is used as power input of MCU. Does it also can detect battery level?

    Yes, but only if you have connected VDD directly to the battery, as mentionned in my initial reply.

  • Hi,

    If I want to use two ADC at the same time, one for "ADC Sensor input", and the other one for "Battery level detect".  How can I modify following source code?

    void saadc_init(void)
    {
        ret_code_t err_code;
    
    	// ---------- Initialize ADC for Pressure-Sensor ----------------
    	// 變更 ADC 的 bit 數
    	nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
    	saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
    
        nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
        // 變更 ADC 倍率 & 參考電壓
    	channel_config.gain = NRF_SAADC_GAIN1_4;
    	channel_config.reference = NRF_SAADC_REFERENCE_VDD4;
    
    	err_code = nrf_drv_saadc_init(&saadc_config, saadc_Sensor_callback);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
    	// ---------- Initialize ADC for Battery detect ----------------
    #if 1
        err_code = nrf_drv_saadc_init(NULL, saadc_Battery_event_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
        err_code = nrf_drv_saadc_channel_init(0, &config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[0], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[1], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    #endif
    }

    System will crash when "second nrf_drv_saadc_init() execute".

    Thank you

  • Hi,

    chianglin said:
    System will crash when "second nrf_drv_saadc_init() execute".

    That is expected. You should only initialize the driver once unless you disable it in between (for instance to save power). So you should remove the second call to nrf_drv_saadc_init(), and use a common event handler for both. If you sample either one or the other, you will know which it is based on what you just sampled. If you sample both at the same time, you will know based on the order. (sampling both at the same time may not be that useful since you probably don't need battery information as often as you need sensor input).

    chianglin said:
    If I want to use two ADC at the same time, one for "ADC Sensor input", and the other one for "Battery level detect".  How can I modify following source code?

    To sample both channels in consecutive order, just enable the second channel as well, and make sure the result buffer is a multiple of the number of channels. If you want to sample either one or the other, and both at a low rate, then it is most power-efficient to configure sampling of the first channel (sensor), do that, and then uninitialized the SAADC. Then do the same when it is time to sample the other channel (VDD).

  • Hi,

    According to your instructions, does follow source code is correct?

    void saadc_init(void)
    {
        ret_code_t err_code;
    
    	// Modify ADC resolution
    	nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
    	saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
    
    	// Initialize ADC
    	err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
        APP_ERROR_CHECK(err_code);
    
    	// ---------- Initialize ADC for Pressure-Sensor ----------------
        nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    	channel_config.gain = NRF_SAADC_GAIN1_4;
    	channel_config.reference = NRF_SAADC_REFERENCE_VDD4;
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
    	// ---------- Initialize ADC for Battery detect ----------------
        nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
        err_code = nrf_drv_saadc_channel_init(1, &config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[0], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[1], SAADC_SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    }
    

    How can I modify saadc_callback() function to read two different ADC channel?

    Would you please give me a “saadc_callback()" sample code?

    Thank you

Related