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

What is the right factor to convert voltate using SAADC?

Hello,

I am getting really confused when using te SAADC to convert the read values into voltages.

I have seen that the example ble_app_proximity() uses the following macro

#define ADC_REF_VOLTAGE_IN_MILLIVOLTS     600
#define ADC_RES_10BIT                     1024   
#define ADC_PRE_SCALING_COMPENSATION      6   
ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE)\
        ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / ADC_RES_10BIT) * ADC_PRE_SCALING_COMPENSATION)

whereas the thingy example uses the following method

#define ADC_GAIN                    NRF_SAADC_GAIN1     // ADC gain.
#define ADC_REFERENCE_VOLTAGE       (0.6f)              // The standard internal ADC reference voltage.
#define ADC_RESOLUTION_BITS         (8 + (SAADC_CONFIG_RESOLUTION * 2)) //ADC resolution [bits].
static uint32_t adc_to_batt_voltage(uint32_t adc_val, uint16_t * const voltage)
{
    uint32_t err_code;
    float    adc_gain;
    uint16_t tmp_voltage;

    err_code = adc_gain_enum_to_real_gain(ADC_GAIN, &adc_gain);
    RETURN_IF_ERROR(err_code);

    float tmp = adc_val / ((adc_gain / ADC_REFERENCE_VOLTAGE) * pow(2, ADC_RESOLUTION_BITS));
    tmp_voltage =  (uint16_t) ((tmp / m_battery_divider_factor) * 1000);
    *voltage = ( (tmp_voltage + 5) / 10) * 10;  // Round the value.

    return M_BATT_STATUS_CODE_SUCCESS;
}

Which one has to be used and when?

Thanks in advance

  • No I am not using different settings and I set my gain as 1_5 and in the battery_lvl_indicator func I saw that the voltage range is from 3v is 2.1v, so that is why all the time I get 100%.because mine is from 3v to 2.7v?

    What should I change to get appropriate battery level percentage? 

  • You should set to your 1/gain setting, i.e., if you use 1_5 gain, you set this to 5.

    You need to create your own function converting the voltage in millivolts to percentage battery. This is the function used in SDK 15.0.0:

    /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
     *
     *  @details The calculation is based on a linearized version of the battery's discharge
     *           curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
     *           is considered to be the lower boundary.
     *
     *           The discharge curve for CR2032 is non-linear. In this model it is split into
     *           4 linear sections:
     *           - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
     *           - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
     *           - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
     *           - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
     *
     *           These numbers are by no means accurate. Temperature and
     *           load in the actual application is not accounted for!
     *
     *  @param[in] mvolts The voltage in mV
     *
     *  @return    Battery level in percent.
    */
    static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
    {
        uint8_t battery_level;
    
        if (mvolts >= 3000)
        {
            battery_level = 100;
        }
        else if (mvolts > 2900)
        {
            battery_level = 100 - ((3000 - mvolts) * 58) / 100;
        }
        else if (mvolts > 2740)
        {
            battery_level = 42 - ((2900 - mvolts) * 24) / 160;
        }
        else if (mvolts > 2440)
        {
            battery_level = 18 - ((2740 - mvolts) * 12) / 300;
        }
        else if (mvolts > 2100)
        {
            battery_level = 6 - ((2440 - mvolts) * 6) / 340;
        }
        else
        {
            battery_level = 0;
        }
    
        return battery_level;
    }

  • You should set to your 1/gain setting, i.e., if you use 1_5 gain, you set this to 5.

    Yes I have did that.

    how do I set the percentage drops?

  • You need to look on the discharge curve of your battery to figure that out.

Related