Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Understanding the GPIOs on the nRF52

November 11, 2023

Understanding the GPIOs on the nRF52

The nRF52 is a versatile microcontroller that offers a wide range of features for building embedded systems. One of the key features is the General-Purpose Input/Output (GPIO) pins, which allow you to connect external devices and interact with them. In this blog post, we will explore the GPIO capabilities of the nRF52 and provide examples to help you understand how to utilize them effectively.

GPIO Basics

GPIO pins can be configured as either an input or an output. As an input, they can detect the voltage level of an external device, while as an output, they can drive a voltage level to control an external device. The nRF52 has a number of GPIO pins, typically labeled as GPIO0, GPIO1, and so on. The actual number and labeling may vary depending on the specific nRF52 development board you are using.

GPIO Configuration

To use a GPIO pin on the nRF52, you need to configure it. The configuration includes setting the pin as an input or output, defining the pin mode, enabling pull-up or pull-down resistors, and configuring the pin sense mechanism (event or task). Let’s see how to configure a GPIO pin using the nrfx library, which is commonly used with the nRF52:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <nrfx.h>

#define LED_PIN 13

void gpio_setup(void) {
  nrfx_err_t err_code;
  
  // Configure LED pin as output
  nrfx_gpio_cfg_output(LED_PIN);
  
  // Enable pull-down resistor on switch pin
  nrfx_gpio_cfg_input(SWITCH_PIN, NRF_GPIO_PIN_PULLDOWN);
}

In this example, we configure the LED_PIN as an output and the SWITCH_PIN as an input with a pull-down resistor enabled.

Reading GPIO Input

To read the state of an input pin, you can simply use the nrfx_gpio_pin_read() function. Here’s an example:

1
2
3
4
5
if (nrfx_gpio_pin_read(SWITCH_PIN) == NRF_GPIO_PIN_SENSE_HIGH) {
  // Switch is pressed
} else {
  // Switch is not pressed
}

In this example, we read the state of the SWITCH_PIN and take different actions based on its value.

Writing GPIO Output

To drive an output pin to a specific voltage level, you can use the nrfx_gpio_pin_write() function. Here’s an example:

1
nrfx_gpio_pin_write(LED_PIN, 1);

In this example, we set the LED_PIN to logic high, turning on the connected LED.

Toggling GPIO Output

You can also toggle the state of an output pin using the nrfx_gpio_pin_toggle() function. This is particularly useful when you want to implement blinking functionality. Here’s an example:

1
nrfx_gpio_pin_toggle(LED_PIN);

In this example, each time this line of code is executed, the state of the LED_PIN will be toggled, resulting in the LED turning on or off.

Conclusion

GPIO pins on the nRF52 provide a flexible way to interact with external devices in your embedded systems. In this blog post, we covered the basics of configuring GPIO pins, reading input values, writing output values, and toggling output values. By understanding these concepts and leveraging the nrfx library, you can unlock the full potential of the GPIO capabilities on the nRF52 microcontroller.

Remember, GPIO pins can vary between different boards and development environments, so always consult the relevant datasheets and documentation to ensure accurate pin configuration and usage for your specific setup.

Happy coding with nRF52 GPIOs!


➡️ Exploring the UART communication on the nRF52


⬅️ Getting started with the nRF52 development kit


Go back to Posts.