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

Charging indication by LED glow

hello guys

i am trying to glow LED while my device gets charging. i have nrf52832  custom board and P0.17 connected to USB supply using voltage divider. when i plug in usb this pin 17 should be high and when i removed usb this pin sholud be low. but it is always high. what i am missing, can anyone suggest ? your help greatly appreciated.

i am doing like

#define BAT_CHG_STATE 17

if(BAT_CHG_STATE)
{
nrf_gpio_cfg_output(26);
nrf_gpio_pin_set(26);
}
else
{
nrf_gpio_pin_clear(26);
}

this functions are inside a loop and LED is glowing everytime when i am not charging the device.

thank you

Parents
  • Hi

    waht you are trying to do is

    you need to define pin 17 as input and detect the state with e.g. nrf_gpio_pin_read.

    I would suggest to watch for the state change and not read, cfg and set in a loop.

    I implemented it like this:

    void init_power_detect(){
    	nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
    	in_config.pull = NRF_GPIO_PIN_PULLUP;
    
    	err_code = nrf_drv_gpiote_in_init(DEV_PIN_CHARGE, &in_config, pin_handler_charge);
    	if(nrf_gpio_pin_read(DEV_PIN_CHARGE)){
    		NRF_LOG_DEBUG("CHARGE ON > SENSE POWER OFF\r\n");
    		nrf_gpio_cfg_sense_input(DEV_PIN_CHARGE, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    		nrf_gpio_pin_set(LED_CHARGE);
    	} else {
    		NRF_LOG_DEBUG("CHARGE OFF > SENSE POWER ON\r\n");
    		nrf_gpio_cfg_sense_input(DEV_PIN_CHARGE, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH);
    		nrf_gpio_pin_clear(LED_CHARGE);
    	}
    }
    
    void pin_handler_charge(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
    	if(pin == DEV_PIN_CHARGE)
    	{
    		switch(action){
    		case NRF_GPIOTE_POLARITY_TOGGLE:
    			if(nrf_gpio_pin_read(DEV_PIN_CHARGE)){
    			    // 1 = Charging
    				nrf_gpio_pin_set(LED_CHARGE);
    			} else {
    			    // 0 = NOT Charging
    				nrf_gpio_pin_clear(LED_CHARGE);
    			}
    			break;
    		}
    	}
    }

    Cheers

Reply
  • Hi

    waht you are trying to do is

    you need to define pin 17 as input and detect the state with e.g. nrf_gpio_pin_read.

    I would suggest to watch for the state change and not read, cfg and set in a loop.

    I implemented it like this:

    void init_power_detect(){
    	nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
    	in_config.pull = NRF_GPIO_PIN_PULLUP;
    
    	err_code = nrf_drv_gpiote_in_init(DEV_PIN_CHARGE, &in_config, pin_handler_charge);
    	if(nrf_gpio_pin_read(DEV_PIN_CHARGE)){
    		NRF_LOG_DEBUG("CHARGE ON > SENSE POWER OFF\r\n");
    		nrf_gpio_cfg_sense_input(DEV_PIN_CHARGE, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    		nrf_gpio_pin_set(LED_CHARGE);
    	} else {
    		NRF_LOG_DEBUG("CHARGE OFF > SENSE POWER ON\r\n");
    		nrf_gpio_cfg_sense_input(DEV_PIN_CHARGE, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH);
    		nrf_gpio_pin_clear(LED_CHARGE);
    	}
    }
    
    void pin_handler_charge(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
    	if(pin == DEV_PIN_CHARGE)
    	{
    		switch(action){
    		case NRF_GPIOTE_POLARITY_TOGGLE:
    			if(nrf_gpio_pin_read(DEV_PIN_CHARGE)){
    			    // 1 = Charging
    				nrf_gpio_pin_set(LED_CHARGE);
    			} else {
    			    // 0 = NOT Charging
    				nrf_gpio_pin_clear(LED_CHARGE);
    			}
    			break;
    		}
    	}
    }

    Cheers

Children
Related