element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Working with Pi Pico C/C++ Projects Easily: A Docker Approach!
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: shabaz
  • Date Created: 8 Jan 2024 1:34 AM Date Created
  • Views 4515 views
  • Likes 9 likes
  • Comments 11 comments
Related
Recommended
  • rpiintermediate
  • pico
  • rp2040
  • pico c sdk
  • raspberry-pi-projects
  • pi pico
  • docker
  • sdk

Working with Pi Pico C/C++ Projects Easily: A Docker Approach!

shabaz
shabaz
8 Jan 2024
Working with Pi Pico C/C++ Projects Easily: A Docker Approach!

Table of Contents

  • Introduction
  • What Is Docker?
  • How Does It Work?
  • What’s Required?
  • Commands Overview
  • Installing Docker
  • Installing and Running the Pi Pico Ready Container
  • Start-Up and Access the Container
  • Download Pi Pico Project Source Code to the Container
  • Build the Code
  • Obtaining the Binary UF2 File
  • Transferring the UF2 File to the Pi Pico
  • Summary

Introduction

Ever seen a Pi Pico project that you’ve wanted to try out, but then you noticed the software is not using the Arduino environment, nor is it using MicroPython or CircuitPython, but instead is using the “Pico C SDK”? If so, it might have put you off, because it’s non-trivial to set up the C SDK environment from scratch. It’s not difficult to do, but there are more steps than there should be!

In an ideal world, raspberrypi.com would have it pre-installed in their Raspberry Pi OS, so that anyone with a Pi could immediately use it.

There are plenty of online tutorials to install the C SDK environment on a PC, and some of the tutorials are targeted at Windows users, some are targeted at Linux, and some expect the user to install Visual Code (an excellent tool, by the way) or CLion (another excellent tool; install both!). These ‘full fat’ installations are great.

However, for the casual user who doesn’t want to go through a lot of steps, there is another method, known as the Docker Container route. For those curious about what Docker is all about, please read the next two sections. For those not interested and just want to get to it, either skip the next two sections!, or, just watch the 8-minute video and you won't need to read a further word. 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

If you're a Windows or Mac user, then there's a slightly easier process, which relies on Docker Desktop instead of having to install a Linux virtual machine. It is shown in the 3-minute video here:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

What Is Docker?

Often, it is desirable to be able to segregate a PC or server into many virtual machines (VM is short for Virtual Machine). Each virtual machine or VM broadly behaves as a ‘ship in the night’, operating independently, and only communicating through the network, if connections between the VMs are needed. In other words, you can make your PC behave as if it is actually several independent PCs.

Some forms of virtual machines need a lot of resources on your real physical computer, such as RAM and disk space, because that RAM and disk space needs to split somehow virtually, amongst as many virtual machines as you are running or plan to run respectively. What if you don’t have a lot of RAM and disk space, or what if you want to have hundreds of VMs on your single PC? In that case, there’s a VM-like technology known as Containers, which can be very high performance, with less resource usage. Containers may share the underlying operating system (OS) on a PC but otherwise operate fairly independently, just like full VMs. As a user, you don’t really need to usually concern yourself with the difference between a VM and a Container.

Docker is a particular container technology, and it’s quite a popular one. In Docker terminology, a running VM-like machine is known as a Docker Container, and it is launched from what is known as a Docker Image.

How Does It Work?

All Docker Images are composed of ‘layers.’ A sales app could have (say) a Ubuntu layer, a web server layer for the sales staff to be able to access, and a layer that contains any glue code to connect to the factory inventory system. A production department app could have a Ubuntu layer, and a web server layer for configuration, and a programmer/debugger layer for burning the Flash memory in the product in the factory. Since both the sales app and the production department app happen to have a Ubuntu layer and a Web Server layer, these common layers are only stored once, thus saving disk space, compared to normal VMs.

Why is this relevant at all to building code for a Pi Pico? Let's get to that : ) The real relevance is that Docker happens to allow you to create Docker Images with a very concise description (known as a Dockerfile). This means that if you want someone to have a computer with the Pi Pico SDK installed, then you can provide them with a short Dockerfile, and their computer will go off and assemble the Docker Image by finding on the Internet all the bits and pieces, using the Dockerfile as a kind of recipe, and will deliver you with a fully-functioning virtual computer (actually known a Container as mentioned before) as if someone has set it all up for you, pre-installed with all the software you need. This means that in one simple step, you have a computer (a container) all ready for building code for the Pi Pico.

What’s Required?

The workflow either requires a Linux computer, or an installation of Docker Desktop. If you're using Windows or Mac, the quickest way is to install Docker Desktop. Once you have Docker Desktop installed, run it from the Windows Start Menu (or Mac equivalent), and then skip the Installing Docker section below.

