Skip to content
Nick Marchuk edited this page May 9, 2023 · 4 revisions

The Raspberry Pi Pico W is a breakout board for the RP2040 microcontroller:

RP2040 microcontroller product page

and the Infineon CYW43439 wireless chip:

Wireless chip

The RP2040 has 2 ARM M0+ CPUs that can run with a clock up to 133MHz, 264k RAM, and no flash memory. It is only comes in a surface mount package, and is available for about $1. In general it is way more powerful than the PIC, with the caveat that you need to add external flash memory, which can be just as expensive as the microcontroller, plus must be carefully routed on the PCB. The RP2040 also has a limited selection of peripherals (2 I2C, 2 UART, 2 SPI, 16 PWM, 4 ADC) which is typically all that is needed, but the PIC has some special peripherals like capacitive sensing that can be useful. The RP2040 makes up for the lack of peripherals with special reprogrammable blocks called PIO. PIO are kind of like big blocks of NAND gates that can be configured in assembly code to automatically read or write digital signals and generate interrupts. For instance, PIO can automatically send out WS2812 logic, without polling a timer like we did with the PIC. Plus the RP2040 has a USB peripheral, and an integrated bootloader based on the mass storage device (MSD) type of USB (like a thumb drive) instead of communication device class (CDC) type of USB (like a serial bootloader on an Arduino or NU32).

The CYW43439 contains two ARM microcontrollers, an M3 and M4, that are dedicated to performing the tasks necessary for wifi and bluetooth communication. These types of 2.4GHz protocols require FCC approval for the PCB design to ensure that they do not interfere with other wireless boards, so it is common to purchase wireless chips presoldered onto their own PCBs that have already been certified, and then use that PCB as if it were a discrete component. The CWY43439 board is preprogrammed to receive instructions over SPI to simplify the tasks that the RP2040 would need to do to manage wireless communication.

The Raspberry Pi foundation manufactures the RP2040, and sells breakout boards called the Raspberry Pi Pico (H or W), or just Pico. The Pico W is $6 on Digikey, which is insane (the PIC32MX170F256B is more than $5 if you by more than 100 units, and that's just the chip!)

The Pico has 2MB of external flash memory, and breaks out most of the pins, and has a button to put the board into bootloader mode. There are lots of other RP2040 boards for reasonable prices that include battery chargers, neopixels, and plugs for I2C chips, and the Pico does not have very good labels on the silkscreen, but for the price it is a nice board to play with.

The RP2040 is relatively new and the datasheet relies on you knowing a lot about ARM chips work in general, so programming it in C is not the easiest. See the github page for the C SDK and the documentation for more details.

But there is another way! With the release of microcontrollers with lots of flash memory and RAM, a version of python has been compiled to fit in such a small space with most of the features of regular python. Adafruit maintains a version called CircuitPython, which is what we will use.

The advantage of CircuitPython over C is that you may already know python, and there are lots of great examples for complicated algorithms in python that are annoying to do in C. The disadvantage is size and speed - reading a pin in CircuitPython may be 100 times slower than the equivalent code in C, and the 2MB on the Pico which is huge for a C program will be gobbled up fast by the python interpreter and so will the RAM.

CircuitPython is becoming a popular way to program microcontrollers, and although you probably won't find it in industry, it can be great for prototyping.

Here is Adafruit's essential learning page for CircuitPython.

The Pico does not come with CircuitPython installed, so the first thing you have to do is program the Pico with the python interpreter. Download the .UF2 file from circuitpython.org.

Press and hold the BOOTSEL button while plugging the Pico in, and it will enumerate as a drive on your computer called RPI-RP2. Drag and drop the .UF2 file onto the drive and the Pico will program itself, and the drive will disappear and come back as a drive called CIRCUITPY. In CIRCUITPY is a file called code.py, which is where you code goes. Edit the code in a text editor, and the code will run as soon as you save the file. If the code contains a print statement, then a USB serial port will be available to communicate over. The RP2040 has native USB and communicates without a baud when you open the port, so you can leave the default baud when you open the port.

I like to work on the code in the Visual Studio Code editor and use putty or screen for a terminal emulator, but another really easy IDE to use is Mu (pronounced moo). Mu comes with some other types of python, and a plotting program if you format your print statements the right way, so let's try it out. Download and install Mu, put CircuitPython on your Pico, and open Mu in CircuitPython mode.

CircuitPython tips and tricks

The default code.py contains the classic Hello World print statement. Press the Serial button in Mu and the code will run, and end, and you'll see a bunch of stuff written in the serial monitor. Click inside the serial monitor and press Ctrl-D to run the code again. Press any key to enter REPL mode.

In REPL mode you can interact with python directly. Note that this is not python running on your computer, this is CircuitPython running on the RP2040, and communication over USB. Pretty cool! REPL mode is great for testing out code without writing a file, and using the help function and tabbing autocomplete to list what functions are inside different classes.

For instance, import the os module, and then type os dot and press tab to see all the things that come with os:

import os
os. (then press tab)

And try a CircuitPython Pico specific module, like board, to list all the features of the Pico:

import board
board. (then press tab)

The help function is not great but can come in handy if you don't have the internet:

import board
help(board.LED)

You can see all of the default modules with:

help("modules")

Everything else has to be added with external libraries, which are added to the circuitpy/lib folder individually. Adafruit maintains a master library zip file, make sure you get the version that matches the .UF2 file. Take a look at the ssd1306 example!

Here is the pinout of the Pico W:

Pico pinout

The Pico W silkscreen does not label the pin names, so you'll use this image a lot. Also, the RESET button is not included, if you want that functionality you can put a button from the RUN pin to GND, but note that it is a hard reset, so the drive and port will disappear (some versions of OSX don't handle this well).

