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

How can I read out the actual value of the power supply, using the low power comparator?

Dear all,

I need to read out the voltage of the battery that supplies my nrf52. From what I have read I see that there are 2 options: LPCOMP and ADC.

Due to restrictions that I have because of my hardware, I cannot add any exteranl components in order to add the voltage divider. So I need to use the LPCOMP.

I have checked the SDK example, but I don't see how to actually read out the value. Searching around has only revealed to me there is a way to trigger an event when the supply voltage falls under a certain value. But what do I need to do in order to measure the value of the supply voltage?

Parents
  • Hello,

    I have checked the SDK example, but I don't see how to actually read out the value

    What example did you look at? The Proximity Application (examples\ble_peripheral\ble_app_proximity) measures the battery level every 2 minutes using ADC. If there is a change in the battery level, the application sends the current level as a notification.

    /**@brief Function for handling the ADC interrupt.
     *
     * @details  This function will fetch the conversion result from the ADC, convert the value into
     *           percentage and send it to peer.
     */
    void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            nrf_saadc_value_t adc_result;
            uint16_t          batt_lvl_in_milli_volts;
            uint8_t           percentage_batt_lvl;
            uint32_t          err_code;
    
            adc_result = p_event->data.done.p_buffer[0];
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
            APP_ERROR_CHECK(err_code);
    
            batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) +
                                      DIODE_FWD_VOLT_DROP_MILLIVOLTS;
            percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts);
    
            err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl, BLE_CONN_HANDLE_ALL);
            if ((err_code != NRF_SUCCESS) &&
                (err_code != NRF_ERROR_INVALID_STATE) &&
                (err_code != NRF_ERROR_RESOURCES) &&
                (err_code != NRF_ERROR_BUSY) &&
                (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
               )
            {
                APP_ERROR_HANDLER(err_code);
            }
        }
    }
    

    Hope this helps.

    Kind regards,
    Øyvind

Reply
  • Hello,

    I have checked the SDK example, but I don't see how to actually read out the value

    What example did you look at? The Proximity Application (examples\ble_peripheral\ble_app_proximity) measures the battery level every 2 minutes using ADC. If there is a change in the battery level, the application sends the current level as a notification.

    /**@brief Function for handling the ADC interrupt.
     *
     * @details  This function will fetch the conversion result from the ADC, convert the value into
     *           percentage and send it to peer.
     */
    void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            nrf_saadc_value_t adc_result;
            uint16_t          batt_lvl_in_milli_volts;
            uint8_t           percentage_batt_lvl;
            uint32_t          err_code;
    
            adc_result = p_event->data.done.p_buffer[0];
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
            APP_ERROR_CHECK(err_code);
    
            batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) +
                                      DIODE_FWD_VOLT_DROP_MILLIVOLTS;
            percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts);
    
            err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl, BLE_CONN_HANDLE_ALL);
            if ((err_code != NRF_SUCCESS) &&
                (err_code != NRF_ERROR_INVALID_STATE) &&
                (err_code != NRF_ERROR_RESOURCES) &&
                (err_code != NRF_ERROR_BUSY) &&
                (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
               )
            {
                APP_ERROR_HANDLER(err_code);
            }
        }
    }
    

    Hope this helps.

    Kind regards,
    Øyvind

Children
No Data
Related