Raspberry Pi Pico Essentials
Raspberry Pi Pico Essentials
Index
Raspberry Pi Pico
Essentials
Program, build, and master over 50 projects with
MicroPython and the RP2040 microprocessor
R I ErDe to smar
Send the temperatu
ED •
T
r o g r a m l o o p .
• T
Main p EST
Autorun
Smartphone
EEPROM
> 0 : B shed-DC GI2eCt the
ru
if n ( ) Hello World #! PWM
Bluetooth
r a t u r e
T = GetTempe nDsert T
UART
rd
Breadboa # ILE
str(T)
Tstr = "T=" +
RP2040
BME-280
7-segment
Length
Sensors
I2S
DAC & ADC
r ) ) #
n(Tst IoT
Tlen = str(le l e n + " \ r \ n " # AT c
END="+T
Dt = "AT+CIPS # Send to ESP
)
uart.write(Dt # Wait 2 sec
)
utime.sleep(2 # Send da
tr)
uart.write(Ts
Dogan Ibrahim
● 495
Raspberry Pi Pico Essentials
Program, build, and master over 50 projects with
MicroPython and the RP2040 microprocessor
Dogan Ibrahim
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Declaration
The Author and the Publisher have used their best efforts in ensuring the correctness of the information contained
in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other
cause.
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops
and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social
media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Content
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 3 • R
aspberry Pi Pico Simple Hardware Projects . . . . . . . . . . . . . . . . . . . 48
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.7 Project 6: Changing the LED flashing rate – using pushbutton interrupts . . . . . . . . 58
●5
Raspberry Pi Pico Essentials
3.8 Project 7: Alternately flashing red, green, and blue LEDs — RGB . . . . . . . . . . . . . 63
3.9 Project 8: Randomly flashing red, green, and blue LEDs — RGB . . . . . . . . . . . . . . 65
3.18 LCDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.24 Project 22: Ultrasonic reverse parking aid with buzzer . . . . . . . . . . . . . . . . . . . 114
4.3 Project 2: Temperature measurement – using the internal temperature sensor . . 119
●6
Content
6.4 Project 1: Generate a 1000 Hz PWM waveform with 50% duty cycle . . . . . . . . . . 147
6.8 PROJECT 5: Measuring the frequency and duty cycle of a PWM waveform . . . . . . 152
7.3 Project 1: Sending the Raspberry Pi Pico internal temperature to an Arduino Uno 160
7.4 Project 2: Receiving and displaying numbers from the Arduino Uno . . . . . . . . . . 165
7.5 Project 3: Communicating with the Raspberry Pi 4 over the serial link . . . . . . . . 166
8.8 Project 5: Display BMP280 temperature and atmospheric pressure on an LCD . . . 196
●7
Raspberry Pi Pico Essentials
10.3 Project 2: Displaying the internal temperature on a smartphone using Wi-Fi . . . 212
11.3 Project 1: Controlling an LED from your smartphone using Bluetooth . . . . . . . . 217
12.3 Project 1: Generating squarewave signal with amplitude under +3.3 V . . . . . . . 226
12.9 Project 7: Generating an accurate sinewave signal using timer interrupts . . . . . 242
Chapter 13 • Automatic Program Execution after the Raspberry Pi Pico Boots . . 245
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
●8
Preface
Preface
Traditionally, a computer was built using a microprocessor chip and many external support
chips. A microprocessor includes a Central Processing Unit (CPU), an Arithmetic and Logic
Unit (ALU), and timing and control circuitry — and as such it is not particularly useful on
its own. A microprocessor must be supported by many external chips such as memory,
input/output, timers, interrupt circuits etcetera, before it becomes a useful computer. The
disadvantage of this type of design was that the chip count was large, resulting in complex
design and wiring, and high power consumption.
A microcontroller on the other hand is basically a single chip computer including a CPU,
memory, input/output circuitry, timers, interrupt circuitry, clock circuitry, and several other
circuits and modules, all housed in a single silicon chip. Early microcontrollers were limited
in their capacities and speed and they consumed considerably more power. Most of the ear-
ly microcontrollers were 8-bit processors with clock speeds in the region of several MHz and
offered only hundreds of bytes of program and data memories. These microcontrollers were
traditionally programmed using the assembly languages of the target processors. 8-bit mi-
crocontrollers are still in common use, especially in small projects where large amounts of
memory or high speed are not the main requirements. With the advancement of chip tech-
nology we now have 32-bit and 64-bit microcontrollers with speeds in the region of several
GHz and offering several GB of memory space. Microcontrollers are nowadays programmed
using a high-level language such as C, C#, BASIC, PASCAL, JAVA, etc.
The Raspberry Pi Pico is based on the fast and very efficient dual-core ARM Cortex-M0+
RP2040 microcontroller chip running at up to 133 MHz. The chip incorporates 264 KB of
SRAM and 2 MB of Flash memory. What makes the Raspberry Pi Pico very attractive is its
large number of GPIO pins, and commonly used peripheral interface modules, such as SPI,
I2C, UART, PWM, plus fast and accurate timing modules.
Perhaps the biggest advantage of the Raspberry Pi Pico compared to other many microcon-
trollers in the marketplace is its very low cost, large memory, and fast and accurate timing
modules. At the time of writing this book the cost of a single unit was around $6.
Raspberry Pi Pico can easily be programmed using some of the popular high-level languag-
es such as MicroPython, or C/C++. There are many application notes, tutorials, and data-
sheets available on the Internet covering the use of the Raspberry Pi Pico.
●9
Raspberry Pi Pico Essentials
This book is an introduction to using the Raspberry Pi Pico microcontroller with the Micro-
Python programming language. The Thonny development environment (IDE) is used in all
the projects in the book, and readers are recommended to use this IDE. There are over
50 working and tested projects in the book, covering almost all aspects of the Raspberry
Pi Pico.
The following sub-headings are given for each project to make it easy to follow:
• Title
• Brief Description
• Aim
• Block Diagram
• Circuit Diagram
• Program Listing with full description
I hope your next microcontroller-based projects make use of the Raspberry Pi Pico, and this
book becomes useful in the development of your projects.
Dr Dogan Ibrahim
London, February, 2021
● 10
Chapter 1 • Raspberry Pi Pico Hardware
1.1 Overview
The Raspberry Pi Pico is a single-board microcontroller module developed by the Raspberry
Pi Foundation. This module is based on the RP2040 microcontroller chip. In this Chapter we
will be looking at the hardware details of the Raspberry Pi Pico microcontroller module in
some detail. From here on, we will be calling this microcontroller module "Pico" for short, in
for appreciation and recognition though of its official name: Raspberry Pi Pico.
At one edge of the board there is the micro-USB B port for supplying power to the board as
well as for programming it. Next to the USB port there is an on-board user LED that can be
used during program development. Next to this LED sits a button named as BOOTSEL that
is used during programming of the microcontroller as we will see in next Chapters. At the
other edge of the board, next to the Raspberry Pi logo, there are 3 connectors that can be
used for debugging your programs.
Figure 1.2 shows the back view of the Pico hardware module. Here, all the GPIO pins are
identified with letters and numbers. You will notice the following types of letters and num-
bers:
● 11
Raspberry Pi Pico Essentials
Some of the GPIO pins are used for internal board functions. These are:
● 12
Chapter 1 • Raspberry Pi Pico Hardware
The Pico's GPIO hardware is +3.3 V compatible and it is therefore important to be careful
not to exceed this voltage when interfacing external input devices to the GPIO pins. +5 V
to +3.3 V logic converter circuits or resistive potential divider circuits must be used if it is
required to interface devices with +5 V outputs to the Pico GPIO pins.
Figure 1.3 shows a resistive potential divider circuit that can be used to lower +5 V to +3.3 V.
● 13
Raspberry Pi Pico Essentials
An on-board SMPS is used to generate the +3.3 V to power the RP2040 from a range of
input voltages from 1.8 V to +5.5 V. For example, 3 alkaline size-AA batteries can be used
to provide +4.5 V to power the Pico.
The Pico can be powered in several ways. The simplest method is to plug the micro-USB
port into a +5 V power source, such as the USB port of a computer or a +5 V power adapt-
er. This will provide power to the VSYS input (see Figure 1.4) through a Schottky diode. The
voltage at the VSYS input is therefore VBUS voltage minus the voltage drop of the Schottky
diode (about +0.7 V). VBUS and VSYS pins can be shorted if the board is powered from an
external +5 V USB port. This will increase the voltage input slightly and hence reduce rip-
ples on VSYS. VSYS voltage is fed to the SMPS through the RT6150 which generates fixed
+3.3 V for the MCU and other parts of the board. VSYS is divided by 3, and is available at
analogue input port GPIO29 (ADC3) which can easily be monitored. GPIO24 checks the
existence of VBUS voltage and is at logic HIGH if VBUS is present.
Another method to power the Pico is by applying external voltage (+1.8 V to +5.5 V) to the
VSYS input directly (e.g., using batteries or external power supply). We can also use the
USB input and VSYS inputs together to supply power to Pico, for example to operate with
both batteries and the USB port. If this method is used, then a Schottky diode should be
used at the VSYS input to prevent the supplies from interfering with each other. The higher
of the voltages will power VSYS.
● 14
Chapter 1 • Raspberry Pi Pico Hardware
Figure 1.7 shows a simplified block diagram of the Pico hardware module. Notice that the
GPIO pins are directly connected from the microcontroller chip to the GPIO connector. GPIO
nos. 26-28 can be used either as digital GPIO or as ADC inputs. ADC inputs GPIO26-29
have reverse-biased diodes to 3 V and therefore the input voltage must not exceed 3V3 +
● 15
Raspberry Pi Pico Essentials
300 mV. Another point to note is that if the RP2040 is not powered, applying voltages to
GPIO26-29 pins may leak through the diode to the power supply (there is no problem with
the other GPIO pins and voltage can be applied when the RP2040 is not powered).
● 16
Chapter 1 • Raspberry Pi Pico Hardware
● 17
Raspberry Pi Pico Essentials
● 18
Chapter 1 • Raspberry Pi Pico Hardware
• piezo speaker
• 1.54-inch IPS LCD
• 4× buttons
• 2× half-bridge motor drives
• two breakout I2C sockets
• easy access to GPIO and ADC pins
• mini breadboard
• no soldering required
• Raspberry Pi Pico not supplied
● 19
Raspberry Pi Pico Essentials
• 4×4 keypad
• 16× APA102 RGB LEDs
• keypad connected via I2C I/O expander
• GPIO pins labelled
● 20
Chapter 1 • Raspberry Pi Pico Hardware
● 21
Raspberry Pi Pico Essentials
● 22
Chapter 2 • Raspberry Pi Pico Programming
2.1 Overview
At the time of writing this book, the Raspberry Pi Pico accepts programming with the fol-
lowing programming languages:
• C/C++
• MicroPython
• assembly language
Although the Pico by default is set up for use with the powerful and popular C/C++ lan-
guage, many beginners find it easier to use MicroPython, which is a version of the Python
programming language developed specifically for microcontrollers.
In this Chapter we will learn how to install and use the MicroPython programming language.
We will be using the Thonny text editor which has been developed specifically for Python
programs.
Many working and fully tested projects will be given in the next Chapters using MicroPython
with our Pico. Use of the C language will also be discussed in later Chapters with some
projects.
● 23
Raspberry Pi Pico Essentials
• In the File Manager window, you will see two files with the names
INDEX.HTM and INFO_UF2.TXT (Figure 2.2).
● 24
Chapter 2 • Raspberry Pi Pico Programming
• Click on the Getting started with MicroPython tab and click Download UF2
file to download the MicroPython firmware. You should see the downloaded
file at the bottom of the screen. This will take only a few seconds (Figure 2.4).
• Close your browser window by clicking on the cross icon located at the top right
corner.
• Open the File Manager by clicking on menu, followed by Accessories.
• Open the Downloads folder (under /home/pi) and locate the file
with the extension: .uf2. This file will have a name similar to:
micropython-20-Jan-2021.uf2 (Figure 2.5)
• Drag and drop this file to Raspberry Pi Pico's removable drive which is named:
RPI-RP2 (at the top left side of the screen – see Figure 2.5).
• After a while, the MicroPython firmware will be installed onto the internal
storage of Pico and the drive will disappear.
• Your Pico is now running MicroPython.
• Powering-down the Pico will not erase MicroPython from its memory.
● 25
Raspberry Pi Pico Essentials
• Click on the label Python at the bottom right-hand corner of Thonny (Figure
2.7).
• You should see the version number of your MicroPython displayed in the bottom
part of the screen where Shell is located (Figure 2.9).
● 26