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:

Building the sample

Before you start building, remember to set up the environment first.

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

  1. Open a terminal window.

  2. Go to my-workspace/ncs-playground directory created in the Setting up the environment section.

    cd my-workspace/ncs-playground
    
  3. Build the sample using the west command, specifying the board (following the -b option) as dongle_nrf52840:

    west build -p always -b dongle_nrf52840 samples/button
    

    Tip

    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.

  4. After running the west build command, the build files can be found in build/zephyr.

Flashing the firmware

The sample is designed to work with the UF2 Bootloader, so that you can easily flash the sample using the UF2 Bootloader. The firmware can be found in build/zephyr with the name zephyr.uf2.

To flash the firmware, complete the following steps:

  1. Push and hold the button and plug your dongle into the USB port of your computer. Release the button after your dongle is connected. The RGB LED turns green.

  2. It will mount as a Mass Storage Device called UF2BOOT.

  3. Drag and drop zephyr.uf2 onto the UF2BOOT volume. The RGB LED blinks red fast during flashing.

  4. Re-plug the dongle and the sample will start running.

Testing

After flashing the firmware to your dongle, complete the following steps to test it:

  1. Plug the dongle into the USB port of your computer.
  2. Open up a serial terminal, specifying the correct serial port that your computer uses to communicate with the dongle:

    Open up a terminal and run:

    screen <serial-port-name> 115200
    
    1. Start PuTTY.
    2. Configure the correct serial port and click Open:

  3. Observe the output of the terminal and press the button. You should see the output, similar to what is shown in the following:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Set up button at GPIO_0 pin 18
    Set up LED at GPIO_0 pin 23
    Press the button
    Button pressed at 512377
    Button pressed at 517505
    Button pressed at 538121
    Button pressed at 571795
    Button pressed at 592077
    ...