problem with GPIO pin as input with nrf9160 using zephyr RTOS

Hi,

I'm using GPIO pin as input button  with nrf9160 using Zephyr RTOS, but when I was press the button I didn't get any response from button state.

hear is my code 

 

#include <zephyr/zephyr.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/printk.h>
#include <inttypes.h>

#define SLEEP_TIME_MS	1000

#define button 6

void main(void)
{
	int ret;
	int count=0;
	
	 int gpio_dev =device_get_binding("GPIO_0");
       if(!gpio_dev)
       {
        printk("Cannot bind gpio device\n");
        return 0;
       }
	ret = gpio_pin_configure(gpio_dev,button, GPIO_INPUT);

	printk("Press the button\n");
	
		while (1)
		{
			int val = gpio_pin_get(gpio_dev,button);
			
			if (val!= 0)
			{
			count=0;
			}
			
			count++;
			printk("count =%d\n",count);
			k_msleep(SLEEP_TIME_MS);
		}

}

when I press button count  value not changing, if anyone know where I did wrong please help me to solve this.

thank you

Parents
  • Hello,

    device_get_binding returns a "const struct device" so  gpio_dev should be declared as 

    const struct device *gpio_dev =device_get_binding("GPIO_0");
    It is always good to look for returned value, so I would replace 
    	ret = gpio_pin_configure(gpio_dev,button, GPIO_INPUT);
    
    by
    	ret = gpio_pin_configure(gpio_dev,button, GPIO_INPUT);
        if (ret != 0)
        {
            printk("Error %d", ret);
            return 1;
        }
    Can you retry with those changes ? If this is still not working, make sure to have CONFIG_GPIO=y in your config and that the dts has
    &gpio0 {
      status = "okay";
    };
    
  • Hi Depraz,

    I changed my code as you said ,after changing also I'm not getting any response form my button. if I changed pin config as

    " ret = gpio_pin_configure( gpio_dev ,button, GPIO_PULL_UP); " then  its working fine in onboard  button. if I connect it to  external button  its not working. My external button is connected to nrf9160 as bellow figure.

    .

    is their any changes i have to do in my code if  I connect  button externally to board?

  • Hello Bhanu,

    As you are referring to external vs internal buttons, are you using the nRF9160DK?

    When it comes the internal buttons, could you try GPIO_PULL_UP | GPIO_ACTIVE_LOW? If you take a look at the dts file of the 9160 you see these pin hardware characteristics defined.

    You can also have a look at how this is done in the Zephyr button example, in which gpio_pin_configure_dt() is used instead of gpio_pin_configure().

    When it comes to your external button, it looks like a pull-down switch. So could you try GPIO_PULL_DOWN for that one?

    Regards,

    Elfving

  • thanks you,

    When it comes the internal buttons, could you try GPIO_PULL_UP | GPIO_ACTIVE_LOW

    now it's working for internal button.

    When it comes to your external button, it looks like a pull-down switch. So could you try GPIO_PULL_DOWN for that one?

    when it come to external  button I tried "GPIO_PULL_DOWN" then also I'm not getting any response. Even if tried GPIO_PULL_UP then also not working.

Reply Children
Related