Home / Tutorials / Raspberry Pi Tutorial / Building Code for the Raspberry Pi Pico
Raspberry Pi Pico

Building Code for the Raspberry Pi Pico

A few months after its release, I finally got a hand on the Raspberry Pi Pico. It’s a microcontroller board featuring the RP2040, but don’t expect it to be like an Arduino UNO. 

In fact, it's very much like the STM32 Blue Pill not only because both are ARM microcontrollers but also, physically.

Anyway, to load a program to a Raspberry Pi Pico, you just need to drag .uf2 files to it while connected to your computer via microUSB cable. Or, you can upload using SWD, just like the STM32F4. Loading a program is easy, it’s building one on a Windows machine that’s slightly hard. This tutorial is a simple guide on building a C/C++ code for the Raspberry Pi Pico. 

Requirements

  1. Visual Studio with Build Tools for C++
  2. CMake
  3. GNU ARM Embedded Tool
  4. Python 3
  5. Raspberry Pi Pico SDK
  6. An IDE of your choice (e.g. Visual Studio Code)

Add Variables to PATH

After installing all of the above, check if both CMake and Nmake are in your environment variables. Press windows key + R to open the Run dialog and paste: rundll32.exe sysdm.cpl,EditEnvironmentVariables. Check the Path variables for both user and system and see if the following locations are there:

 

If not, then just add the above to the Path variable in both user and system variables.

 

Raspberry Pi Pico Code Structure

For this example, you'll need the following files:

  • blink.c
  • main CMakeLists
  • source file CMakeLists
  • folders blink and build

we will write (or copy) a short C code that flashes the onboard LED on the Raspberry Pi Pico. Create the following in the IDE of your choice:

We’ll name it blink.c, and then put it inside a folder named blink. On that same folder, we create a CMakeLists.txt file, which will be used by CMake for building the code above. The CMakeLists.txt contains the following:

Learn more about CMake here.

Next, we create a project folder named Blink_project. We then place the blink folder inside this and then another CMakeLists.txt with the following text:

Notice that we have specified two file paths above, one for PICO_SDK_PATH and PICO_TOOLCHAIN_PATH. The first one should point at the location of your Raspberry Pi Pico SDK (uncompressed) and the other should point to your GNU ARM Embedded toolchain.

Next, create an empty folder named build

Then finally, copy the file pico_sdk_import.cmake from inside the pico_sdk/external folder and paste it inside the Blink_project folder.

By now, this should be the structure of your project:

After setting up the project, it’s time to build!

Building and Compiling the Code

Open Developer Command Prompt (windows key, then type developer in search box). Change directory to the build folder inside the project directory.

Next, build using CMake

Raspberry Pi Pico build using cmake

After that, build files will now be inside the build folder. But we are not done yet! Remember that the Raspberry Pi Pico needs binaries and no binaries have been created.

To create the binaries from the source and build files, run make inside the same directory:

Raspberry Pi Pico compile using nmake

If the above command is successful, the binary files are now inside build/blink folder.

Building with Visual Studio 2019

You can also build files and binaries using Visual Studio 2019.

Open a New Project on VS 2019. If you installed the C++ Build Tools, an option for building through CMAKE will appear. 

Name your project. Then in Solution Explorer, delete everything that can be deleted. These should be the only ones left:


Then, we will follow the same file structure as before, changing the CMakeLists.txt to the ones above.

However, VS 2019 adds another (and important) file CMakeSettings.json. This houses the settings we need to compile the files correctly for the Raspberry Pi Pico. 

If CMakeSettings.json is missing from your project, close VS 2019, open it again and load the project folder. Then click Open the CMake Settings Editor:

configure cmake

In solution explorer, right-click on the CMakeSettings.json file, click Open With.., and choose JSON Editor.

Replace what’s inside the CMakeSettings.json file with the following:

Make sure to change D: \\pico-sdk above to the location of your SDK folder. Save the file and VS 2019 will automatically try to create the build files. If everything’s good, you can now start creating binaries.

Still on VS 2019, click Tools > Command Line > Developer Command Prompt.

Open VS2019 developer command prompt

Change directory to the build folder, and then run nmake as before.

The binaries will now be inside the folder <Project Name>/build/<src file>/:

 

Now you can plug in your Raspberry Pi Pico where it will appear as a removable drive. Then, drag the .uf2 file and that's it! You've programmed your Pico on Windows.

Check Also

Send SMS with Raspberry Pi

How to Send SMS with Raspberry Pi

It’s 2020 and sending SMS is now an old technology. But such technology remains to …

Leave a Reply

Your email address will not be published. Required fields are marked *