Raspberry Pi Pico Basics: Blinking an LED with GPIO and printf
Abstract
Learn how to create your first Raspberry Pi Pico project using the VS Code Extension for Pico/RP2040 and the Pico SDK. This step-by-step guide covers setting up a new C/C++ project, configuring a GPIO pin to control an LED, and using printf over USB for simple debugging and status indication.
1. Introduction
Similar to how the first STM32 project is blinking an LED, the “Hello World” of Raspberry Pi Pico is also blinking an LED. This tutorial will guide you through:
- Creating a new C/C++ project for the Pico in VS Code.
- Configuring a GPIO pin as an output.
- Writing a simple program to blink an LED and print status messages using printf.
- Compiling, flashing, and running the project on your Raspberry Pi Pico board.
2. Prerequisites
Before starting your first Pico project, make sure you have the following installed and set up:
- VS Code (Visual Studio Code).
- Raspberry Pi Pico/RP2040 Extension for VS Code (This simplifies project setup, compilation, and flashing).
- Pico SDK and the necessary build tools (usually handled by the VS Code extension setup).
- A Raspberry Pi Pico
- A Micro-USB cable to connect the board to your PC for power, flashing, and printf output (serial communication).
- Basic Circuit: An LED connected to a GPIO pin (e.g., GPIO 25 which is the on-board LED) through a current-limiting resistor (e.g., 330 ohms). For simplicity, we’ll use the on-board LED for this first project.
3. Core Functions for Timer Interrupts
We’ll use the Pico VS Code Extension’s functionality to quickly generate a project structure.
Step 1 – Launch VS Code
- Open VS Code.
Step 2 – Create a New Project
- Press Ctrl + Shift + P (or Cmd + Shift + P on Mac) to open the Command Palette.
- Type and select “Pico: New Project”:
- Choose “C/C++”.
- Select an empty folder for your project (e.g., ~/pico-projects/HackerBlink).
- Name your project (e.g., HackerBlink).
- Select the Board type (e.g., Pico):
- Choose the Stdio support “Console over USB” and Crete the Project:
Step 3 – Configure the Project (Optional but Recommended)
- The extension will create a txt and source files.
- The default configuration for Blink should include the necessary libraries for GPIO and standard I/O (like printf). Check that your txt has:
- CMake
- target_link_libraries(HackerBlink pico_stdlib)
4. Writing the LED Blink Code
Open the main source file (usually HackerBlink.c or main.c in the project’s root). We’ll add the necessary includes, define the LED pin, and implement the blinking logic.
Key Code Components
We need to include the standard Pico libraries and use the provided functions:
- pico/stdlib.h: Includes basic Pico functions.
- hardware/gpio.h: Required for all GPIO functions.
- pico/time.h: Required for the sleep_ms() delay function.
- h: For the printf function.
Complete main.c Code
#include
#include "pico/stdlib.h"
#include "hardware/gpio.h"
// Define the GPIO pin for the on-board LED (usually GPIO 25 on Pico)
#define LED_PIN 25
int main()
{
// 1. Initialize Standard I/O (for printf over USB)
stdio_init_all();
// 2. Initialize GPIO Pin
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT); // Set pin as output
printf("--- Raspberry Pi Pico Blink LED Started! ---\n"); // Startup message
while (true) {
// 3. Turn LED ON
gpio_put(LED_PIN, 1);
printf("LED is ON\n");
sleep_ms(500); // Wait for 500 milliseconds
// 4. Turn LED OFF
gpio_put(LED_PIN, 0);
printf("LED is OFF\n");
sleep_ms(500); // Wait for 500 milliseconds
}
return 0; // Should never be reached in an embedded infinite loop
}
Code Explanation
- stdio_init_all(): This crucial function initializes all standard I/O, which, for the Pico, defaults to communicating over the USB connection (Virtual COM Port) for printf.
- gpio_init(LED_PIN): Initializes the selected GPIO pin.
- gpio_set_dir(LED_PIN, GPIO_OUT): Configures the pin to be an Output.
- gpio_put(LED_PIN, 1): Sets the pin state to High (1), which usually turns the LED ON.
- gpio_put(LED_PIN, 0): Sets the pin state to Low (0), which usually turns the LED OFF.
- sleep_ms(500): Halts the program execution for 500 milliseconds.
- printf(…): Prints a message to the serial console every time the LED state changes, confirming that the code is running and functioning as expected.
5. Compiling, Flashing, and Testing
Step 1 – Build the Project
- Click the “Build” icon in the VS Code bottom toolbar, or use the shortcut Ctrl + Shift + B.
The build process will generate a *.uf2 and the usual binaries (*.bin, *.hex and *.elf) file in your build folder.
Step 2 – Flash the Code
- Enter BOOTSEL Mode: Hold down the BOOTSEL button on your Pico and plug the USB cable into your PC. Release the button. Your Pico should appear as a removable drive named RPI-RP2.
- Click on the Run icon, the *.elf file will be loaded into the board’s memory:
Alternatively:
- Drag and Drop: Locate the generated .uf2 file in your project’s build directory (e.g., build/HackerBlink.uf2).
- Drag and drop the .uf2 file onto the RPI-RP2 drive. The Pico will automatically reboot, and the new program will start running.
Step 3 – Testing with printf Output
Open the Serial Monitor: In VS Code, it should be readily available, if not, press Ctrl+Shift+P and type “serial monitor”:
Locate the proper COM and click Start Monitoring:
Confirm Output: You should see the printf messages appearing in the serial console:
— Raspberry Pi Pico Blink LED Started! —
LED is ON
LED is OFF
LED is ON
LED is OFF
…
Check the LED: The on-board LED on your Raspberry Pi Pico should be blinking every 500ms (on for 500ms, off for 500ms).
6. Hands-On Lab Recap
In this introductory article, you successfully:
- Created a C/C++ project for the Raspberry Pi Pico using the VS Code extension.
- Learned to initialize and configure a GPIO pin as an output.
- Used the Pico SDK functions like gpio_init(), gpio_set_dir(), gpio_put(), and sleep_ms().
- Implemented printf for basic status and debugging over the USB serial port.
- Compiled, flashed, and tested your first “Hello World” project.
7. Common Issues & Fixes
| Issue | Cause | Solution |
|---|---|---|
| LED doesn't blink | Wrong GPIO pin defined or LED circuit error (if external). | Ensure #define LED_PIN 25 is used for the on-board LED. Check external circuit for correct resistor and polarity. |
| Code doesn't run / RPI-RP2 drive doesn't appear | Pico isn't in BOOTSEL mode or faulty USB cable. | Unplug and replug the USB cable while holding the BOOTSEL button. Try a different cable. |
| No printf output | stdio_init_all() is missing or the serial monitor is not connected/at the wrong speed. | Ensure stdio_init_all(); is at the start of main(). Check your serial monitor connection and baud rate (usually 115200). |
| Compilation error | Missing includes or incorrect CMakeLists.txt. | Ensure you have #include "pico/stdlib.h", etc., and that pico_stdlib is linked in CMakeLists.txt. |