For now, get familiar with the regular functions you'd want on a microcontroller, like time, defining a pin as input or output, reading a pin or turning on and off the LED that is presoldered onto the board.

import board
import time
import digitalio
import analogio
some_led = digitalio.DigitalInOut(board.GP16) # put an LED circuit on pin GP16
some_led.direction = digitalio.Direction.OUTPUT
some_button = digitalio.DigitalInOut(board.GP15) # put a button circuit on GP15
some_button.direction = digitalio.Direction.INPUT
built_in_led = digitalio.DigitalInOut(board.LED)
built_in_led.direction = digitalio.Direction.OUTPUT
adc_pin_a0 = analogio.AnalogIn(board.A0)
while 1:
    print("("+str(adc_pin_a0.value)+",)") # print with plotting format
    if (adc_pin_a0.value > 14000):
        some_led.value = 1
    else:
        some_led.value = 0
    if (some_button.value == 0):
        built_in_led.value = 1
    else:
        built_in_led.value = 0
    time.sleep(.05) # delay in seconds

Some questions to consider: What is the resolution of the ADC? What is the resolution of time.monotonic()? How can you do higher resolution time? How long does it take to turn a pin on?

Note that from the serial monitor you can press Ctrl-C to kill the code and get to REPL if you suspect an infinite loop. Also, you cannot print too much data to the serial monitor in Mu, Mu will crash if you print over 100 times per second (putty and screen do a little better but at some rates will also eventually crash).

Where python really shines is with higher orders of math. On your computer, you would use numpy or scipy for math functions. In CircuitPython, these libraries come from the ulab module.

Try:

from ulab import numpy as np # to get access to ulab numpy functions
# Declare an array with some made up data like
a = np.array([1,3,5,2,4,8,10])
# Test some stats functions, like
print(np.min(a))
print(np.max(a))
print(np.median(a))
# Want to know all the functions available in numpy? In REPL type np. and press Tab.

Your assignment: Make an array with 1024 elements. Into the array, sum 3 sine waves each with a different frequency. Take an FFT of the array and plot the output. Upload your code and a screenshot of the plot in a folder called HW11 in your repo.

Clone this wiki locally