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.
● British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
● ISBN 978-3-89576-427-1 Print
ISBN 978-3-89576-428-8 eBook
ISBN 978-3-89576-429-5 ePub
● © Copyright 2021: Elektor International Media B.V.
Prepress Production: D-Vision, Julian van den Berg
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
Chapter 1 • Raspberry Pi Pico Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.2 Pico hardware module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Comparison with the Arduino UNO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 Operating conditions and powering the Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 Pinout of the RP2040 microcontroller and Pico module . . . . . . . . . . . . . . . . . . . . 14
1.6 Other RP2040 microcontroller-based boards . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6.1 Adafruit Feather RP2040 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6.2 Adafruit ItsyBitsy RP2040 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.6.3 Pimoroni PicoSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.6.4 Arduino Nano RP2040 Connect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.6.5 SparkFun Thing Plus RP2040 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.6.6 Pimoroni Pico Explorer Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.6.7 SparkFun MicroMod RP2040 Processor . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6.8 SparkFun Pro Micro RP2040 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6.9 Pico RGB Keypad Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6.10 Pico Omnibus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.6.11 Pimoroni Pico VGA Demo Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 2 • Raspberry Pi Pico Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Installing MicroPython on the Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.1 Using a Raspberry Pi 4 to aid installing MicroPython on the Pico . . . . . . . . . 23
2.2.2 Using a PC (Windows 10) to help install MicroPython on Pico . . . . . . . . . . . 29
Chapter 3 • R
aspberry Pi Pico Simple Hardware Projects . . . . . . . . . . . . . . . . . . . 48
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.2 Project 1: Flashing LED – Using the on-board LED . . . . . . . . . . . . . . . . . . . . . . . 48
3.3 Project 2: External flashing LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.4 Project 3: Flashing SOS in Morse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.5 Project 4: Flashing LED – using a timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.6 Project 5: Alternately flashing LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
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.10 Project 9: Rotating LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.11 Project 10: Binary-counting LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.12 Project 11: Christmas lights (random flashing 8 LEDs) . . . . . . . . . . . . . . . . . . . 72
3.13 Project 12: Electronic dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.14 Project 13: Lucky day of the week . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.15 Project 14: Door alarm with 7-colour flashing LED . . . . . . . . . . . . . . . . . . . . . . 80
3.16 Project 15: 2-digit, 7-segment display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.17 Project 16: 4-digit, 7-segment display seconds counter . . . . . . . . . . . . . . . . . . . 93
3.18 LCDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.19 Project 17: LCD functions – displaying text . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.20 Project 18: Seconds counter — LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.21 Project 19: Reaction timer with LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.22 Project 20: Ultrasonic distance measurement . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.23 Project 21: Height of a person (stadiometer) . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.24 Project 22: Ultrasonic reverse parking aid with buzzer . . . . . . . . . . . . . . . . . . . 114
Chapter 4 • Using Analogue-To-Digital Converters (ADCs) . . . . . . . . . . . . . . . . . 117
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.2 Project 1: Voltmeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.3 Project 2: Temperature measurement – using the internal temperature sensor . . 119
4.4 Project 3: Temperature measurement – using an external temperature sensor . . 120
4.5 Project 4: ON/OFF temperature controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
4.6 Project 5: ON/OFF temperature controller with LCD . . . . . . . . . . . . . . . . . . . . . 125
4.7 Project 6: Measuring the ambient light intensity . . . . . . . . . . . . . . . . . . . . . . . . 128
4.8 Project 7: Ohmmeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
4.9 Project 8: Internal and external temperature . . . . . . . . . . . . . . . . . . . . . . . . . . 133
4.10 Project 9: Using a thermistor to measure temperature . . . . . . . . . . . . . . . . . . 135
Chapter 5 • Data Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.2 Project 1: Logging the temperature data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.3 Project 2: Reading the logged data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
●6
Content
Chapter 6 • Pulse Width Modulation (PWM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.2 Basic theory of pulsewidth modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.3 PWM channels of the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.4 Project 1: Generate a 1000 Hz PWM waveform with 50% duty cycle . . . . . . . . . . 147
6.5 Project 2: Changing the brightness of an LED . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.6 Project 3: Varying the speed of a brushed DC motor . . . . . . . . . . . . . . . . . . . . . 149
6.7 Project 4: Frequency generator with LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
6.8 PROJECT 5: Measuring the frequency and duty cycle of a PWM waveform . . . . . . 152
6.9 PROJECT 6: Melody maker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Chapter 7 • Serial Communication (UART) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
7.2 Raspberry Pi Pico UART serial ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
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
Chapter 8 • The I2C Bus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
8.2 The I2C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
8.3 I2C pins of the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
8.4 Project 1: I2C port expander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
8.5 Project 2: EEPROM memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
8.6 Project 3: TMP102 temperature sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
8.7 Project 4: BMP280 temperature and atmospheric pressure sensor . . . . . . . . . . . 188
8.8 Project 5: Display BMP280 temperature and atmospheric pressure on an LCD . . . 196
Chapter 9 • The SPI Bus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.2 Raspberry Pi Pico SPI ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
9.3 Project 1: SPI Port expander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Chapter 10 • Wi-Fi with the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
10.2 Project 1: Controlling an LED from a smartphone using Wi-Fi . . . . . . . . . . . . . . 206
●7
Raspberry Pi Pico Essentials
10.3 Project 2: Displaying the internal temperature on a smartphone using Wi-Fi . . . 212
Chapter 11 • Bluetooth with the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . 217
11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
11.2 Raspberry Pi Pico Bluetooth interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
11.3 Project 1: Controlling an LED from your smartphone using Bluetooth . . . . . . . . 217
11.4 Project 2: Sending the Raspberry Pi Pico's internal temperature to
the smartphone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Chapter 12 • Using Digital-to-Analogue Converters (DACs) . . . . . . . . . . . . . . . . . 225
12.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
12.2 The MCP4921 DAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
12.3 Project 1: Generating squarewave signal with amplitude under +3.3 V . . . . . . . 226
12.4 Project 2: Generating fixed voltages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
12.5 Project 3: Generating a sawtooth signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
12.6 Project 4: Generating a triangular signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
12.7 Project 5: Arbitrary periodic waveform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
12.8 Project 6: Generating a sinewave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
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
Appendix A • Bill of Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
●8