0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use Raspberry Pi Pico with C: Simple Guide

To use Raspberry Pi Pico with C, install the official Raspberry Pi Pico SDK and set up a build environment with CMake. Write your C code using the SDK functions, then compile and upload it to the Pico via USB using picotool or drag-and-drop the generated UF2 file.
📐

Syntax

The basic structure to program Raspberry Pi Pico in C uses the Pico SDK. You include the SDK header, define a main() function, and use SDK functions to control hardware like GPIO pins.

Key parts:

  • #include <pico/stdlib.h>: Includes standard Pico functions.
  • int main(): The program entry point.
  • gpio_init(): Initialize a GPIO pin.
  • gpio_set_dir(): Set pin direction (input/output).
  • gpio_put(): Set pin output value.
  • sleep_ms(): Pause execution in milliseconds.
c
#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    stdio_init_all();
    gpio_init(25);           // Initialize onboard LED pin
    gpio_set_dir(25, GPIO_OUT); // Set pin as output

    while (true) {
        gpio_put(25, 1);     // Turn LED on
        sleep_ms(500);      // Wait 500 ms
        gpio_put(25, 0);     // Turn LED off
        sleep_ms(500);      // Wait 500 ms
    }
    return 0;
}
💻

Example

This example blinks the onboard LED on the Raspberry Pi Pico every half second. It shows how to initialize the SDK, set up a GPIO pin, and create a simple loop to toggle the LED.

c
#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    stdio_init_all();
    gpio_init(25);           // Initialize onboard LED pin
    gpio_set_dir(25, GPIO_OUT); // Set pin as output

    while (true) {
        gpio_put(25, 1);     // Turn LED on
        sleep_ms(500);      // Wait 500 ms
        gpio_put(25, 0);     // Turn LED off
        sleep_ms(500);      // Wait 500 ms
    }
    return 0;
}
Output
The onboard LED blinks on and off every 500 milliseconds.
⚠️

Common Pitfalls

Common mistakes when using Raspberry Pi Pico with C include:

  • Not setting up the SDK and build environment correctly, causing build errors.
  • Forgetting to initialize GPIO pins before use.
  • Using wrong pin numbers or directions (input vs output).
  • Not calling stdio_init_all() if using standard input/output functions.
  • Uploading the wrong file type or not using the correct method to flash the Pico.

Always follow the official SDK setup instructions and double-check pin configurations.

c
// Wrong: Using gpio_put without gpio_init or gpio_set_dir
#include "pico/stdlib.h"

int main() {
    gpio_put(25, 1); // This will not work properly
    return 0;
}

// Right: Initialize and set direction first
#include "pico/stdlib.h"

int main() {
    gpio_init(25);
    gpio_set_dir(25, GPIO_OUT);
    gpio_put(25, 1); // Correct usage
    return 0;
}
📊

Quick Reference

Here is a quick summary of key commands and steps to use Raspberry Pi Pico with C:

StepDescription
Install SDKDownload and set up Raspberry Pi Pico SDK and toolchain.
Write CodeCreate C files using SDK functions like gpio_init, gpio_set_dir.
BuildUse CMake and Make to compile your program.
FlashCopy UF2 file to Pico by USB or use picotool to upload.
RunPico runs your program automatically after flashing.

Key Takeaways

Set up the official Raspberry Pi Pico SDK and toolchain before coding in C.
Always initialize GPIO pins and set their direction before use.
Use CMake to build your C programs and generate UF2 files for flashing.
Upload programs by copying UF2 files to the Pico's USB storage or using picotool.
Test simple examples like blinking the onboard LED to verify your setup.