LED Blink

As starting with any new programming language or microcontroller, my first program was to blink an LED at a fixt rate (also known as the Hello World of microcontrollers). This was also an introduction to the digital output on a GPIO pin. The resources for the project include the C SDK User Guide, the RP2040 Datasheet and Prof. Hunter's website.


The complete code

The following is the complete C code used to blink the LED.

/*
 * Parth Sarthi Sharma (pss242@cornell.edu)
 * Code based on examples from Raspberry Pi Foundation.
 * The code initializes the LED pin to be output and then
 * turns it ON and OFF in an infinite while loop
 */
#include <stdio.h> //The standard C library
#include "pico/stdlib.h" //Standard library for Pico
#include "hardware/gpio.h" //The hardware GPIO library

#define LED 25 //Define the default LED Pin

int main(){ //The main function
    stdio_init_all(); //Initialize all of the present standard stdio types that are linked into the binary.

    gpio_init(LED); //Initialize the LED pin
    gpio_set_dir(LED, GPIO_OUT); //Initialize the LED pin to be output

    while(1){ //While eternity
        printf("LED OFF\n"); //Print out "LED OFF" to the screen
        gpio_put(LED, 0); //Turn the LED off
        sleep_ms(1000); //Sleep for 1000 milliseconds
        printf("LED ON\n"); //Print out "LED ON" to the screen
        gpio_put(LED, 1); //Turn the LED on
        sleep_ms(1000); //Sleep for 1000 milliseconds
    }
}


Stepping through the code

Includes

The first lines of code in the C source file include some header files. One of these is standard C headers (stdio.h) and the others are headers which come from the C SDK for the Raspberry Pi Pico. The first of these, pico/stdlib.h is what the SDK calls a "High-Level API." These high-level API's "provide higher level functionality that isn’t hardware related or provides a richer set of functionality above the basic hardware interfaces." The architecture of this SDK is described at length in the SDK manual. All libraries within the SDK are INTERFACE libraries.

The next include pulls in hardware API which is not already brought in by pico/stdlib.h. As the name suggests, this interface library gives us access to the API's associated with the hardware GPIO pins on the RP2040.

Don't forget to link these in the CMakeLists.txt file!

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"


Global declarations and defines

The next section of the code is basically a single line which #define's the LED pin number (GPIO 25 is linked to the on-board LED).

#define LED 25


The main function

Initializing UART

The first line in main() is a call to stdio_init_all(). This function initializes stdio to communicate through either UART or USB, depending on the configurations in the CMakeLists.txt file.

stdio_init_all();


GPIO initialization and configuration

In the next 2 lines of the code, I initialized the LED pin and configured it to be the output pin. The gpio_init() function is used to initialize the pin and the gpio_set_dir() function us used to set the pin direction which can be GPIO_OUT (output) or GPIO_IN (input).

gpio_init(LED);
gpio_set_dir(LED, GPIO_OUT);


The infinite while loop

This is the loop which which runs forever and executes the code sequentially. It basically contains 2 subsections: turning the LED on and turning the LED off. I also used the printf() statement to print the output to the screen. In order to see the output, I used the serial monitor provided by the Arduino IDE. Then I used gpio_put() to set the pins HIGH or LOW. Lastly, I used the sleep_ms() to put the CPU to sleep for 1 second for both HIGH and LOW.

while(1){
    printf("LED OFF\n");
    gpio_put(LED, 0);
    sleep_ms(1000);
    printf("LED ON\n");
    gpio_put(LED, 1);
    sleep_ms(1000);
}


The output

In order to view the output, I used the serial monitor provided by the Arduino IDE. As it shows, the LED is toggling at an interval of 1 second.

Output of the LED Blink code


CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(BlinkLED)

pico_sdk_init()

add_executable(BlinkLED BlinkLED.c)

pico_enable_stdio_usb(BlinkLED 1)
pico_enable_stdio_uart(BlinkLED 0)

pico_add_extra_outputs(BlinkLED)

target_link_libraries(BlinkLED pico_stdlib)