If you're going the Linux route, then Ubuntu will work, but but other distributions will be fine too. If you don’t have a Linux machine, you could pick up a used small Linux PC or instance, old Intel NUCs, they take up hardly any space) for £100 and use it like a little Linux server; no need to plug keyboard/monitor/mouse to it, thus saving a lot of cost, and you can remotely log into it from your normal desktop or laptop machine. Otherwise, a zero-cost option is to use a Linux virtual machine. This blog won’t go into that, but if you have recommendations for people, please mention them in the comments.

Other than that, all that is needed is the Pi Pico itself, and a USB cable.

Commands Overview

The commands that will be used are shown below. You can download the commands as a text file. 

image

Installing Docker

The following commands will install Docker on your Linux machine (if you're using Docker Desktop, skip this section!). Replace my_username in the fourth line with yours. Note that on Linux distributions other than Ubuntu, you’ll need to change the sudo apt lines. Google will help here (e.g. google for 'docker install red hat’), and please mention it in the comments what commands were successful for you.

sudo apt update
sudo apt install docker.io
sudo groupadd docker
sudo usermod -aG docker my_username

You can test if Docker is functioning by typing the following:

docker run hello-world

If all goes well, you’ll see a Hello from Docker message appear among a load of other text. If that last command does not work, try typing the following, and then reattempt that last command:

sudo chmod 666 /var/run/docker.sock

Installing and Running the Pi Pico Ready Container

Assuming you got the Docker message displayed successfully, or if you're using Docker Desktop, then you are now ready to install a virtual computer (Docker Container) that is all ready for Pi Pico builds. The virtual computer will be called pc1.

