Get started with MicroPython on Raspberry Pi Pico: The Official Raspberry Pi Pico Guide
By Gareth Halfacree and Ben Everard
()
About this ebook
Microcontrollers, like the RP2350 chip at the heart of Raspberry Pi Pico 2, are computers stripped back to their bare essentials. You don't use monitors or keyboards with them — instead, you program them over USB to take their input from (and send their output to) on-board input/output pins.
Using these programmable connections, you can light LEDs, make noises, send text to screens, and much more. In Get started with MicroPython on Raspberry Pi Pico 2nd Edition, you will learn how to use the beginner-friendly MicroPython language to write programs, and you’ll connect up hardware to make your Raspberry Pi Pico interact with the world around it. Using these skills, you can create your own electromechanical projects.
Fully updated for Raspberry Pi Pico 2 and 2 W, as well as the latest version of MicroPython, this book shows you how to:
- Get started with Raspberry Pi Pico 2 and Pico 2 W, as well as the original Pico and Pico W
- Work with various electronic components
- Create your own programmable electronic contraptions
- Turn Pico 2 W into a network-connected node for the Internet of Things
- Link your Pico 2 W to your smartphone, tablet, or another Pico 2 W with Bluetooth Low Energy (BLE)
Whether you’re using Raspberry Pi Pico for a home project, industrial automation, or learning (or teaching!) electronics and programming, this book will show you how.
- Chapter 1: Get to know your Raspberry Pi Pico
- Chapter 2: Programming with MicroPython
- Chapter 3: Physical computing
- Chapter 4: Physical computing with Raspberry Pi Pico
- Chapter 5: Traffic light controller
- Chapter 6: Reaction game
- Chapter 7: Burglar alarm
- Chapter 8: Temperature gauge
- Chapter 9: Data logger
- Chapter 10: Digital communication protocols: I2C and SPI
- Chapter 11: Wi-Fi connectivity with Pico W
- Chapter 12: Bluetooth connectivity with Pico W
- Appendix A: Raspberry Pi Pico specifications
- Appendix B: Pinout guide
- Appendix C: Programmable I/O
Gareth Halfacree
Gareth Halfacree is a freelance technology journalist, writer, and former system administrator in the education sector. With a passion for open-source software and hardware, he was an early adopter of the Raspberry Pi platform and has written several publications on its capabilities and flexibility. He can be found on Mastodon as @[email protected] or via his website at freelance.halfacree.co.uk.
Read more from Gareth Halfacree
The Official Raspberry Pi Beginner's Guide: How to use your new computer Rating: 4 out of 5 stars4/5The Official BBC micro:bit User Guide Rating: 4 out of 5 stars4/5Raspberry Pi User Guide Rating: 4 out of 5 stars4/5
Related to Get started with MicroPython on Raspberry Pi Pico
Related ebooks
Design an RP2040 board with KiCad: Creating Raspberry Pi Pico-compatible PCBs Rating: 0 out of 5 stars0 ratingsThe Official Raspberry Pi Projects Book Volume 2: 200 Pages of Coding and Creating Rating: 0 out of 5 stars0 ratingsLearning Raspberry Pi Rating: 0 out of 5 stars0 ratingsRaspberry Pi Home Automation with Arduino - Second Edition Rating: 0 out of 5 stars0 ratingsRaspberry Pi Mechatronics Projects HOTSHOT Rating: 5 out of 5 stars5/5Arduino Solutions Handbook: Design interesting DIY projects using Arduino Uno, C and C++ (English Edition) Rating: 0 out of 5 stars0 ratingsArduino for the Cloud:: Arduino Yun and Dragino Yun Shield Rating: 0 out of 5 stars0 ratingsMechatronics for the Evil Genius: 25 Build-it-Yourself Projects Rating: 0 out of 5 stars0 ratingsSimulation-Driven Electronics Design: The easy way to design your own electronics projects (English Edition) Rating: 0 out of 5 stars0 ratingsArduino: The ultimate guide to Arduino, including projects, programming tips & tricks, and much more! Rating: 0 out of 5 stars0 ratingsArduino Networking Rating: 4 out of 5 stars4/5Raspberry Pi for Python Programmers Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsArduino Wearable Projects Rating: 0 out of 5 stars0 ratingsDigital Electronics with Arduino: Learn How To Work With Digital Electronics And MicroControllers Rating: 5 out of 5 stars5/5Arduino Android Blueprints Rating: 0 out of 5 stars0 ratingsRaspberry Pi Robotics Essentials Rating: 0 out of 5 stars0 ratingsRaspberry Pi Projects For Dummies Rating: 5 out of 5 stars5/5Getting Started with Raspberry Pi: Program Your Raspberry Pi! Rating: 4 out of 5 stars4/5Learn Arduino Prototyping in 10 days Rating: 0 out of 5 stars0 ratingsInternet of Things with Arduino Cookbook Rating: 1 out of 5 stars1/5Learning IoT with Particle Photon and Electron Rating: 0 out of 5 stars0 ratingsRaspberry Pi 5 Projects for Newbies: A Simplified Guide to Creating Projects with the Raspberry Pi 5 Rating: 0 out of 5 stars0 ratingsArduino Electronics Blueprints Rating: 4 out of 5 stars4/5Arduino App Bluetooth Robotics Rating: 4 out of 5 stars4/5Learning C for Arduino Rating: 0 out of 5 stars0 ratingsArduino Robotic Projects Rating: 4 out of 5 stars4/5
Computers For You
Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5The ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 4 out of 5 stars4/5Procreate for Beginners: Introduction to Procreate for Drawing and Illustrating on the iPad Rating: 5 out of 5 stars5/5Elon Musk Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Technical Writing For Dummies Rating: 0 out of 5 stars0 ratingsUX/UI Design Playbook Rating: 4 out of 5 stars4/5The Self-Taught Computer Scientist: The Beginner's Guide to Data Structures & Algorithms Rating: 0 out of 5 stars0 ratingsStorytelling with Data: Let's Practice! Rating: 4 out of 5 stars4/5CompTIA Security+ Get Certified Get Ahead: SY0-701 Study Guide Rating: 5 out of 5 stars5/5The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution Rating: 4 out of 5 stars4/5Deep Search: How to Explore the Internet More Effectively Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsBecoming a Data Head: How to Think, Speak, and Understand Data Science, Statistics, and Machine Learning Rating: 5 out of 5 stars5/5Learning the Chess Openings Rating: 5 out of 5 stars5/5Computer Science I Essentials Rating: 5 out of 5 stars5/5Fundamentals of Programming: Using Python Rating: 5 out of 5 stars5/5The Musician's Ai Handbook: Enhance And Promote Your Music With Artificial Intelligence Rating: 5 out of 5 stars5/5A Quickstart Guide To Becoming A ChatGPT Millionaire: The ChatGPT Book For Beginners (Lazy Money Series®) Rating: 4 out of 5 stars4/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5CompTIA IT Fundamentals (ITF+) Study Guide: Exam FC0-U61 Rating: 0 out of 5 stars0 ratings
Reviews for Get started with MicroPython on Raspberry Pi Pico
0 ratings0 reviews
Book preview
Get started with MicroPython on Raspberry Pi Pico - Gareth Halfacree
Get started with MicroPython
on Raspberry Pi Pico,
2nd Edition
Get started with MicroPython on Raspberry Pi Pico
by Gareth Halfacree and Ben Everard
ISBN: 978-1-912047-29-1
Copyright © 2024 Gareth Halfacree and Ben Everard
Printed in the United Kingdom
Published by Raspberry Pi Ltd., 194 Science Park, Cambridge, CB4 0AB
Editors: Brian Jepson, Liz Upton
Technical Editors: Brian Jepson, Jo Hinchliffe
Interior Designer: Sara Parodi
Production: Nellie McKesson
Photographer: Brian O’Halloran
Illustrator: Sam Alder
Graphics Editor: Natalie Turner
Publishing Director: Brian Jepson
Head of Design: Jack Willis
CEO: Eben Upton
January 2025: Second Edition, Second Printing
June 2024: Second Edition
January 2021: First Edition
The publisher, and contributors accept no responsibility in respect of any omissions or errors relating to goods, products or services referred to or advertised in this book. Except where otherwise noted, the content of this book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
Welcome
You might think of computers as things you stick on your desk and type on. That is certainly one type of computer, but it’s not the only type. In this book, we’re looking at microcontrollers — small processing units with a bit of memory that are good at controlling other hardware. You probably have lots of microcontrollers in your house already.
There’s a good chance your washing machine is controlled by a microcontroller; maybe your watch is; you might find one in your coffee machine or microwave. All these microcontrollers already have software running on them and the manufacturers make it hard to make any kind of change to that software.
A Raspberry Pi Pico, on the other hand, is a microcontroller that you can easily program (and reprogram!) over a USB connection. In this book, we’ll look at how to get started with Pico and how to make it work with other electronic components. By the end of the book, you’ll know how to create your own programmable electronic contraptions. What you do with them is up to you.
You can find this book’s example code, errata, and other resources in its GitHub repository at rptl.io/pico-resources-2e. If you’ve found what you believe is a mistake or error in the book, please let us know by using our errata submission form at rptl.io/pico-errata-2e.
About the authors
Gareth Halfacree is a freelance technology journalist, writer, and former system administrator in the education sector. With a passion for open-source software and hardware, he was an early adopter of the Raspberry Pi platform and has written several publications on its capabilities and flexibility. He can be found on Mastodon as @[email protected] or via his website at freelance.halfacree.co.uk.
Ben Everard is a geek who has stumbled into a career that lets him play with new hardware. As the editor of HackSpace magazine (hsmag.cc), he spends more time than he really should experimenting with the latest (and not-so latest) DIY tech. He lives in Bristol with his wife and two daughters in a house that’s slowly filling up with electronics equipment and 3D printers.
Colophon
Raspberry Pi is an affordable way to do something useful, or to do something fun.
Democratising technology — providing access to tools — has been our motivation since the Raspberry Pi project began. By driving down the cost of general-purpose computing to below $5, we’ve opened up the ability for anybody to use computers in projects that used to require prohibitive amounts of capital. Today, with barriers to entry being removed, we see Raspberry Pi computers being used everywhere from interactive museum exhibits and schools to national postal sorting offices and government call centres. Kitchen table businesses all over the world have been able to scale and find success in a way that just wasn’t possible in a world where integrating technology meant spending large sums on laptops and PCs.
Raspberry Pi removes the high entry cost to computing for people across all demographics: while children can benefit from a computing education that previously wasn’t open to them, many adults have also historically been priced out of using computers for enterprise, entertainment, and creativity.
Raspberry Pi eliminates those barriers.
Raspberry Pi Press
store.rpipress.cc
Raspberry Pi Press is your essential bookshelf for computing, gaming, and hands-on making. We are the publishing imprint of Raspberry Pi Ltd. From building a PC to building a cabinet, discover your passion, learn new skills, and make awesome stuff with our extensive range of books and magazines.
The MagPi
magpi.raspberrypi.com
The MagPi is the official Raspberry Pi magazine. Written for the Raspberry Pi community, it is packed with Pi-themed projects, computing and electronics tutorials, how-to guides, and the latest community news and events.
Chapter 1
Get to know your Raspberry Pi Pico
Get acquainted with your powerful new microcontroller board and learn how to attach pin headers and install MicroPython to program it
Raspberry Pi Pico-family boards are miniature marvels, putting the same technology that underpins everything from smart home systems to industrial factories in the palm of your hand. Whether you’re looking to learn about the MicroPython programming language, take your first steps in physical computing, or want to build a hardware project, Pico — and the amazing community behind it — will support you every step of the way.
Raspberry Pi Pico devices are microcontroller development boards. They’re designed for experimenting with physical computing using a special type of processor: a microcontroller. The size of a stick of gum, the Pico boards pack a surprising amount of power thanks to the chip at their centre: an RP2040 microcontroller for Pico and Pico W, and an RP2350 for Pico 2 and Pico 2 W.
Raspberry Pi Pico boards aren’t designed to replace Raspberry Pi, which is an entirely different class of device known as a single-board computer. You might use Raspberry Pi to play games, write software, or browse the web. Raspberry Pi Pico is designed for physical computing projects, where it is used to control anything from LEDs and buttons to sensors, motors, and even other microcontrollers.
Throughout this book you’ll learn all about the Raspberry Pi Pico boards, but the skills you learn will also apply to any development board based around the RP2040 or RP2350 microcontrollers — and even other devices, so long as they are compatible with the MicroPython programming language.
A guided tour of Raspberry Pi Pico 2
A Raspberry Pi Pico-family microcontroller — ‘Pico’ for short — is a lot smaller than even Raspberry Pi Zero, the most compact of Raspberry Pi’s single-board computers. Despite this, it includes a lot of features — all accessible using the pins around the edge of the board. The second-generation Pico series is available in two versions, Pico 2 and Pico 2 W; you’ll see the difference between the two later. If you have an original Pico or Pico W, the layout of the board is generally the same.
Figure 1-1 shows Raspberry Pi Pico 2 as seen from above. If you look at the longer edges, you’ll see gold-coloured sections with small holes. These are the pins which provide the RP2040 microcontroller with connections to the outside world — known as input/output (IO).
Figure 1-1: The top of the board
The pins on your Pico 2 are very similar to the pins that make up the general-purpose input/output (GPIO) header on a Raspberry Pi — but while most Raspberry Pi single-board computers come with the physical metal pins already attached, Pico boards do not.
If you want to buy a Pico 2 with headers mounted, look for Raspberry Pi Pico 2 or Pico 2 W with headers. There’s a good reason to offer models without headers attached: look at the outer edge of the circuit board and you’ll see it’s bumpy, with little circular cut-outs (Figure 1-2).
These bumps create what is called a castellated circuit board, which can be soldered on top of other circuit boards without using any physical metal pins. It’s very helpful in builds where you need to keep the height to a minimum, making for a smaller finished project. If you buy an off-the-shelf gadget powered by a Pico-family microcontroller, it’ll almost certainly be fitted using the castellations.
The holes just inwards from the bumps are to accommodate 2.54mm male pin headers. You’ll recognise them as the same type of pins used on the bigger Raspberry Pi’s GPIO header. By soldering these in place pointing downwards, you can push your Pico into a solderless breadboard to make it easy to connect and disconnect new hardware — great for experiments and rapid prototyping!
The chip at the centre of your Pico 2 (Figure 1-3) is an RP2350 microcontroller. This is a custom integrated circuit (IC), designed and built by Raspberry Pi to operate as the brains of your Pico and other microcontroller-based devices. If you look at it closely, you’ll see a Raspberry Pi logo etched into the top of the chip along with a series of letters and numbers which let engineers track when and where the chip was made.
Figure 1-2: Castellation
Figure 1-3: RP2350 chip
At the top of your Pico is a micro USB port (Figure 1-4). This provides power to make your Pico run, and also sends and receives data that lets your Pico talk to a Raspberry Pi or another computer via its USB port. This is how you’ll load programs onto your Pico.
Figure 1-4: micro USB port
If you hold your Pico up and look at the micro USB port head-on, you’ll see it’s shaped so it’s narrower at the bottom and wider at the top. Take a micro USB cable, and you’ll see its connector is the same.
The micro USB cable will only go into the micro USB port on your Pico one way up. When you’re connecting it, make sure to line the narrow and wide sides up the right way around — you could damage your Pico if you try to brute-force the micro USB cable in the wrong way up!
Just below the micro USB port is a small button marked ‘BOOTSEL’, shown in Figure 1-5. ‘BOOTSEL’ is short for boot selection, which switches your Pico between two start-up modes when it’s first switched on. You’ll use the boot selection button later, as you get your Pico ready for programming.
At the bottom of your Pico are three smaller gold pads with the word ‘DEBUG’ above them (Figure 1-6). These are designed for debugging, or finding errors, in programs running on the Pico, using a special tool called a debugger. You won’t need to use the debug header at first, but you may find it useful as you write larger and more complicated programs. On some Raspberry Pi Pico models, the debug pads are replaced by a small, three-pin connector.
Figure 1-5: Boot selection button
Figure 1-6: Debug pads
Turn your Pico over and you’ll see the underside has writing on it (Figure 1-7). This printed text is known as a silk-screen layer, and labels each of the pins with its core function. You’ll see things like ‘GP0’ and ‘GP1’, ‘GND’, ‘RUN’, and ‘3V3’. If you ever forget which pin is which, these labels will tell you — but you won’t be able to see them when the Pico is pushed into a breadboard, so we’ve printed full pinout diagrams in this book for easier reference.
You might have noticed that not all the labels line up with their pins. The small holes at the top and bottom of the board are mounting holes, designed to allow you to fix your Pico to projects more permanently, using screws or nuts and bolts. Where the holes get in the way of the labelling, the labels are pushed further up or down the board: looking at the top-right. So ‘VBUS’ is the first pin on the left, ‘VSYS’ the second, and ‘GND’ the third.
Figure 1-7: Labelled underside
You’ll also see some flat, gold pads labelled with ‘TP’ and a number. These are test points, and are designed for engineers to quickly check that a Raspberry Pi Pico is working after it has been assembled at the factory — you won’t be using them yourself. Depending on the test pad, the engineer might use a multimeter or an oscilloscope to check that your Pico is working properly before it’s packaged up and shipped to you.
If you have a Raspberry Pi Pico 2 W, you’ll find another piece of hardware on the board: a silver metal rectangle (Figure 1-8). This is a shield for a wireless module, like the one on Raspberry Pi 4 and Raspberry Pi 5, which can be used to connect your Pico to a Wi-Fi network or to Bluetooth devices. It’s connected to a small antenna which sits at the very bottom of the board — which is why you’ll find the debug pads or connector closer to the middle of the board on Raspberry Pi Pico 2 W.
Figure 1-8: The Raspberry Pi Pico 2 W wireless module and antenna
Soldering the headers
Look at a standard Raspberry Pi Pico-family board, and you’ll see it is completely flat. There are no metal pins like you’d find on the GPIO header of your Raspberry Pi or on the Pico-family boards with headers.
The easiest way to use a Pico is to attach it to a solderless breadboard —for that, you’ll need to attach pin headers. You’ll need a soldering iron with a stand, some solder, a cleaning sponge, your Pico, and two 20-pin 2.54 mm male header strips. You can use a solderless breadboard to make the process easier.
Although this section shows how to solder headers to an original Raspberry Pi Pico, the process is the same for a Pico 2.
Sometimes 2.54 mm headers are provided in strips longer than 20 pins. If yours are longer, just count 20 pins in from one end and look at the plastic between the 20th and 21st pins: you’ll see it has a small indentation at either side.