Blinky

The Blinky sample blinks an LED forever using the GPIO API.

The source code shows how to:

  1. Get a pin specification from the devicetree as a gpio_dt_spec

  2. Configure the GPIO pin as an output

  3. Toggle the pin forever

See PWM Blinky for a similar sample that uses the PWM API instead.

Build the Blinky with west build, changing rpi_pico appropriately for your board: (rpi_pico for Raspberry Pi Pico)

cd ~/zephyrproject/zephyr
west build -p always -b rpi_pico samples/basic/blinky

Error

ModuleNotFoundError: No module named 'elftools'

Solution:

pip3 install pyelftools

Output

[10/131] Performing build step for 'second_stage_bootloader'
[1/2] Building ASM object CMakeFiles/boot_stage2.dir/home/arcslab/zephyrproject/modules/hal/rpi_pico/src/rp2_common/boot_stage2/boot2_w25q080.S.obj
[2/2] Linking ASM executable boot_stage2
[130/131] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
      BOOT_FLASH:         256 B        256 B    100.00%
           FLASH:       14984 B    2096896 B      0.71%
             RAM:        3816 B       264 KB      1.41%
        IDT_LIST:          0 GB         2 KB      0.00%
Generating files from /home/arcslab/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: rpi_pico
Converting to uf2, output size: 30720, start address: 0x10000000
Wrote 30720 bytes to zephyr.uf2
[131/131] cd /home/arcslab/zephyrproje...project/zephyr/build/zephyr/zephyr.elf

building an app for this board will generate a build/zephyr/zephyr.uf2 file. If the Pico is powered on with the BOOTSEL button pressed, it will appear on the host as a mass storage device. The UF2 file should be drag-and-dropped to the device, which will flash the Pico.

Code

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

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

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

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)

/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

int main(void)
{
	int ret;
	bool led_state = true;

	if (!gpio_is_ready_dt(&led)) {
		return 0;
	}

	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return 0;
	}

	while (1) {
		ret = gpio_pin_toggle_dt(&led);
		if (ret < 0) {
			return 0;
		}

		led_state = !led_state;
		printf("LED state: %s\n", led_state ? "ON" : "OFF");
		k_msleep(SLEEP_TIME_MS);
	}
	return 0;
}

Last updated