Skip to content

Button

Overview

A simple button sample demonstrates the use of GPIO input with interrupts. The sample prints a message to the console each time a button is pressed.

Requirements

Before you start, check that you have the required hardware and software:

  • 1x nRF9151 Connect Kit
  • 1x USB-C Cable
  • A computer running macOS, Ubuntu, or Windows 10 or newer

Building the sample

To build the sample, follow the instructions in Getting Started Guide to set up your preferred building environment.

Use the following steps to build the Button sample on the command line.

  1. Open a terminal window.

  2. Go to NCS-Project/nrf9151-connectkit repository cloned in the Getting Started Guide.

  3. Build the sample using the west build command, specifying the board (following the -b option) as nrf9151_connectkit/nrf9151.

    west build -p always -b nrf9151_connectkit/nrf9151 samples/button
    

    The -p always option forces a pristine build, and is recommended for new users. Users may also use the -p auto option, which will use heuristics to determine if a pristine build is required, such as when building another sample.

    Tip

    You can also build this sample for the nrf9151_connectkit/nrf9151/ns target. In such a case, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M).

    west build -p always -b nrf9151_connectkit/nrf9151/ns samples/button
    
  4. After building the sample successfully, the firmware with the name merged.hex can be found in the build directory.

Flashing the firmware

Connect the nRF9151 Connect Kit to the computer with a USB-C cable:

Then flash the sample using west flash:

west flash

Tip

In case you wonder, the west flash will execute the following command:

pyocd load --target nrf91 --frequency 4000000 build/merged.hex

Testing

After programming the sample, test it by performing the following steps:

  1. Open up a serial terminal, specifying the correct serial port that your computer uses to communicate with the nRF9151 SiP:

    1. Start PuTTY.
    2. Configure the correct serial port and click Open:

    Open up a terminal and run:

    screen <serial-port-name> 115200
    

    Open up a terminal and run:

    screen <serial-port-name> 115200
    
  2. Press the DFU/RST button to reset the nRF9151 SiP.

  3. Press the USR/P25 button and observe the output of the terminal. You should see the output, similar to what is shown in the following:

    Terminal
    *** Booting nRF Connect SDK v2.9.99-98a5e50b9ac1 ***
    *** Using Zephyr OS v3.7.99-693769a5c735 ***
    Set up button at gpio@842500 pin 25
    Press the button
    Button pressed at 500938
    Button pressed at 524745
    Button pressed at 539674
    Button pressed at 548661
    Button pressed at 562823
    ...