Salman Faris
Published

2️⃣ 🚆MakerTrain: Introduction to Thingy:91x & nRF Connect

In this section, we’ll blink the Thingy:91X onboard LED using nRF Connect SDK — your first step into embedded IoT development!

IntermediateFull instructions provided1 hour303
2️⃣ 🚆MakerTrain: Introduction to Thingy:91x & nRF Connect

Things used in this project

Hardware components

Thingy:91 X
Nordic Semiconductor Thingy:91 X
×1

Software apps and online services

nRF Connect SDK
Nordic Semiconductor nRF Connect SDK

Story

Read more

Schematics

Thingy:91x Schematics PDF File.

Code

Thing:91X Code

C/C++
Run this code and see the output.
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

/* Delay in milliseconds */
#define SLEEP_TIME_MS   100

/* DeviceTree aliases for the LEDs */
#define LED0_NODE DT_ALIAS(led0)
#define LED1_NODE DT_ALIAS(led1)
#define LED2_NODE DT_ALIAS(led2)

/* Collect all LED GPIO specs into an array */
static const struct gpio_dt_spec leds[] = {
	GPIO_DT_SPEC_GET(LED0_NODE, gpios),
	GPIO_DT_SPEC_GET(LED1_NODE, gpios),
	GPIO_DT_SPEC_GET(LED2_NODE, gpios),
};

#define NUM_LEDS (sizeof(leds) / sizeof(leds[0]))

int main(void)
{
	int ret;

	/* Initialize all LEDs */
	for (int i = 0; i < NUM_LEDS; i++) {
		if (!gpio_is_ready_dt(&leds[i])) {
			printf("LED %d not ready\n", i);
			return 0;
		}

		ret = gpio_pin_configure_dt(&leds[i], GPIO_OUTPUT_INACTIVE);
		if (ret < 0) {
			printf("Failed to configure LED %d\n", i);
			return 0;
		}
	}

	/* Loop through LEDs one by one */
	while (1) {
		for (int i = 0; i < NUM_LEDS; i++) {
			/* Turn on current LED */
			gpio_pin_set_dt(&leds[i], 1);
			printf("LED %d ON\n", i);
			k_msleep(SLEEP_TIME_MS);

			/* Turn off current LED */
			gpio_pin_set_dt(&leds[i], 0);
			printf("LED %d OFF\n", i);
		}
	}

	return 0;
}

Credits

Salman Faris
31 projects • 446 followers
Maker | Hardware Hacker | Electronics Enthusiast

Comments