Incorporating the Raspberry Pi Pico into Your Next Project

There’s a new $4 microprocessor module in town and it’s called the Raspberry Pi Pico.  It’s based on a bespoke RP2040 — Raspberry Pi’s first foray into custom silicon creation.  I placed an order last week and five of these little guys arrived just a few minutes ago.

First Appearances

From the image, you’ll note that the footprint incorporates castellated vias as well as PTH vias.  That means these devices can be mounted with breakaway 0.1” headers (expensive) or attached directly to a board and sent through a reflow machine (inexpensive.)  Anytime you purchase and install a header through an assembly company, the price of the board goes up substantially.  The least expensive option is to take advantage of the castellated vias and surface mount the device.



Image of the Raspberry Pi Pico from raspberrypi.org



You can also tell from the image that the module is not densely populated.  There’s a great deal of unutilized PCB space, likely because the designers chose to stick with 0.1” (2.54 mm) pitch header holes.  This makes the device easy to use with solderless breadboards, but bigger than it needs to be.  By the looks of things, it appears reasonable that the RP2040 and supporting circuitry could be easily fit into a footprint half the size if a 0.5” (1.27 mm) pitch header hole was used.

The devices I ordered arrived in a reel-pack.  That means these are ready for a pick-and-place machine and easy incorporation into my next project!



Raspberry Pi Picos in an SMT reel.  Image from Raspberrypi.org



Features

The RP2040 is a dual ARM Cortex-M0+ running at 133 MHz.  It has 264kB on-chip SRAM and can support up to 16 MB of off-chip flash memory (although the module only comes with 2MB installed).  The chip has 2 UARTs, 2 SPI controllers, 2 I2C controllers, 16 PWM channels, and a USB 1.1 controller and PHY that can function as host or device.  Those devices are mapped to some of the 30 GPIO pins



System overview from the RP2040 datasheet.



First Project

“Hello World!” is a right-of-passage program for every microcontroller I’ve ever worked with and the Pico is no different.  On a side-note, it’s probably best that engineers are such stalwarts of arcane traditions, otherwise, we’d spend time programming in classic quotes like “I’m sorry, Dave. I’m afraid I can’t do that”, “Shall we play a game?“, “Strange women lying in ponds distributing swords is no basis for a system of government”, or perhaps even “I’m Pickle Riiick!”.  Any which will result in at least a half-hour detour to select the appropriate quote and then watch the clip on youtube.

Raspberry Pi provided a half-dozen ways to create and then compile code for the device.  Once complied, the device can be mounted as a filesystem and the code is simply copied from the compile location on the device.  This makes programming the device cheap and easy.  You’ve just got to get the toolchain up and running on your favorite OS and then pick your favorite Integrated Development Environment (IDE).  The Pico supports Linux, MacOS, and Windows10 using C/C++, Python, and Visual Studio.

I opted for the C++ SDK and command-line level since I could do everything from a remote shell on a headless pi and wouldn’t have to go to the trouble of finding an extra keyboard and monitor.  The following instructions are taken (with modifications for laziness) from the Getting Started with Raspberry Pi Pico Datasheet

# goto user home directory, create and change into directory ‘pico’
$ mkdir ~/pico && cd ~/pico

# download the sdk and examples into ‘pico-sdk’ and ‘pico-examples’ directories
$ git clone -b master https://github.com/raspberrypi/pico-sdk.git
$ cd pico-sdk
$ git submodule update –init
$ cd ..
$ git clone -b master https://github.com/raspberrypi/pico-examples.git

# use aptitude to install necessary software
$ sudo apt update
$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential minicom

# Prepare cmake build directory, let compiler know where the sdk is.
$ cd pico-examples
$ mkdir build && cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake ..

# move into hello-world directory
$ cd hello_world

# compile the code using all 4 cores on the pi 4
$ make -j4

# Press and hold BOOTSEL button.  Then plug in the pico, release the BOOTSEL button,
# and use dmesg | tail to find (sda).  Then create a mount point.
$ sudo mkdir -p /mnt/pico
$ sudo mount /dev/sda1 /mnt/pico

# Put the compiled .uf2 file onto the Pico
$ sudo cp usb/hello_usb.uf2 /mnt/pico
# Finish writing any files in cache
$ sudo sync

# unmount the drive and safely remove
$ sudo umount /mnt/pico

# replug the usb cable and use minicom to view the output via usb
$ minicom -b 115200 -o -D /dev/ttyACM0

What’s next?

After you confirm the toolchain works properly, it’s time to throw caution to the wind and create your own project.  To do that you need three files — CMakeLists.txt, pico_sdk_import.cmake, and whatever program you write, for example: myAwesomeSasquatch.c.  After we get the first two files in,  copy the directory so you don’t have to keep redoing this work.

# create a program directory and move into it
$ mkdir ~/pico/customProgram && cd ~/pico/customProgram

# Bring the sdk cmake file into the directory
$ cp ../pico-sdk/external/pico_sdk_import.cmake
$ mkdir build

# create a file CMakeLists.txt
$ touch CMakeLists.txt
$ nano CMakeLists.txt

# use the nano editor to type the following, then exit and save
# [Ctrl]+[x] cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)
project(test_project)
pico_sdk_init()
add_executable(test
myAwesomeSasquatch.c)
pico_enable_stdio_usb(test 1)
pico_enable_stdio_uart(test 1)
pico_add_extra_outputs(test)
target_link_libraries(test pico_stdlib)

# Now make the backup
$ cp -r ../customProgram ../defaultSDKdirectory

# Create a program file.
$ touch myAwesomeSasquatch.c

$ nano myAwesomeSasquatch.c

# Type the following into nano then close with [ctrl]+[x] #include <stdio.h>
#include “pico/stdlib.h”
#include “hardware/gpio.h”
#include “pico/binary_info.h”
const uint LED_PIN = 25;
int main() {
            bi_decl(bi_program_description(“Pickle Rick!”));
            bi_decl(bi_1pin_with_name(LED_PIN, “On-board LED”));
            stdio_init_all();
            gpio_init(LED_PIN);
            gpio_set_dir(LED_PIN, GPIO_OUT);
            while (1) {
                        gpio_put(LED_PIN, 0);
                        sleep_ms(1500);
                        gpio_put(LED_PIN, 1);
                        puts(“Hack the Gibson!\n”);
                        sleep_ms(1000);
            }
 }

# Move into the build directory and compile
$ cd build
$ export PICO_SDK_PATH=../../pico-sdk
$ cmake ..
$ make -j4

# Press and hold BOOTSEL button.  Then plug in the pico and release the BOOTSEL button,
# and use dmesg | tail to find (sda).  Then create a mount point.
$ sudo mkdir -p /mnt/pico
$ sudo mount /dev/sda1 /mnt/pico

# Put the compiled .uf2 file onto the Pico
$ sudo cp myAwesomeSasquatch.uf2 /mnt/pico

# Finish writing any files in cache
$ sudo sync

# unmount the drive and safely remove
$ sudo umount /mnt/pico

That’s it, you’re ready to go!  Get out there, read some manuals and make something awesome!  We’ll go through how to put this device on a PCB in a future blog post!

Get a Quote