Getting started with programming the Raspberry Pi Pico W with C/C++

Note: All links found on this page open in a new tab :)

Hello dear reader, in this article I will show you how you can install the Raspberry Pi C/C++ SDK on Arch Linux so you can get started programming the board

I will link some useful resources at the end of the article so stick around.

First make sure you have the required dependencies installed on your system. Run `sudo pacman -S make cmake git` to install them.

Next we need to install the libraries that will be used to build and compile our C/C++ code into a format that can be executed by our Raspberry Pi. Very conveniently, there is an AUR package named pico-sdk that you can install using an AUR helper like yay. To install it using yay just run yay -S pico-sdk. You can also manually build it, by cloning the git repo, and running makepkg -si from the root of the directory. But if you are a beginner/want to save time, just use an AUR helper.

Once that is done, the next step is to clone the sdk from Github. As of the time I am writing this blog post, the sdk resides at https://github.com/raspberrypi/pico-sdk. Use git clone https://github.com/raspberrypi/pico-sdk to get it on your system and put it in a folder somewhere. When it's cloned, go into the folder and run git submodule update --init This will clone some libraries needed for the Pico W.

Now we can try building an example project and uploading it on our board. We will start with the blink program. You can find the example code and many more examples at https://github.com/raspberrypi/pico-examples. Keep in mind that if you are using the Pico W you need to look inside the pico_w folder for the appropriate examples. That mistake cost me a lot of time. So, locate the blink example project which is in blink if you are using a regular Pico, or in pico_w/wifi/blink if you are using a Pico W. You can also clone the examples repository for easier access.

Inside the example folder you will see different things based on the one you chose according to your board. Since this article is for the Pico W we will go with that. In our folder we need two things, a CMakeLists.txt file and then a C file containing our code. There are two example for the Pico W, picow_blink.c and picow_blink_slow_clock.c. Just pick one. Next we need to update the content of our CMakeLists.txt file. Head over to this link, copy and replace the content of your CMakeLists.txt file with it. You can later on update it as you please. And to finish, copy the pico_sdk_import.cmake from the sdk you cloned. The path should be /path/to/sdk/external/pico_sdk_import.cmake. Copy it in place it in your example project folder.

If you are not familiar with CMake you can click here to view a screenshot explaining briefly what each line in that file is doing picow_cmake_explained

At this point your C file should contain the code corresponding to your platform and your CMake file should also contain the correct configuration for CMake to generate the build files for your project. Now create a directory named build in your example project folder. If you have done everything well we should have this folder structure in your example project:

Double check that and if everything is correct we move on to the exciting part.

First, we need to tell CMake where to find our sdk, so run export PICO_SDK_PATH=/path/to/sdk. That done, from the build directory, run the following command cmake -DPICO_BOARD=pico_w .. Which essentially means to generate the build files for the configuration that is in the folder above. If you were using a regular Pico you would just run cmake ... It will generate a bunch of files. Next run make to compile the project. When it's done, you should have another bunch of files generated and among them should be a .uf2 file, that is the one file we need to copy to our board.

Plug your USB cable into your computer. Next, locate the BOOTSEL button on your Pico, press and hold it and plug the USB in while still holding the BOOTSEL. Wait one second and release the BOOTSEL. This will essentially make your Pico act as a mass storage device. On some Linux systems, the Pico should automatically appear in your file system, but if it doesn't then it means you have to mount it yourself. You can find a lot of tutorials online on how to do this. Once your Pico is showing as a mass storage device, just copy the .uf2 file to it and if everything went well, the Pico should not appear anymore as a mass storage device. It should reboot and the LED should start blinking.

Congratulations, you just made the first steps in becoming an embedded software programmer. Now, everything doesn't always go well, while I am confident that this article covers all the problems I had while doing this myself, you might still encounter some bugs depending on your local environment. If that happens, don't let it discourage you, I was in the same spot, use Google, ask people on Reddit, Discord, everywhere you can get help. And you will solve the issue.

I hope this have been useful, until next time.

Joseph

Back to posts