led_couleur.c

nRF52833-DK (boards.json id: nrf52833-dk) · File : led_couleur.c · Language : C · Use case : Faire tourner les trois LED (RVB ou 3 LED)

Ribbon-style animation on the three user LEDs: led0, led1, led2 light up one by one. On the MDK it is the red/green/blue cycle of the RGB LED; on the DK it is three of the four LEDs on P0.13 to P0.16.

Hardware

An nRF52833-DK board (Nordic, nRF52833 Cortex-M4F SoC, BLE 5) powered over USB: 4 LEDs on P0.13-P0.16 (active-low) and 4 buttons on P0.11, P0.12, P0.24, P0.25 (active-low, internal pull-up must be enabled). The board alone. On the MDK, check the BSP exposes the three colors as led0/led1/led2 (the MakerDiary BSP case).

Wiring

No wiring, on-board LEDs. For an external LED: anode through 330 Ω to a header pin, cathode to GND, plus a devicetree overlay.

How it works

A for loop walks the GPIO spec array: set 1, 150 ms pause, set 0. The BSP active-low flags are applied automatically by gpio_pin_set_dt.

Running it

Zephyr project: west build -b nrf52833dk/nrf52833 then west flash (on-board J-Link); 115200 serial console on the USB UART connector.

Variants

Overlap the LEDs for a fade, add led3 on the DK for a four-step chaser, or drive true RGB color with three PWM channels.

led_couleur.c

/* Cycle sur led0/led1/led2 : RVB du MDK, trois des quatre LED du DK.
 * Sur le DK, LEDs P0.13/P0.14/P0.15 (aliases led0/led1/led2). */
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

static const struct gpio_dt_spec leds[3] = {
	GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios),
	GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios),
	GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios),
};

int main(void)
{
	for (int i = 0; i < 3; i++) {
		if (!gpio_is_ready_dt(&leds[i])) {
			printk("LED %d absente\n", i);
			return 1;
		}
		gpio_pin_configure_dt(&leds[i], GPIO_OUTPUT_INACTIVE);
	}

	while (1) {
		for (int i = 0; i < 3; i++) {
			gpio_pin_set_dt(&leds[i], 1);  /* une à la fois */
			k_msleep(150);
			gpio_pin_set_dt(&leds[i], 0);
		}
	}
	return 0;
}

← Back to the board