Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
R. Scott Coppersmith
Published © MIT

Basic GPIO on the Nordic Thingy:91 X

The Nordic Thingy:91 X is a prototyping platform with nRF9151 SiP, nRF5340 SoC, nRF7002 Wi-Fi, and plenty of sensors to play with.

BeginnerProtip2 hours201
Basic GPIO on the Nordic Thingy:91 X

Things used in this project

Hardware components

Thingy:91 X
Nordic Semiconductor Thingy:91 X
×1
Segger J-Link EDU mini
×1

Software apps and online services

nRF Connect SDK
Nordic Semiconductor nRF Connect SDK
nRF Cloud
Nordic Semiconductor nRF Cloud
VS Code
Microsoft VS Code

Story

Read more

Code

Thingy:91 X Blinky + Button

C/C++
Button 1 changes the LED color from blue to red
/*
 * Blinky + Button RSC build 24 April 2025
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   100

/* The devicetree node identifier for the "ledx" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define LED1_NODE DT_ALIAS(led1)
/* Get the node identifier for button 1 through its alias sw0 */
#define SW0_NODE DT_ALIAS(sw0)
/* Get the device pointer, pin number, and pin's configuration flags through gpio_dt_spec
 */
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);

int main(void)
{
	int ret;
	

	if (!gpio_is_ready_dt(&led0)) {
		return 0;
	}
	if (!gpio_is_ready_dt(&led1)) {
		return 0;
	}
	if (!device_is_ready(button.port)) {
		printk("Error: button device %s is not ready\n",
		       button.port->name);
		return  -1;
	}
	ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}
	ret = gpio_pin_configure_dt(&led1, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}
	ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
	if (ret != 0) {
		printk("Error %d: failed to configure %s pin %d\n",
			   ret, button.port->name, button.pin);
		return -1;
	}
	while (1) {
		/* Read the status of the button and store it */
		bool val = gpio_pin_get_dt(&button);

		/* Update the LED to the status of the button */
		gpio_pin_set_dt(&led0, val);
		gpio_pin_set_dt(&led1, !val);
		
		k_msleep(SLEEP_TIME_MS);
	}
	return 0;
}

Credits

R. Scott Coppersmith
17 projects • 25 followers
Research Engineer, Embedded systems designer
Contact

Comments

Please log in or sign up to comment.