In the previous post we installed the necessary softwares to get started with embedded development on the Raspberry Pi Pico. This time let's make the "Hello world" of embedded development: blinking an LED.
The Pico SDK is a software layer that lets you write code in a much simpler way than directly communicating with the microcontroller. Get the SDK by running these commands:
cd ~/Code git clone -b master --recurse-submodules https://github.com/raspberrypi/pico-sdk.git
You'll need to add the
PICO_SDK_PATH variable to your profile so CMake can access it:
First we'll create a simple program that does nothing but it helps us understand what files are needed to compile a program. We need to create a new folder, copy the Pico SDK CMake file to it and create a CMakeLists.txt file to configure the board and link libraries from the SDK:
mkdir PicoTestto create a project PicoTest
cp ../pico-sdk/external/pico_sdk_import.cmake .
CMakeLists.txt— see listing below
echo '#include "source.h"' > source.c
From within Visual Studio Code, open the folder
PicoTest. When CMakeTools asks you if to configure project, say yes and select the kit GCC for arm-none-eabi x.y.z.
If everything goes well there should be a
build folder with a
PicoTest.uf2 file in it. You could upload this file to a Pico board and it would run but obviously it wouldn't do anything.
So next let's create a program that actually does something.
Blink an LED
The classic example program blinks the built-in LED on the Pico and this is what we're going to do. We'll create a new project but instead of manually going through the same steps as previously, let's use boilerplate script that creates the necessary files for us.
git clone firstname.lastname@example.org:peterzimon/pico-toolbox.git– this repo contains a couple of small useful tools. The script in the
makepicofolder is the one that creates the base project with all the extra files that's needed. It's heavily inspired by this repo.
- Open your bash config and add
makepicoscript to the path:
makepico.sh blink– this creates a new folder called
blinkwith all the necessary files. Note that by default it creates C++ files, if you want to use plain C you can run
makepico.sh blink -cinstead.
Open the folder in VSCode and when asked, select the GCC Arm kit.
If everything goes alrite the project should auto-build and you should see a
build folder with a lot of files in it.
Copy and paste this to
Build in the VSCode toolbar to re-build the code.
The simplest way to run a program on a Pico is to mount the board as an USB device and upload a
u2f file to it:
- Connect the Raspberry Pi Pico to your Mac using a micro-USB cable, making sure that you hold down the BOOTSEL button as you do so, to force it into USB Mass Storage Mode. A new drive will appear on your Mac.
- Drag and drop
build/blink.u2fto the Pico's root folder. The Pico should automatically unmount itself (resulting in a "Disk ejected improperly" error 😤) and the Pico's built-in LED should start blinking.
For more info on this method check out the Getting started doc (from page 9).
It's quite cumbersome to remount the Pico every time we want to update our program, so in the next post we'll look at a more robust way to upload programs and debug our code using another Pico board.