Raspberry Pi Pico Guide A Complete Newbies To Expert Guide To Master The Use of Raspberry Pi Pico To The Max
Raspberry Pi Pico Guide A Complete Newbies To Expert Guide To Master The Use of Raspberry Pi Pico To The Max
guide
John Ezekiel
Copyright 2021© John Ezekiel
1
Chapter 1: Raspberry Pi
Boards
The Raspberry Pi is a single-board computer that
runs the Linux operating system. You can use a
mouse, monitor, keyboard, and other accessories
to communicate with them through a prompt or
graphical user interface, such as the Raspberry Pi
OS desktop.
2
sensors, cameras, plugin modules, and so on. Its
programming capabilities also add to its flexibility.
Pico Raspberry Pi
3
device. It's the unit in charge of controlling various
aspects of an electronic system.
4
Other features include programmable IOs for
relaying computer instructions, GPIO pins, and so
on.
5
• 16 PWM channels, 2 UART channels, 2 12C
controllers, and 2 SPI controllers
6
Note that the 40 IO pins do not have headers;
instead, they have holes into which you can solder
headers.
7
Diagram of the unit might be useful for ease of
use.
8
for a total of 40. The machine and power-related
pins account for 14 of the 40 pins, while the
remaining 26 are GPIO pins. Three additional pins
are also present, which are used for SWD
Debugging.
9
Additionally, as shown below, there are
Castellations in the GPIO pins that allow the Pico
to be soldered to a project or carrier board.
10
Chapter 2: The RP2040
Microprocessor
11
on a 40nmsw node and has dual ARM-cortex-M0+
cores running at 133 MHz.
12
• The core form is represented by 0 (zero). The
ARM Cortex-M0+ is the core form used here,
which is why the value is 0.
13
Putting the Raspberry Pi Pico
Board to Work
14
the most widely used programming language for
the Pico, according to James Adams, the
Raspberry Pi's Chief Operating Officer.
C/C++
15
MicroPython
16
The Raspberry Pi Pico: Programming
and Setup
17
In addition, we'll be using the Thonny IDE as a text
editor. MicroPython and the Thonny IDE are
available for download on the web.
MicroPython
Thonny
20
We can begin carrying out various tasks once we
are confident in the MicroPython's functionality.
21
Chapter 3: Using the
Raspberry Pi Pico to
Complete Tasks
There are various tasks that can be carried out
with the Raspberry Pi Pico once it has been
completely set up and configured. We'll go over it
here in as much depth as possible in an easy-to-
understand manner for newcomers.
Let's start with the easy ones and work our way
up to the more difficult ones.
22
Soldering Pins to your Raspberry
Pi Pico
23
experience can simply solder single wires directly
to the board using female pin headers.
25
• Clean and Prepare Your Workspace: Make sure
the area where you'll be staying in is clean, well-
ventilated, and free of dirt that might
contaminate the solder. Often, make sure that the
soldering iron and its stand are within reach of the
soldering side.
26
Before you cut it, double-check that the number is
right, and then cut.
27
Also, when the Raspberry Pi Pico is inserted in the
breadboard, make sure the long pins are at a 90-
degree angle to the Raspberry Pi Pico. Check that
all of the pins are properly seated and straight.
28
the pin with the soldering iron for a stronger
adhesive effect with the solder.
30
• A bread board that is half the size of a full-size
bread board
The procedure
31
• In the breadboard, position the 330 ohm
resistor. It should be positioned such that one of
its legs is parallel to Pin 38 and the other is simply
in the breadboard. The aim of this is to create a
GND rail on which all of the rail's pins can be
attached.
32
The Operation’s Coding
33
• Download and install the necessary library. The
code for it can be found in the largest space above
the REPL. We begin by importing two
MicroPython libraries. The first library is the Pin
class, which can be found in the Machine library,
and the second is utime, which is used to monitor
the code's speed.
35
We included a sleep function in the image below
to make the code pause for a second while the
loop runs.
while True :
led .toggle()
print(‘’Toggle’’)
utime.sleep(1)
• After completing all of the steps, your code
should look exactly like the image below. When
you run the code, the Python Shell will say the
word "TOGGLE" every second.
36
The code is thus;
37
Chapter 4: Using the
Raspberry Pi Pico with a
display that is 7-segmented
38
display numbers for keeping track of time,
distances, scores, and other things have this sort
of display.
39
We'll be connecting our Raspberry Pi Pico to a 7-
segment board with a TM1637 controller board
for this project.
Needed Materials
40
The Connection
41
You have the option of attaching the CLK and DIO
pins to either of the Raspberry Pi Pico's 12V SCL
and SDA pins.
42
tm1637.py should be copied to your Pico's root
directory. Because the Pico with MicroPython
does not appear as a drive letter, the simplest
solution is to open tm1637.py in your preferred
IDE or Thonny and save it from there.
import tm1637
from machine import Pin
from utime import sleep
43
We'll use the utime library to add a one-second
"sleep" delay between the various functions so
you can take your time looking at them on the
screen.
mydisplay = tm1637.TM1637(clk=Pin(16),
dio=Pin(17))
44
Only the first four characters will be shown if you
enter more than four. Make sure the parameter
string is enclosed in quotes.
mydisplay.show(“Pico”)
sleep
mydisplay.show(“ “)
sleep
45
display. When changing the display, you do not
have to blank it. However, if you make use of
another show command with a lesser than
maximum character number, then any character
that you do not replace will still be displayed on
the screen. As an instance, if you show(“halo”)
then show(“20”), 20lo will be displayed on the
screen.
mydisplay.number(-123)
sleep
46
However, if you use another show command with
a character count lower than the limit, any
characters that you do not replace will remain on
the screen. For example, if you show(“halo”) then
show(“20”), the screen will display 20lo.
47
You may be wondering why you'd want to use the
number method alone when you can use the
"show" method to display letters or numbers. One
explanation for this is that integers do not need to
be converted to strings. Another explanation is
that the display can only show the number of
digits required. As a result, no blank spaces are
needed to match the number to the right side.
mydisplay.numbers(12,59)
sleep(1)
48
between them if one is available for your phone.
12:59 is an example.
49
Use the "temperature" method to show the
temperature in Celsius. The letter C and the
degree symbol will be added together after your
digits using this form. You may also enter the
temperature as a one- or two-digit integer.
Negative temperatures can still be entered, but
only to a single digit.
mydisplay.show(“ “)
50
mydisplay.brightness is a variable that controls
the brightness of my monitor
import tm1637
from machine import Pin
from utime import sleep
mydisplay = tm1637.TM1637(clk=Pin(16),
dio=Pin(17))
# Show a word
mydisplay.show("Pico")
sleep(1)
51
mydisplay.show(" ")
sleep(1)
#show numbers
mydisplay.number(-123)
sleep(1)
#show temperature
mydisplay.temperature(99)
52
sleep(1)
53
Chapter 5: Building a
Raspberry Pi Pico Weather
Station
54
simply position the DHT sensor through your
window to get accurate weather readings, or use
it to measure the temperature within your home.
Requirements
• Raspberry Pi Pico
55
• I2C backpack with 16x2 1602 LCD
• a wooden breadboard
56
• To transfer files from your Raspberry Pi
and Pico board, you'll need a MicroUSB
cable.
57
Getting your Raspberry Pi Pico up
and running
58
5. Connect your Pico's micro USB to your
Raspberry Pi while holding down the BOOTSEL
button on your Pico.
59
1.Place your Pico board on the breadboard to
begin.
60
5. To power your LCD screen, connect the LCD
GND to the GND rail on your breadboard and the
LCD VCC to the Pico's VBUS (pin 40).
61
2. Disconnect the Raspberry Pi and Pico board
from each other.
62
will, however, discover how to compile the Pico
board's source code. The pico-sdk simple
installation for beginners is also included in this
form. or users that are starting from the ground
up. Go to the “Getting started with Raspberry Pi
Pico C/C++ dev” PDF here to learn more about
the installation process with examples.
63
mkdir weather-station
cd weather-station
git clone -b master
https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init
cd ..
git clone https://github.com/carolinedunn/pico-
weather-station
64
sudo apt install cmake gcc-arm-none-eabi build-
essential
65
• Line 199-200: Because this printf
statement is only for troubleshooting
purposes, it can only be accessed
through minicom. In the
troubleshooting section, we'll go over
accessing readings via minicom.
66
5. To create your UF2 file, use the commands
below in the same Step 3 terminal.
cd pico-weather-station
mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake ..
make
67
2. Disconnect your Raspberry Pi and Pico board
from each other.
68
Using DC Motors With Your
Raspberry Pi Pico
69
Raspberry Pi Pico? Is it possible to bind them
directly to the GPIO? No, it's not.
70
• Thonny is already loaded on your
computer
71
to-male jumper wires for the
breadboard link.
72
on the breadboard with the micro USB port
hanging over the top.
73
5. Connect GPIO 15 on the Pico to IN2 on the
DRV8833.
74
Continue thus;
def forward():
motor1a.high()
motor1b.low()
def backward():
motor1a.low()
motor1b.high()
def stop():
motor1a.low()
motor1b.low()
76
4. Create a final "test" function that will call up all
of the previous functions and run a test sequence
that will move the motor "forward" for two
seconds, then "backward" for another two
seconds before stopping it.
def test():
forward()
utime.sleep(2)
backward()
utime.sleep(2)
stop()
77
for in range(5):
test()
78
Chapter 6: Using a Raspberry Pi
Pico and an Ultrasonic Sensor
80
Hardware Setup of Ultrasonic
Sensor on Raspberry Pi Pico
81
1. Import the Pin class from the machine library,
followed by the utime library. The GPIO pins are
regulated by the Pin class, and the time library is a
time-based feature library.
82
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
def ultra():
trigger.low()
utime.sleep_us(2)
83
5. Pull the trigger pin high for five microseconds
before lowering it. Before turning off the pulse,
this will send a short wave out of the ultrasonic
sensor.
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
84
7. Create a new while loop to check for the receipt
of an echo. The present microseconds timestamp
will be stored in the signalon variable as a result
of this.
while echo.value() == 1:
signalon = utime.ticks_us()
85
9. Add a new variable called distance. The
response to the equation will be saved in this
variable. We multiply the journey time (343.2 m/s
= 0.0343 cm per microsecond) by the speed of
sound (343.2 m/s = 0.0343 cm per microsecond).
The product of the equation is then divided by
two because we don't need to know the total
distance traveled. All we care about is the
distance between the object and the sensor.
86
11. We now create a loop to run the function
every second outside of it.
while True:
ultra()
utime.sleep(1)
● import utime
● trigger = Pin(3, Pin.OUT)
● echo = Pin(2, Pin.IN)
● def ultra():
● trigger.low()
87
● utime.sleep_us(2)
● trigger.high()
● utime.sleep_us(5)
● trigger.low()
● while echo.value() == 0:
o signaloff = utime.ticks_us()
● while echo.value() == 1:
o signalon = utime.ticks_us()
● timepassed = signalon - signaloff
● distance = (timepassed * 0.0343) / 2
● print("The distance from object is
",distance,"cm")
● while True:
● ultra()
● utime.sleep(1)
88
Save the file to the Pico as code.py, then run it by
clicking the green arrow. In the Python Shell, the
distance will be printed every second.
89
to work with outputs, such as an LED, and inputs,
such as a sensor. We'll end up with a 16-line
MicroPython motion detector in the end. Before
beginning this project, make sure your Pico is set
up and tested.
90
A PIR sensor
3 jumper wires (female to male)
Procedure
91
3. Place the anode (long leg) of an LED on pin 34
of the breadboard, and the short leg on the GND
rail. The circuit is completed after these steps are
completed successfully.
92
6. Connect the PIR's GND pin to the GND rail on
the breadboard.
93
1. Import the Pin class from the machine library,
then utime. These libraries help us to keep track
of the progress of our project and communicate
with the GPIO in a more productive manner.
94
way, we'll make sure the GPIO pin is turned off at
the start of the project.
95
4. Make sure the LED is turned off at the start of
the project, then wait three seconds before
continuing. These two lines ensure that the LED
does not activate twice. It also gives the sensor a
chance to settle before being used.
led.low()
utime.sleep(3)
while True:
96
print(pir.value())
if pir.value() == 0:
print("LED On")
led.high()
utime.sleep(5)
97
7. If no movement is detected, the final part of
the conditional test will be activated. Add a line to
switch off (low) the LED, which then pauses for
0.2 seconds, and uses an Else condition to print a
message to the Python Shell. As the conditional
test runs again, the loop will repeat.
else:
print("Waiting for movement")
led.low()
utime.sleep(0.2)
98
from machine import Pin
import utime
led = Pin(28, Pin.OUT)
pir = Pin(16, Pin.IN, Pin.PULL_UP)
led.low()
utime.sleep(3)
while True:
print(pir.value())
if pir.value() == 0:
print("LED On")
led.high()
utime.sleep(5)
else:
99
print("Waiting for movement")
led.low()
utime.sleep(0.2)
100
Connecting a Raspberry Pi Pico to
an Analog Joystick
101
Materials needed
Items needed
102
The Process
103
• SW to GP16 (physical pin 21). This
should work for the majority of GPIo
pins.
104
Chapter 7: MicroPython
Joystick Programming for
Raspberry Pi Pico
When you press down on an analog thumbstick,
you get three devices in one: an X axis
potentiometer, a Y axis potentiometer, and a
digital momentary button. We'll write some easy
code to see where the joystick is pointing in the
section below.
105
2. Import the required modules, including Pin,
ADC from machine, and utime
import utime
xAxis = ADC(Pin(27))
106
yAxis = ADC(Pin(26))
while True:
107
xValue = xAxis.read_u16()
yValue = yAxis.read_u16()
buttonValue= button.value()
108
utime.sleep(0.1)
109
7. To save space, halt the execution and delete
the print statement.
xStatus = "middle"
yStatus = "middle"
110
9. Within the loop, use if/then statements to
correctly label the joystick and button status.
xStatus = "left"
xStatus = "right"
yStatus = "up"
yStatus = "down"
111
if buttonValue == 0:
buttonStatus = "pressed"
import utime
112
xAxis = ADC(Pin(27))
yAxis = ADC(Pin(26))
while True:
xValue = xAxis.read_u16()
yValue = yAxis.read_u16()
buttonValue = button.value()
xStatus = "middle"
yStatus = "middle"
113
buttonStatus = "not pressed"
xStatus = "left"
xStatus = "right"
yStatus = "up"
yStatus = "down"
if buttonValue == 0:
114
buttonStatus = "pressed"
utime.sleep(0.1)
115
A low-cost piezo buzzer can be a great addition to
any Raspberry Pi Pico project, whether it's an
alarm clock that wakes you up to, a game that
beeps when you lose a turn or a beautiful display
that plays an 8-bit music. MicroPython makes
programming these low-cost buzzers a breeze.
They can also change the pitch to make musical
sounds.
116
The Pico can be connected to a Piezo buzzer and
programmed using MicroPython to generate a
short tune as shown below.
Items needed
• Raspberry Pi Pico.
117
How to Attach a Buzzer to a
Raspberry Pi Pico
Buzzer Programming
118
1. Import PWM and Pin from the machine library
first, followed by sleep from the utime library.
buzzer = PWM(Pin(15))
119
pitch of the sound will be. We have the option of
selecting 500.
buzzer.freq(500)
buzzer.duty_u16(1000)
120
continue to be heard even after the program has
finished running.
sleep(1)
buzzer.duty_u16(0)
• buzzer = PWM(Pin(15))
• buzzer.freq(500)
121
• buzzer.duty_u16(1000)
• sleep(1)
• buzzer.duty_u16(0)
122
Chapter 8: Playing Musical
Notes
You may use various frequencies to produce a
variety of musical notes and they are all possible.
A few lists of frequencies for musical notes can be
found online, and the majority of them can be
traced back to Brett Hagman’s Arduino tone
library on Github. These values will be used to
build a song list that we can use with our previous
code.
Let us begin!
buzzer = PWM(Pin(15))
● tones = {
● "B0": 31,
● "C1": 33,
124
● "CS1": 35,
● "D1": 37,
● "DS1": 39,
● "E1": 41,
● "F1": 44,
● "FS1": 46,
● "G1": 49,
● "GS1": 52,
● "A1": 55,
● "AS1": 58,
125
● "B1": 62,
● "C2": 65,
● "CS2": 69,
● "D2": 73,
● "DS2": 78,
● "E2": 82,
● "F2": 87,
● "FS2": 93,
● "G2": 98,
● "GS2": 104,
126
● "A2": 110,
● "AS2": 117,
● "B2": 123,
● "C3": 131,
● "CS3": 139,
● "D3": 147,
● "DS3": 156,
● "E3": 165,
● "F3": 175,
● "FS3": 185,
127
● "G3": 196,
● "GS3": 208,
● "A3": 220,
● "AS3": 233,
● "B3": 247,
● "C4": 262,
● "CS4": 277,
● "D4": 294,
● "DS4": 311,
● "E4": 330,
128
● "F4": 349,
● "FS4": 370,
● "G4": 392,
● "GS4": 415,
● "A4": 440,
● "AS4": 466,
● "B4": 494,
● "C5": 523,
● "CS5": 554,
● "D5": 587,
129
● "DS5": 622,
● "E5": 659,
● "F5": 698,
● "FS5": 740,
● "G5": 784,
● "GS5": 831,
● "A5": 880,
● "AS5": 932,
● "B5": 988,
● "C6": 1047,
130
● "CS6": 1109,
● "D6": 1175,
● "DS6": 1245,
● "E6": 1319,
● "F6": 1397,
● "FS6": 1480,
● "G6": 1568,
● "GS6": 1661,
● "A6": 1760,
● "AS6": 1865,
131
● "B6": 1976,
● "C7": 2093,
● "CS7": 2217,
● "D7": 2349,
● "DS7": 2489,
● "E7": 2637,
● "F7": 2794,
● "FS7": 2960,
● "G7": 3136,
● "GS7": 3322,
132
● "A7": 3520,
● "AS7": 3729,
● "B7": 3951,
● "C8": 4186,
● "CS8": 4435,
● "D8": 4699,
● "DS8": 4978
● }
133
3. Make a note list or an array for your song. Use
the letter P to represent the pauses in the song.
Each note should be enclosed in quotation marks.
Song =
["E5","G5","A5","P","E5","G5","B5","A5","P","E5",
"G5","A5","P","G5","E5"]
def playtone(frequency):
buzzer.duty_u16(1000)
buzzer.freq(frequency)
134
5. Make a function called bequiet that sets
duty_u16 to 0 and turns off the buzzer.
def bequiet():
buzzer.duty_u16(0)
def playsong(mysong):
135
for i in range(len(mysong)):
if (mysong[i] == "P"):
bequiet()
else:
playtone(tones[mysong[i]])
sleep(0.3)
bequiet()
136
numeric frequency value is needed before a tone
can be played. As a consequence, we must extract
each note number from the tonal list. For
example, if we just call playtone(mysong[i]), it will
fail because it will try to play the string "E5"
instead of the integer 659, which is required.
playsong(song)
137
When you run this code, the buzzer will play a
familiar sound. The full code should look like this:
buzzer = PWM(Pin(15))
● tones = {
● "B0": 31,
● "C1": 33,
● "CS1": 35,
138
● "D1": 37,
● "DS1": 39,
● "E1": 41,
● "F1": 44,
● "FS1": 46,
● "G1": 49,
● "GS1": 52,
● "A1": 55,
● "AS1": 58,
● "B1": 62,
139
● "C2": 65,
● "CS2": 69,
● "D2": 73,
● "DS2": 78,
● "E2": 82,
● "F2": 87,
● "FS2": 93,
● "G2": 98,
● "GS2": 104,
● "A2": 110,
140
● "AS2": 117,
● "B2": 123,
● "C3": 131,
● "CS3": 139,
● "D3": 147,
● "DS3": 156,
● "E3": 165,
● "F3": 175,
● "FS3": 185,
● "G3": 196,
141
● "GS3": 208,
● "A3": 220,
● "AS3": 233,
● "B3": 247,
● "C4": 262,
● "CS4": 277,
● "D4": 294,
● "DS4": 311,
● "E4": 330,
● "F4": 349,
142
● "FS4": 370,
● "G4": 392,
● "GS4": 415,
● "A4": 440,
● "AS4": 466,
● "B4": 494,
● "C5": 523,
● "CS5": 554,
● "D5": 587,
● "DS5": 622,
143
● "E5": 659,
● "F5": 698,
● "FS5": 740,
● "G5": 784,
● "GS5": 831,
● "A5": 880,
● "AS5": 932,
● "B5": 988,
● "C6": 1047,
● "CS6": 1109,
144
● "D6": 1175,
● "DS6": 1245,
● "E6": 1319,
● "F6": 1397,
● "FS6": 1480,
● "G6": 1568,
● "GS6": 1661,
● "A6": 1760,
● "AS6": 1865,
● "B6": 1976,
145
● "C7": 2093,
● "CS7": 2217,
● "D7": 2349,
● "DS7": 2489,
● "E7": 2637,
● "F7": 2794,
● "FS7": 2960,
● "G7": 3136,
● "GS7": 3322,
● "A7": 3520,
146
● "AS7": 3729,
● "B7": 3951,
● "C8": 4186,
● "CS8": 4435,
● "D8": 4699,
● "DS8": 4978
● }
● song =
["E5","G5","A5","P","E5","G5","B5","A5","P","E5",
"G5","A5","P","G5","E5"]
147
● def playtone(frequency):
● buzzer.duty_u16(1000)
● buzzer.freq(frequency)
● def bequiet():
● buzzer.duty_u16(0)
● def playsong(mysong):
● for i in range(len(mysong)):
● if (mysong[i] == "P"):
● bequiet()
148
● else:
● playtone(tones[mysong[i]])
● sleep(0.3)
● bequiet()
● playsong(song)
149
Chapter 9: Making Use of
the Raspberry Pi Pico With
an Arduino Board
The Arduino IDE, which uses “Arduino Language,”
a variant of C++, is now a third way to write code
for our Raspberry Pi Pico. Since Arduino has been
around for so long, there are a plethora of pre-
existing "sketches" (the Arduino term for
programs) and tutorials available. If you've ever
worked with Arduino boards, you're probably
already familiar with the IDE and language. In this
guide, we'll show you how to set up your
Windows or Ubuntu computer so you can
150
program your Raspberry Pi Pico with Arduino
code.
151
4. To install all of the applications, click Install.
This can take some time, and it can appear to be
stuck at times, but be patient.
https://github.com/earlephilhower/arduino-
pico/releases/download/global/package rp2040
index.json
152
https://github.com/earlephilhower/arduino-
pico/releases/download/global/package rp2040
index.json
153
Set the COM port for the Raspberry Pi Pico under
Tools >> Port.
154
Making Use of the Arduino For
Ubuntu
https://raw.githubusercontent.com/raspberrypi/p
ico-setup/master/pico setup.sh
155
3. Start the installation with pico setup.sh. If
prompted, type your sudo password.
156
6. To make the changes take effect, log out or
restart your computer.
https://github.com/earlephilhower/arduino-
pico/releases/download/global/package rp2040
index.json
157
10. In the search box, type "pico" and then select
the Raspberry Pi Pico / RP2040 board to install.
Another massive update, about 300MB in size, will
be triggered as a result of this.
dmesg $
158
14. Go to Files >> Examples >> Fundamentals >>
Blink to verify that we can program the Arduino.
159
Resetting Your Raspberry Pi Pico
Using a Button
160
If this occurs, we must disconnect and reconnect
the Pico in order for it to reset. Pulling out the
micro USB lead repeatedly, which has a fixed
insertion number rating will wear it out. If the Pico
is connected to a powerful USB hub with off/on
buttons, we can simply press the off/on buttons.
What are our options if we don't want this?
Items needed
• A Raspberry Pi Pico.
161
• Push-buttons
Procedure
162
4. Connect the other jumper wire to be in line
with the GND. Then connect the wire's other end
to the top right corner of the button.
163
This button makes flashing a C/C++ project or
MicroPython firmware much simpler.
164
3. Save your MicroPython firmware UF2 file or
your C/C++ project to the drive. This will cause it
to reboot automatically.
165
Conclusion
This guide has detailed extensively how to
maximize the Raspberry Pi Pico to its full
potential, especially for newbies. Hopefully, you
get acclimatized to the procedures and get
creative with your Raspberry Pi Pico.
166