This is only a preview of the May 2022 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
KickStart
b y M ike Tooley
Part 8: Introducing the Raspberry Pi Pico
Our occasional KickStart series aims to
show readers how to use readily available
low-cost components and devices to
solve a wide range of common problems
in the shortest possible time. Each of the
examples and projects can be completed
in no more than a couple of hours using
‘off-the-shelf’ parts. As well as briefly
explaining the underlying principles and
technology used, the series will provide
you with a variety of representative
solutions and examples, along with just
enough information to be able to adapt
and extend them for your own use.
This eighth instalment provides you with
an introduction to the popular Raspberry
Pi Pico microcontroller. We’ll describe its
architecture and how to make a simple
application. So, if you’ve not dabbled with
microcontrollers before, the compact Pico
could be just what you’ve been waiting for!
Accurate on-chip clock and timer
Processor temperature sensor
Eight programmable I/O state machines
it with existing devices. For example,
Table 8.1 provides a comparison of the
RPi Pico and the popular Arduino Nano.
for custom peripherals
Fast floating-point libraries in ROM
USB 1.1 programming connection
Low-power sleep and dormant modes.
When assessing the suitability of a
microcontroller for use in a particular
application it can be useful to compare
Fig.8.1. Versions of the Raspberry Pi Pico
are available with and without headers.
T
he Raspberry Pi Pico
microcontroller builds on the
success of more than a decade
of Raspberry Pi (RPi) computers. At
the outset, it’s important to be aware
that this new board isn’t a cut-down
version of its big brother. Instead, it’s
perhaps best to think of it as a lowcost general-purpose microcontroller
with RPi pedigree, but without all the
bells and whistles of a full-blown RPi
computer. For example, there’s no
Ethernet connection, no monitor port
and no camera interface. However, what
you do get is a competent and very
soundly engineered microcontroller
that not only delivers comparable
functionality to that of the immensely
popular Arduino Uno and Nano boards,
but also has a few added features that
can help to make life easy. Key features
of the RPi Pico include:
Dual-core ARM Cortex-M0+ processor
Flexible clock running at up to 133MHz
264kB of SRAM, and 2MB of on-board
Flash storage
26 multi-function GPIO pins
Three 12-bit ADC channels
16 controllable PWM channels
Two SPI, two I2C and two UART for
communication
36
What’s on the board?
The RPi Pico is supplied on a printed
circuit board that measures a miniscule
51 × 21mm (slightly larger than an
Arduino Nano but significantly smaller
than an Arduino Uno). The board is
supplied in two versions; one without
Table 8.1. Comparing the Raspberry Pi Pico and Arduino Nano
Feature
Raspberry Pi Pico
Arduino Nano*
Microcontroller
RP2040
ATmega328
RAM
264kB
2kB
EAPROM
2MB
32kB (2kB bootloader)
Clock speed
Up to 133MHz
16MHz
USB interface
Micro. USB
Mini. USB
Communication
I2C (2), SPI (2), UART (2)
I2C, SPI, UART
ADC
3 × 12-bit
8 × 10-bit
PWM/DAC channels
Up to 16
6
Digital I/O
23 digital I/O lines with three available for ADC
14 digital I/O lines
Debug port
Yes (via three-pin SWD connector)
No
Supply
5V regulated or 1.8V to 5.5V (typically using a
3.7V LiPo battery – see text)
5V regulated, or
7V to 12V regulated
Pins
40 pins (DIL compatible) and 3-pin for SWD
30-pin plus 6-pin
ICSP header
User LED
Yes
Yes
Timer
Yes
No
Temperature sensor
Yes
No
Real-time clock
(RTC)
Yes (but will require an external battery
connection to maintain the clock)
No
Dimensions
51 × 21 mm
43.9 × 18.5 mm
Ground connections
8 (distributed)
2
*The Arduino Nano Every pin-compatible evolution of the original Arduino Nano features an
enhanced ATmega4809 processor with twice the RAM of its predecessor.
Practical Electronics | May | 2022
headers for direct mounting into a
motherboard using an outer set of
castellated pads and one that can be fitted
with two 2-way headers using an inner
set of plated-through holes (see Fig.8.1).
The GPIO pin assignment for the RPi
Pico is shown in Fig.8.2. In common
with other microcontrollers, pins are
assigned different functions as required
by a specific application. For example,
pin-1 can be used as a general-purpose
digital I/O line (GP0) as well as a serial
communication transmit line (UART0), a
serial peripheral interface receive line (SPI
RX), or as a data line (I2C0 SDA) for interintegrated circuit communication (I2C).
The RPi Pico uses standard 3.3V logic
and there’s a total of 25 GPIO pins to
play with. Three of the GPIO pins can
be configured as analogue inputs (its
RP2040 chip incorporates four 12-bit
analogue-to-digital converters (ADC) but
only three of them can be accessed from
the outside world). In common with other
budget microcontrollers, the Pico does
not have true DAC capability. Instead,
analogue output voltages can be produced
by using pulse-width modulation (PWM)
techniques via 16 of the GPIO pins with
external low-pass filtering.
The Pico has an on-board regulator that
generates the 3.3V supply bus from a 5V
supply rail (see later). There’s a single LED
indicator available for user applications
and a boot selector (BOOTSEL) button
that’s used to enter boot mode when the
board is connected to a USB host device.
Note that boot mode can also be entered
by taking the RUN/Reset pin low (by
shorting the pin to GND (0V). Note also
that when the board enters boot mode
the host computer will recognise it as
a USB drive. This makes it very easy to
update the RP2040’s firmware.
The RP2040 chip
The RPi Pico is based on an RP2040 chip,
a dual-core ARM Cortex M0+ running at
clock speeds of up to 133MHz. The chip
provides 30 multifunction GPIO pins that
Fig.8.2. Pin assignment for the Raspberry Pi Pico’s GPIO connectors.
can be configured for several popular
communication standards including
I2C, SPI, and UART, as well as the ADCs
mentioned previously.
The RP2040 offers a reasonable amount
of onboard RAM (264kB) but does not
provide any internal Flash memory.
Instead, this is provided by an external
memory chip (see Fig.8.3) linked to the
microcontroller by means of the Quad
Serial Peripheral Interface (QSPI) bus. The
QSPI bus uses four data lines (QSPI_SD0
to QSPI_SD3) and is consequently faster
than the Serial Peripheral Interface (SPI)
that only uses two data lines (MOSI and
MISO). The external Flash memory has a
2MB capacity available for program, file
and data storage, including that required
to support the MicroPython language.
Note that the RP2040 can support up
to 16MB of external Flash memory
and several manufacturers (including
Pimoroni, Adafruit and SparkFun) have
exploited this feature in their
own microcontroller products
(see Fig.8.4).
The RP2040’s eight PIO state
machines make it possible to
use custom hardware logic and
data processing blocks that run
independently of the CPU.
This feature makes it possible
to off-load processor-intensive
tasks where large amounts of
data, high data rates or accurate
timing is required.
Coding and debugging
the Raspberry Pi Pico
Fig.8.3. Raspberry Pi Pico board layout.
Practical Electronics | May | 2022
During programming, the RPi
Pico appears as a mass storage
device and files can be opened,
edited, saved and executed in the normal
way. Your work files are stored on the
device itself, which keeps the coding
process very straightforward (note that it
is also important to keep backup copies
of your work files on your PC).
The read, evaluate, print and loop
(REPL) function will allow you to test
fragments of code immediately and
without the need to save your code.
When connected and used in REPL mode
a chevron prompt (>>>) appears in the
integrated development environment
(IDE) – see later.
Note that the USB port can be configured
both as a host or as slave in USB 1.1 mode,
supporting data transfer up to 12Mbit/s.
This will allow you to attach a wide variety
of USB peripherals as well as treating the
board as a USB device for programming
and REPL testing.
In common with other ARM Cortex
processors, the R2040’s Serial Wire
Debug (SWD) port provides a means
of debugging your code. The SWD port
can be a very useful feature because it
provides access to the chip’s firmware,
Fig.8.4. Pimoroni’s PicoLipo retains chip
and pin compatibility with the Raspberry
Pi Pico but offers on-board battery
management and a connector for an
external 3.7V LiPo battery.
37
(Left) Fig.8.5.
Selecting
Thonny’s
options.
(Right) Fig.8.6.
Installing the
MicroPython
firmware.
allowing you to set breakpoints, trace execution and step
through blocks of code. To make use of the serial debugging
feature, a dedicated SWD probe must be connected to the
SWD port which is separated from the rest of the GPIO pins
and accessed at the bottom of the board (see Fig.8.3). Three
physical connections are required: SWDIO (bidirectional data),
SWCLK (clock), and GND (ground).
Power supply arrangements
One of the most interesting and potentially useful features
of the RPi Pico is its ability to operate from multiple power
sources, including:
5V supply via the on-board micro-USB connector
External regulated 5V supply connected to the board’s VSYS
and GND pins
Battery with a voltage in the range 1.8V to 5.5V.
In normal use during application development, power can be
supplied through the USB cable connected to a host computer
and no other supply will be required. When operated with other
equipment the device can derive its supply from an existing
regulated +5V rail. For autonomous operation in the field, the
board can be powered from an external battery, in which case
VSYS should typically be around 3.7V for a standard lithium
polymer (LiPo) battery. However, due to the use of an on-board
RT6150 buck-boost DC-to-DC converter (see Fig.8.3.) the input
voltage applied to VSYS can range from as little as 1.8V to
just over 5V, so instead of using a single 3.7V LiPo cell, two
or three series-connected NiMH batteries can be connected.
Note, however, that external battery management circuitry will
be required for applications that use non-replaceable batteries
(ie, where batteries cannot be removed for recharging).
Trying it out
In conjunction with an IDE, the RPi Pico is very easy to use.
There are several excellent coding environments to choose from,
but the author’s favourite is ‘Thonny’ and this environment
will already be familiar to most existing RPi users. Thonny is
simple to use and makes the coding process relatively painless.
To get started, visit www.thonny.org and download Thonny for
your operating system. When you subsequently click the Install
option you will be prompted to select a destination location,
after which you can create a desktop icon.
When the desktop icon is clicked, the default Thonny
window will appear. Now, while holding the Pico’s ‘Boot Sel’
button, connect the USB cable from the Pico to a vacant port on
your computer and finally release the ‘Boot Sel’ button. Next,
click on ‘Run’ from Thonny’s menu bar and ‘Select Interpreter’
and use the dropdown list to select ‘MicroPython (RPi Pico)’.
Finally, select the port to be used (see Fig.8.5) and enter the
code in Listing 8.1 using Thonny’s code editor.
Listing 8.1 MicroPython code for monitoring the internal
temperature of an RPi Pico’s RP2040 microcontroller.
# Print internal temperature (deg.C)
import machine
import utime
sensor = machine.ADC(4)
while True:
value = sensor.read_16()/19859
temp = 27 - 581 * (value - 0.706)
print(temp)
utime.sleep(2)
Installing the MicroPython firmware
If you need to install the MicroPython firmware (see Fig.8.6)
just click on ‘Install’ and wait for ‘Done!’ to appear and then
close the window. The shell window will now appear with the
REPL chevron prompt (>>>) at the bottom of the screen. Return
to the editor window and then click on the ‘Run’ icon (the
green circled arrow). When asked where to save the program,
click on ‘Raspberry Pi Pico’ (see Fig.8.7).
Executing your code
Following the ‘Run’ command, your code will be executed
and you should see the processor core temperatures appearing
in the Shell window (see
Fig.8.8). If this isn’t the
case, check that you’ve
saved the code to the
RPi Pico (not to the host
computer) and that there
are no error messages in
the Shell window.
Adding an LCD
Fig.8.7. Saving your MicroPython code.
38
Fig.8.8. Results appearing in the Shell window.
It’s easy to add an LCD
display to the RPi Pico
using either the board’s
built-in I2C or SPI
interfaces (we’ve chosen
the former simply because
we have several 20x2
Practical Electronics | May | 2022
LCD I2C displays to hand). The required circuit arrangement is shown in Fig.8.9.
Notice that there are just four connections from the RPi Pico to the LCD board. The
MicroPython code is shown in Listing 8.2. Note that the code makes use of the I2C
library which supports communication between the RPi Pico and the LCD display.
Fig.8.10 shows the results appearing on the LCD.
Listing 8.2 Pico MicroPython code: temperature display using a 16x2 I2C LCD.
# Pi Pico temperature display using a 16x2 I2C LCD module
Going Further
This section (overleaf) details a variety
of sources that will help you locate
parts and further information that
will allow you to make good use of
the Raspberry Pi Pico in your own
projects. It also provides links to
relevant underpinning knowledge and
manufacturers’ reference information.
# Import required libraries
from machine import I2C,Pin,ADC
from pico_i2c_lcd import I2cLcd
from time import sleep
# Use the internal temperature sensor
sensor_temp = ADC(4)
# Get the current temperature
def get_temperature():
reading = sensor_temp.read_u16() * 0.0000504
temperature = 27 - (reading - 0.706)/0.001721
temperature = round(temperature, 2)
return temperature
# Initialise I2C port and LCD display
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000)
lcd = I2cLcd(i2c, 39, 2, 16)
Fig.8.9. Circuit arrangement for adding
an LCD.
# Main loop to display temperature every four seconds
while True:
lcd.putstr(“Temperature:\n”+str(get_temperature())+” deg.C”)
sleep(4)
lcd.clear()
Digital inputs and outputs
Digital inputs and outputs are easily handled using the machine library. For example,
to read a button connected to GPIO Pin 0 and operate an LED connected to GPIO
Pin 1 you would need something like the code shown in Listing 8.3. This code has
an added delay using time.sleep() so the button must be held down for at least
one second before the change of LED state occurs. The code has been commented
and, as you can see, it is reasonably straightforward. To make things even easier
we can make use of the led.toggle() function, as shown in Listing 8.4. Finally,
a dedicated breadboard (like the one shown in Fig.8.11 – see Going Further) can be
invaluable when experimenting with the RPi Pico.
Fig.8.10. Results appearing on the LCD.
Listing 8.3 Code for sensing the state of a button and operating an LED
from machine import Pin
import time
led = Pin(1, Pin.OUT)
# LED connected to GPIO Pin 1
button = Pin(0, Pin.IN, Pin.PULL_DOWN) # Button connected to GPIO
Pin 0
Fig.8.11. The excellent Raspberry Pi Pico
breadboard from SB Components is an
ideal platform for hardware experiments.
while True:
if button.value():
led.value(1) # Turn the LED on
else:
led.value(0) # Turn the LED off
time.sleep(1)
# Hold button down for 1s
Listing 8.4 Improved code using the led.toggle() function
from machine import Pin
from utime import sleep
led = Pin(1, Pin.OUT)
button = Pin(0, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
sleep(1)
Practical Electronics | May | 2022
39
Table 8.2. Going Further with Raspberry Pi Pico
Topic
Source
Notes
RPi Pico
boards
Numerous suppliers eg, The Pi Hut (www.thepihut.com) offer the
RPi Pico boards, both with and without soldered headers.
RPi Pico books
The official RPi Pico guide is Get Started with Micropython on
Raspberry Pi Pico by Gareth Halfacre and Ben Everard (ISNB 9781-912-04786-4). Introduction to the RPi Pico and Thonny, plus
simple projects suitable for beginners.
Programming the Pico – Learn Coding and Electronics with the
Raspberry Pi Pico by Simon Monk (ISBN 979-8-464-88217-1) is
another excellent introduction.
RPi Pico
projects
RP2040
These two books are intended for complete beginners
and they both assume little previous knowledge of
electronics and coding.
Useful beginners’ guide to connecting an RPi Pico to
a PC, installing the Thonny Python IDE, and writing a
MicroPython program to blink the Pico’s LED
https://bit.ly/pe-may22-pico1
Official documentation for the RP2040 microcontroller IC:
https://bit.ly/pe-may22-pico2
RP2040 datasheet: https://bit.ly/pe-may22-pico3
MicroPython
Latest MicroPython documentation: https://bit.ly/pe-may22-pico4
Software
debugging
Information on using the SWD interface with an RPi computer:
https://bit.ly/pe-may22-pico5
RPi Pico
breadboards
and expansion
boards
Boards with headers cost more than those without.
Soldering skills will be required if you intend to solder
your own headers!
The RP2040’s datasheet is extremely detailed and runs
to well over 600 pages!
Latest Thonny version for Windows, Mac or Linux: www.thonny.org
Two useful guides to using SWD with an RPi Pico
SB Components provide a variety of breadboards and
expansion boards designed for the RPi Pico. Similar
boards at the Pi Hut.
https://shop.sb-components.co.uk/
www.thepihut.com
STEWART OF READING
Fluke/Philips PM3092 Oscilloscope
2+2 Channel 200MHz Delay TB,
Autoset etc – £250
LAMBDA GENESYS
LAMBDA GENESYS
IFR 2025
IFR 2948B
IFR 6843
R&S APN62
Agilent 8712ET
HP8903A/B
HP8757D
HP3325A
HP3561A
HP6032A
HP6622A
HP6624A
HP6632B
HP6644A
HP6654A
HP8341A
HP83630A
HP83624A
HP8484A
HP8560E
HP8563A
HP8566B
HP8662A
Marconi 2022E
Marconi 2024
Marconi 2030
Marconi 2023A
17A King Street, Mortimer, near Reading, RG7 3RS
Telephone: 0118 933 1111 Fax: 0118 933 2375
USED ELECTRONIC TEST EQUIPMENT
Check website www.stewart-of-reading.co.uk
PSU GEN100-15 100V 15A Boxed As New
£400
PSU GEN50-30 50V 30A
£400
Signal Generator 9kHz – 2.51GHz Opt 04/11
£900
Communication Service Monitor Opts 03/25 Avionics
POA
Microwave Systems Analyser 10MHz – 20GHz
POA
Syn Function Generator 1Hz – 260kHz
£295
RF Network Analyser 300kHz – 1300MHz
POA
Audio Analyser
£750 – £950
Scaler Network Analyser
POA
Synthesised Function Generator
£195
Dynamic Signal Analyser
£650
PSU 0-60V 0-50A 1000W
£750
PSU 0-20V 4A Twice or 0-50V 2A Twice
£350
PSU 4 Outputs
£400
PSU 0-20V 0-5A
£195
PSU 0-60V 3.5A
£400
PSU 0-60V 0-9A
£500
Synthesised Sweep Generator 10MHz – 20GHz
£2,000
Synthesised Sweeper 10MHz – 26.5 GHz
POA
Synthesised Sweeper 2 – 20GHz
POA
Power Sensor 0.01-18GHz 3nW-10µ W
£75
Spectrum Analyser Synthesised 30Hz – 2.9GHz
£1,750
Spectrum Analyser Synthesised 9kHz – 22GHz
£2,250
Spectrum Analsyer 100Hz – 22GHz
£1,200
RF Generator 10kHz – 1280MHz
£750
Synthesised AM/FM Signal Generator 10kHz – 1.01GHz
£325
Synthesised Signal Generator 9kHz – 2.4GHz
£800
Synthesised Signal Generator 10kHz – 1.35GHz
£750
Signal Generator 9kHz – 1.2GHz
£700
HP33120A
HP53131A
HP53131A
Audio Precision
Datron 4708
Druck DPI 515
Datron 1081
HP/Agilent HP 34401A Digital
Multimeter 6½ Digit £325 – £375
40
HP 54600B Oscilloscope
Analogue/Digital Dual Trace 100MHz
Only £75, with accessories £125
(ALL PRICES PLUS CARRIAGE & VAT)
Please check availability before ordering or calling in
Keithley 228
Time 9818
Marconi 2305
Marconi 2440
Marconi 2945/A/B
Marconi 2955
Marconi 2955A
Marconi 2955B
Marconi 6200
Marconi 6200A
Marconi 6200B
Marconi 6960B
Tektronix TDS3052B
Tektronix TDS3032
Tektronix TDS3012
Tektronix 2430A
Tektronix 2465B
Farnell AP60/50
Farnell XA35/2T
Farnell AP100-90
Farnell LF1
Racal 1991
Racal 2101
Racal 9300
Racal 9300B
Solartron 7150/PLUS
Solatron 1253
Solartron SI 1255
Tasakago TM035-2
Thurlby PL320QMD
Thurlby TG210
Function Generator 100 microHz – 15MHz
Universal Counter 3GHz Boxed unused
Universal Counter 225MHz
SYS2712 Audio Analyser – in original box
Autocal Multifunction Standard
Pressure Calibrator/Controller
Autocal Standards Multimeter
o er
lifier
Voltage/Current Source
DC Current & Voltage Calibrator
Modulation Meter
£250
Counter 20GHz
£295
Communications Test Set Various Options
POA
Radio Communications Test Set
£595
Radio Communications Test Set
£725
Radio Communications Test Set
£800
Microwave Test Set
£1,500
Microwave Test Set 10MHz – 20GHz
£1,950
Microwave Test Set
£2,300
Power Meter with 6910 sensor
£295
Oscilloscope 500MHz 2.5GS/s
£1,250
Oscilloscope 300MHz 2.5GS/s
£995
Oscilloscope 2 Channel 100MHz 1.25GS/s
£450
Oscilloscope Dual Trace 150MHz 100MS/s
£350
Oscilloscope 4 Channel 400MHz
£600
PSU 0-60V 0-50A 1kW Switch Mode
£300
PSU 0-35V 0-2A Twice Digital
£75
Power Supply 100V 90A
£900
Sine/Sq Oscillator 10Hz – 1MHz
£45
Counter/Timer 160MHz 9 Digit
£150
Counter 20GHz LED
£295
True RMS Millivoltmeter 5Hz – 20MHz etc
£45
As 9300
£75
6½ Digit DMM True RMS IEEE
£65/£75
Gain Phase Analyser 1mHz – 20kHz
£600
HF Frequency Response Analyser
POA
PSU 0-35V 0-2A 2 Meters
£30
PSU 0-30V 0-2A Twice
£160 – £200
Function Generator 0.002-2MHz TTL etc Kenwood Badged
£65
d
£350
£600
£350
POA
POA
£400
POA
POA
POA
Marconi 2955B Radio
Communications Test Set – £800
Practical Electronics | May | 2022
|