Type the following (in either a Linux command shell, or, if you're using Docker Desktop, in a Windows PowerShell, or Mac terminal) to install it:

docker run -d -it --name pc1 --mount type=bind,source=${PWD},target=/home/dev lukstep/raspberry-pi-pico-sdk:latest

Note: If you're using Docker Desktop, if you get an error "error during connect" or "This error may indicate that the docker daemon is not running" then that means you need to start up Docker Desktop from the Windows Start Menu (or Mac equivalent menu).

After it completes (it may take a few minutes), the virtual computer is now residing on your computer, and is running actually, but you need to type one more command to access it.

Start-Up and Access the Container

You can access the container by typing:

docker exec -it pc1 /bin/sh

When you type that, all you’ll see is a command prompt / # and that is a success! You are now in the root directory (/) of the virtual computer.

Note that if you ever shut down your PC, then you need to start up the container in a different way (the command in the section above both installed and started up the container, but after a reboot, you only need to start the container, without re-installing it). If you ever restart your PC, if you're using Linux, then type the following to start up and access the container:

docker start pc1
docker exec -it pc1 /bin/sh

If you're using Docker Desktop, then ensure Docker Desktop is running (from the Windows Start Menu or Mac equivalent menu), and you can start up the container by clicking on the triangular play icon next to the container, as shown in the screenshot below, and then the Status will change to Running. Then, you can type docker exec -it pc1 /bin/sh in a PowerShell to get inside a shell in the container.

image

Incidentally, if desired, Docker Desktop users can directly enter the container (without using PowerShell) by clicking as shown in the screenshot below:

image

For Docker Connect users, if you ever see a message "error during connect" or "This error may indicate that the docker daemon is not running" then that means that Docker Desktop is not running, and you will need to run it from the Windows Start Menu. If you see the message "Error response from daemon: Container xxxxxxxxxxxxxxx is not running" then that means you need to click on the triangular play icon.

Download Pi Pico Project Source Code to the Container

From the / # prompt, type the following:

cd /home/dev
mkdir pico
cd pico

You now have a pico folder to house any desired projects. A lot of projects are on GitHub and can be downloaded using git clone commands.

The example project here will be one called sound_fx_mixer. This particular one requires two git clone commands (most will require just one):

git clone https://github.com/raspberrypi/pico-extras.git
git clone https://github.com/shabaz123/sound_fx_mixer

Now you can go to the project folder, and create a build folder within that, where the final binary .uf2 file will be built.

cd sound_fx_mixer
mkdir build

You are now ready to build the code!

Build the Code

The sound_fx_mixer project happens to require a particular environment variable to be set, but many other projects won’t require this step; the project page would normally advise if the project needed any extra steps like this:

export PICO_EXTRAS_PATH=/home/dev/pico/pico-extras

Now type the following to build the project:

cd /home/dev/pico/sound_fx_mixer/build
cmake ..
make

Note that if you ever subsequently need to rebuild the code, you can usually just type make but if that doesn’t work, and if it’s not a code bug, then repeat everything in this section.

If all goes well, you’ll see the output Built target sound_fx_mixer as shown in the screenshot below, indicated by the arrow. To see the output, type ls and you should see a sound_fx_mixer.uf2 file. That’s the file that needs to be transferred to the Pi Pico!

image

Obtaining the Binary UF2 File

The binary file, called sound_fx_mixer.uf2 in the example above, will reside in the pc1 container. To get it onto your normal computer environment, open up a command prompt (shell), or a new PowerShell window if you're using Docker Desktop, and from your normal home user folder, type the following (the dot at the end signifies that you want to copy to your current folder):

docker cp pc1:/home/dev/pico/sound_fx_mixer/build/sound_fx_mixer.uf2 .

You should now have the .uf2 file in your home folder, all ready for transferring to the Pi Pico!

Transferring the UF2 File to the Pi Pico

Note: This is far easier to do with Windows! If you’re using Linux, it’s an awkward procedure.

Let’s assume for the purpose of this blog post you have access to a Windows machine. In that case, get the .uf2 file copied onto the Windows PC, and then get the Pi Pico (unpowered), and hold down the BOOTSEL button on it (it is the only button on the Pi Pico), and plug in the USB connection to your Windows PC. Now release the BOOTSEL button.

You’ll see a new drive letter appear on your PC, because the Pi Pico will behave like a USB memory stick. You can now drag-and-drop the .uf2 file onto that drive letter, and it will get uploaded to the Pico! It will automatically run.

Summary

The steps described in this blog post should hopefully allow people to try out Pi Pico C/C++ projects quickly, within about 15 minutes if all goes well. All the steps in this blog post are downloadable in a pico-docker-steps text file.

If you try it out and have issues, or have any feedback, please let me know.

Thanks for reading.

  • Sign in to reply
  • shabaz
    shabaz over 1 year ago

    For those using Docker Desktop, Visual Studio Code (VS Code) connects to it, so that the code can be edited and built within VS Code.

    First make sure in Docker Desktop that the container is running, and if not, start it up.

    image

    In VS Code, click on the Extensions icon on the left (the square blocks icon) and type remote development and install:

    image

    Now you can go to View->Command Palette (or type Ctrl-Shift-P on Windows at least) and type attach, and select Attach to Running Container:

    image

    You'll see the name of the container, and you can click it.  

    After a short while, the bottom of the VS Code window will show in green that it's connected:

    image

    Now select the folders icon on the left side, and click on Open Folder. In the box that appears, type the desired path to open, for instance /home/dev/ or /home/dev/pico if you've created the pico folder in the container before.

    image

    And this is what it all looks like. Access to the terminal (shell) is achieved by clicking on the text TERMINAL as shown in the green box, and the commands can be typed there to build the code.

    image

    After the code is built by typing commands in that terminal window, then the .uf2 file can be transferred to the PC by navigating to it in the file view in the left pane,  then right-click on the file and select Download.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fmilburn
    fmilburn over 1 year ago in reply to shabaz

    I was able to get a Pico to work with the Arduino IDE sometime back but haven’t used that in quite sometime. It might be worth revisiting. It appears from a search that it can be used with Visual Studio code.  The raspberrypi.com link you provided above looks to have potential as well.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fmilburn
    fmilburn over 1 year ago in reply to shabaz

    You are correct, I was just slow to figure it out. I really need to read the documentation or warch some videos as this was the first time I have tried to use it. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 1 year ago in reply to fmilburn

    For a  Windows installer route (non-Docker), for Windows users there is this: https://www.raspberrypi.com/news/raspberry-pi-pico-windows-installer/ which I believe (not sure) includes Visual Code.

    However, they mention there could be conflicts if there are other things installed on the PC. With Docker, there's no chance of conflicts, it will (in theory) always work since the container is a clean machine. But, if the Windows installer method works, then that is worth doing.

    There are hopefully some decent blogs discussing directly installing the SDK and Visual Code procedure, so this blog only covers the Docker method.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 1 year ago in reply to fmilburn

    Ah.. I just read your error message. I now understand.

    That message "docker: error during connect: this error may indicate that the docker daemon is not running" indicates that Docker Desktop is exited, or not started. (Maybe your computer went to sleep and it exited, although I thought it shouldn't). Once you start up Docker Desktop (i.e. from Windows Start menu) then that error won't occur.

    On the other hand, if you ever get a message beginning "Error response from daemon" then that's a sign that the Docker Desktop just needs to be checked to see that the container is started up, i.e. check the state of the container in the Docker Desktop window. If it's not in Running state, then the triangle Play icon can be clicked as shown below:

    image

    Then, the docker exec -it pc1 /bin/sh command will work in a PowerShell, to get you inside the container.

    Incidentally, if you wish, you could directly go inside the container like this:

    image

    That's the same as typing that docker exec -it pc1 /bin/sh command.

    I'll add some text in the blog with this info, to explain what to do if that error message appears.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube