Blinking an onboard LED using the Raspberry Pi Pico is a simple way to get started with programming the board. You can achieve this using either MicroPython or C/C++ programming languages. Here, I’ll provide examples for both.
Using MicroPython:
Connect your Raspberry Pi Pico to your computer via USB.
Create a new file named blink.py and open it in a text editor.
Copy and paste the following code into blink.py:
import machine
import time
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.toggle()
time.sleep(1)
Save the file.
Copy blink.py to the Raspberry Pi Pico. You can do this by dragging and dropping the file into the Pico’s USB drive.
The LED on pin 25 (GP25) should start blinking once the file is copied over.
Using C/C++:
Set up your development environment for Raspberry Pi Pico programming. You’ll need the official SDK and a code editor (e.g., Visual Studio Code).
Create a new C/C++ file named blink.c and open it in your code editor.
Copy and paste the following code into blink.c:
int main() {
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (true) {
gpio_put(LED_PIN, 1);
sleep_ms(1000);
gpio_put(LED_PIN, 0);
sleep_ms(1000);
}
return 0;
}
Save the file.
Build the code using the provided build system for the Raspberry Pi Pico SDK.
Flash the compiled binary onto your Raspberry Pi Pico.
The LED on pin 25 (GP25) should start blinking after flashing the binary onto the Pico.
These examples demonstrate how to blink the onboard LED of the Raspberry Pi Pico using both MicroPython and C/C++.