Skip to content

nRF52 Programming Tutorials – Part 3 Controlling LEDs & Reading Switch Inputs

From part 2 of the NRF Getting started article we learned about Nordic SDK, in part 3 of the NRF Getting started series we will learn about controlling LEDs and reading switch inputs.

The NRF 52 DK has four buttons and four LEDs that are connected to dedicated I/Os on the nRF52832 chip.

  • For both switches and LEDs, the pin number has been assigned by nordic. in order for the user to control the LEDs, the pin numbers have to be defined first.

Ex:  #define LED_PIN1 17

#define SWITCH_PIN1 13

The led pin number is as follows: –

  • LED_PIN1  17
  • LED_PIN2  18
  • LED_PIN3  19
  • LED_PIN4  20

The switch pin number is as follows: –

  • SWITCH_PIN1 13
  • SWITCH_PIN2 14
  • SWITCH_PIN3 15
  • SWITCH_PIN4 16
  • The basic working template of the program will be to control the LEDs using buttons, meaning when the user presses the button1, led1 will be on and when released led will go off.

The procedure is as follows :- The steps involved in the process will be put up in the main():

  • Configure the switches as input

The syntax for the process is nrf_gpio_cfg_input (SWITCH_PIN1, NRF_GPIO_PIN_PULLUP);

SWITCH_PIN1, NRF_GPIO_PIN_PULLUP are the parameters passed in the cfg_input.

  • The procedure remains the same for the next three buttons/switches.
  • The next step will be to configure LED as output
  • The procedure remains the same for the next three buttons/switches.
  • The next step will be to configure LED as output
  • The syntax for this is nrf_gpio_cfg_output(LED_PIN1);
  • The LED_PIN1 is the pin defined earlier in the program.
  • The next steps will be to toggle the LEDs.
  • The syntax for this process will be
    • nrf_gpio_pin_clear(LED_PIN1);
    • nrf_gpio_pin_clear() will turn on the LED
  • The rest of the pins have to be configured the same way.
  • The statements to be written next will have to be put in an infinite loop() for continuous execution.
  • Next, we will have to read the switch input using the read syntax
    • nrf_gpio_pin_read(switch pin1)
  • Then we will have to set the led pin low, the syntax for that is: nrf_gpio_pin_set(LED_PIN1);
  • The above steps of reading, setting and clearing the pins as to be put up in an if-else condition for decision making.
 while (true)
 if(nrf_gpio_pin_read(switch pin1))
  • Similarly, all the switch pin has to be read and LED pins to have to be cleared and set for turning on and off the  LEDs based on the switch pressed.

Leave a Reply

Your email address will not be published. Required fields are marked *