This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reading Battery Level nrf52840

Hi

I want to measure the battery lithium, this battery is the principal source (3.7v 300mh), the maximum voltage is 4.2 when the charge is full,

i'm using this code for to read the analog input  y calculate the battery level 

this is for to init

void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
{
	
}	

void saadc_init(void)
{
	ret_code_t err_code;
	nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);
	
	err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
	APP_ERROR_CHECK(err_code);
	
	err_code = nrfx_saadc_channel_init(0,&channel_config);
	APP_ERROR_CHECK(err_code);
	
}

i am using the voltage devisor R1=1k and R2 =10k 

int main(void)
{
   
	log_init();
	saadc_init();
	
	
	nrf_saadc_value_t adc_val;
	
	NRF_LOG_INFO("APPlication Started!!");
	
	
    while (1)
    {
      nrfx_saadc_sample_convert(0,&adc_val);
			
			valueVoltaje = (adc_val * 4.2f / 1024)*0.91;// 1.07  calculo del parametro a utilizar para captura de voltaje de la bateria
			porcentaje = (valueVoltaje/4.2f)*100;
	}

i have a problem with the voltage calculating, its not the same multimeter measurement  

i have no idea if  i need to change some parameter or where I can see other example

this is the circuit

thank you and regards

sorry my English is not good 

Parents Reply Children
  • Hi Ketiljo

    thank you for to response

    I have a question about external refence voltage, how can i do that?,

    void saadc_init(void)
    {
    	ret_code_t err_code;
    	nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    	
      
    	
    	err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
    	APP_ERROR_CHECK(err_code);
    	
    	err_code = nrfx_saadc_channel_init(0,&channel_config);
    	APP_ERROR_CHECK(err_code);
    	
    }

     my reference voltage now is 3.3 but I need to change to 1.8 in vdd , I want to do a test with 1.8v 

    is single ended or diferential with ? 

    NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);

    nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL(NRF_SAADC_INPUT_VDD,NRF_SAADC_INPUT_AIN1);

    thank you and regards

  • You can only have VDD or the internal 0.6 V as a reference, there are no external references available, see https://infocenter.nordicsemi.com/topic/ps_nrf52840/saadc.html

    If you want to measure the battery voltage, you need to measure single ended. 

  • oohh ok, nice!

    thank you very much 

    best reagrds

  • Hi Marcos,
    I'm buiding this application on nRF52833 as well.
    I started with this topic 1 days ago
    So Can you explain for me this piece of code ? Thanh you very much

        while (1)
        {
          nrfx_saadc_sample_convert(0,&adc_val);
    			
    			valueVoltaje = (adc_val * 4.2f / 1024)*0.91;// 1.07  calculo del parametro a utilizar para captura de voltaje de la bateria
    			porcentaje = (valueVoltaje/4.2f)*100;
    	}

  • Hi john12

    in this line read and covert the analog input to bits , and pass the data to varaible "adc_val" i am using 10bits resolution

    nrfx_saadc_sample_convert(0,&adc_val); 

    this is the equation to get the voltage value, multiplicate the adc_value * Vdd( The Vdd 3.3v), devide by 1024(10 bits resolution) *  your deviser voltage for example if you have R1=800k and R2=2M

    devide R2/R1 = 2M/800k = 2.5  this is the porpotion, you can see this https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/measuring-lithium-battery-voltage-with-nrf52

    this line is wrong, 

    valueVoltaje = (adc_val * 4.2f / 1024)*0.91;

    the power supply for micro nrf52 is 1.8v to 3.6v i am using 3.6v aprox, then your maximun voltage analog input is 3.3v 

    valueVoltaje = (adc_val * 3.6f / 1024)*2.5;

    and this part

    porcentaje = (valueVoltaje/3.6f)*100;

    its for voltage battery percent

    here I little tutorial about this example 

    https://youtu.be/HfuIPiBgTfg

    sorry , my English is not good xd 

    but I hope to help you

Related