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

Struggling with basic GPIO read on NRF52

Hello, I am trying to develop on the NRF52. I have the development board and have been spending time with the SDK and documentation. I understand how the GPIO should work, but I can't seem to get it to do a basic read.

I have a five position switch attached to pins 8, 9, and 10. Trying to detect when the pins are high.

I'm using the LED's on the Dev board as debugging tools.


//5 Position Switch Pins
#define SWITCH_0 8
#define SWITCH_1 9
#define SWITCH_2 10

//5 position switch value
uint32_t fiveposition = 0;

static void gpio_init(void)
{
		//initialize the 5 Position Switch GPIO
		nrf_gpio_cfg_sense_input(SWITCH_0, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
		nrf_gpio_cfg_sense_input(SWITCH_1, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
		nrf_gpio_cfg_sense_input(SWITCH_2, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);

		//turn on GPIO Nested Vector Inturrupt
		//NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk;
    //NVIC_EnableIRQ(GPIOTE_IRQn);
	
		//Setup LED's for debugging 5 position switch
		nrf_gpio_cfg_output(BSP_LED_0);
		nrf_gpio_cfg_output(BSP_LED_1);
		nrf_gpio_cfg_output(BSP_LED_2);
		nrf_gpio_cfg_output(BSP_LED_3);
		nrf_gpio_pin_clear(BSP_LED_0);
		nrf_gpio_pin_clear(BSP_LED_1);
		nrf_gpio_pin_clear(BSP_LED_2);
		nrf_gpio_pin_clear(BSP_LED_3);

}

int main(void)
{
		gpio_init();
	
    while (true)
    {
				uint32_t switch0 = 0;
				uint32_t switch1 = 0;
				uint32_t switch2 = 0;
				switch0 = nrf_gpio_pin_read(SWITCH_0);
				switch1 = nrf_gpio_pin_read(SWITCH_1);
				switch2 = nrf_gpio_pin_read(SWITCH_2);
			
				if (switch0 == 1) {
					nrf_gpio_pin_set(BSP_LED_0);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_0);
				}
				
				if (switch1 == 1) {
					nrf_gpio_pin_set(BSP_LED_1);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_1);
				}
				
				if (switch2 == 1) {
					nrf_gpio_pin_set(BSP_LED_2);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_2);
				}			
    }
		
}
  • Without seeing your schematic, this is just a guess but have you confused GPIO port pin numbers with the physical pin numbers on the package (8,9,10)?

    Dan

  • I assume the development board you have is the nRF52 DK or nrf52 Preview DK? If so there are a few issues with your code:

    • The switch numbers should be 13, 14 and 15. (You could use the BSP header file as you do with LED's, and use BUTTON_1 etc.)
    • You should enable the internal pullup resistors, not the pulldown's.

    For a real application you would probably be better off using events/interrupts via the GPIOTE driver instead of reading the input pins in your main loop. You could also use PPI which would allow you to control the LED's using buttons without any involvement from the CPU (except the initial configuration).

    (I converted this from a answer to a comment)

  • Why do you say the switch numbers should be 13, 14 and 15 when he states that he has a 5-position switch attached to pins 8, 9 and 10? He doesn't say he's trying to detect the buttons on the board, he's trying to detect his own switch. And you can't really say whether it should be a pull-up or a pull-down since he doesn't state whether the common terminal on the 5-position switch is V+ or GND. If it's GND then he should change it to a pullup, if it's V+ however, then the pulldown is correct.

  • You are right RK. I must have jumped to the code before I had read all the text. My answer is based on this misunderstanding, and reflects what would have to be done in order to use the buttons on the DK. I do not see anything wrong with the code as long as the pin numbers and pull is correct.

  • Me neither - perhaps the original poster can confirm what the 5-position switch common terminal is connected to (V+ or GND) and also confirm the exact pins on the devkit they attached it to to make sure those pins are the right ones for 8, 9 and 10. This is so simple it's hard to understand what the problem is.

    Also how about trying a very low-tech approach of putting a multimeter on the pin you're trying to read and ensuring it is at V+.

Related