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

GPIO does not sense button press

I have a project that uses the nRF51822_xxAA chip with a pull-up button connected to pin 9. Using a meter, I have verified that the pin drops low (0 V) when the button is pressed so I'm confident that the button hardware is working. However I get no response in the code. I have written a simple test program that also does not get any button input. The test project does not use the SoftDevice (to keep things simple) but my real project does use S110 SoftDevice. I'm using Keil as my dev system and I'm using SDK 10.0.0

I have tried several variants using app_gpio, nrf_drv_gpio, app_button, etc. Nothing seems to work.

Here is my code snippet:

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "app_gpiote.h"

#define BUTTON_0 	9		// pin 9

#define APP_GPIOTE_MAX_USERS    1

uint8_t button_state;

app_gpiote_user_id_t m_app_gpiote_my_id;

void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    NVIC_SystemReset();
}

void gpiote_event_handler(uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low)
{
    if (event_pins_high_to_low & (1 << BUTTON_0))
		button_state = true;
    
    if (event_pins_low_to_high & (1 << BUTTON_0))
		button_state = false;
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;

    nrf_gpio_cfg_sense_input(BUTTON_0, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    
    APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);    
    err_code = app_gpiote_user_register(&m_app_gpiote_my_id, (1 << BUTTON_0), (1 << BUTTON_0), gpiote_event_handler);
    APP_ERROR_CHECK(err_code);
    
    err_code = app_gpiote_user_enable(m_app_gpiote_my_id);
    APP_ERROR_CHECK(err_code);
	
    while(true)
    {
        nrf_delay_ms(500);
    }
}

I appreciate any help.

  • Hi Brent

    Would it be suitable for you to use Board Support Package to detect button presses? It should be easy to use. In e.g. ble_app_template project in nRF51 SDK 10.0.0 there is a callback function (bsp_event_handler) for the buttons on the PCA10028 board that works without modification. To get a response on a button connected to pin P0.09, just modify the BUTTON_1 configuration in the pca10028.h file to

    #define BUTTON_1       9
    

    Then when you press your custom button you should get an event BSP_EVENT_KEY_0

    Otherwise, to detect a button press in a simple way, you could look at this simple example.

  • Thanks for the feedback.

    I'm not using any of the BSP functionality for this board. It's actually a really simple board with only one button, that's why it's so confusing why this is not working.

    I did try the simple example with the same result - the button does not register as being pressed. I've also tried with the dev kit with soldered on pull up and button with the same result. I'm wondering if there is something about using pin 9??? I do have a UART working for debug information that is mapped to other pins so I know GPIO does work for this application. Anyway, I'm digging into the nordic code further to see if I can isolate where it's failing.

  • In think it should work with SDK 8.0.0, but with SDK 10.0.0, you need to use the drivers. Try the pin_change_int example in nRF51 SDK 10.0.0 which configures a single button to toggle a LED

  • Thanks for the suggestion. I tried the pin_change_init example (removing the output LED code) and it also does not work. A breakpoint set on the handler is never reached. Does anyone know if using the softdevice would cause issues here? I'm not calling the softdevice but I wonder if it may be intercepting the GPIO data??? I'm going to try it without the softdevice installed and see what happens (this didn't help on my previous experiments).

  • Perhaps there is a little misunderstanding. The pin_change_int example in the SDK does not use the softdevice, so the procedure is to erase the chip (with e.g. pressing the "Erase All" button in nRFgo Studio), and then program only the pin_change_int example (no softdevice).

    If you want to make this work with the softdevice, you should program the softdevice first in nRFgo studio (e.g. the S110 8.0.0), then flash any of the BLE examples in the SDK (for S110 if you have programmed the S110 softdevice). When you have that working, you can add the pin_exchange_int code to the BLE example.

    Most BLE examples however have already configured the Board Support Package library in order to detect button presses. In that case, you can detect button presses as described in this tutorial.

Related