Image by Rainer Stropek on Flickr
An introduction to using MicroPython with Pico Boards!
The Raspberry Pi Pico is a microcontroller board from Raspberry Pi that is incredibly powerful and yet pretty straightforward to start programming and learning with. It’s affordable too at $4 and you can program it with pretty much any computer, including a Raspberry Pi!
The Pico can be programmed in numerous languages and the code can be created using different software programming environments. In this guide we are going to program the Pico using the MicroPython language. There are lots of tutorials and support material for all kinds of projects on the Pico using MicroPython online and there are lots of people using MicroPython so it’s a good language if you are a beginner and you need to ask online for help or research for a solution.
First let’s install some brilliant free software to program our Pico. It’s called “Thonny” and is available at https://thonny.org/. Download and install Thonny on your computer. Next grab your Pico and plug a USB cable into it and press and hold the small button on the Pico whilst you plug the USB into the computer. If it’s the first time you have ever plugged your brand new Pico into a computer you don’t need to hold the button down.
Launch Thonny and click the “run” drop down menu. Click “select interpreter” and in the dialogue box select “Micropython Raspberry Pi Pico”. If everything has gone well it should detect which port your Pico is connected too. Before we close this dialogue box you should see a “install or update firmware” link in the bottom corner. Click this button and Thonny will rather brilliantly download and install the latest MicroPython firmware onto your Pico. Don’t worry, if you decide later to use a different programming language like C or CircuitPython you can replace this firmware as many times as you like.
Once the firmware is installed we are ready to start tinkering with code on the Pico. There are a couple of different ways we can do this. We can directly input code to the Pico via the “REPL” “read Evaluate Print and Loop” which also can be called the “shell”. We can see the shell input and output window in the lower left hand corner of the Thonny environment. Any commands written in the shell will immediately be read by the Pico which evaluates them and prints any response needed back to the shell window. It then loops round to be ready to read the next command.
The classic first command to write into a shell is
print (“Hello World”)
If you hit the return/enter key on your computer you should see that the shell window prints “Hello World”. Another simple fun experiment for the shell is that it understands mathematical operations without much special code. Try inputting;
and then hitting the enter key. The shell evaluates this, interprets that it needs to perform the maths operation and then prints the answer back to the window for you to read.
The REPL is useful for experiments and for trying to get small bits of code ideas working well. However if you turn off and on (or disconnect and reconnect) your Pico you’ll notice that nothing is saved from your previous REPL session. The other way to run code on a Pico is to write the code and save it as a .py file on the Pico device itself. You can save more than one MicroPython script on the Pico and you can choose which one to run using Thonny or you can set it up to run a particular program whenever the Pico is powered on which is useful for projects you don’t want attached to the computer!
Type the following code into the code input box, take care to make sure that it matches and notice that all the lines after while True: are indented. Thonny should do this automatically for you as it detects that they need to be.
import machine import utime led= machine.Pin(25, machine.Pin.OUT) while True: led.value(1) utime.sleep(1) led.value(0) utime.sleep(1)
If you click the “run” button that looks like a green play button Thonny will open a save window suggesting you save the file to the attached Pico. Give your file a name such as blink.py and hit the OK button. You should now see on the Pico that the built in LED which is attached to pin 25 should start to blink on and off. As a next experiment, try changing the sleep values in the code to make the LED go on and off faster and slower. If you do this and save the file with a different name you’ll notice that the Pico now has multiple files on it. This is great as the Pico acts as its own storage and you can open these files and edit them as you would any file.
If you wanted your blink sketch to work without having to plug in the pico to Thonny and clicking run, rename the file to main.py. A MicroPython file called main.py automatically will be run whenever the pico is powered up.
Finally, if you are inspired to learn more about programming the Pico with MicroPython there is an excellent book you can buy in print or download for free from Raspberry Pi themselves, it’s called Getting Started with MicroPython on Raspberry Pi Pico and it’s at https://hackspace.raspberrypi.org/books/micropython-pico.
Getting started with Pico
How to set up pico with thonny
Raspberry Pi Pico
Also In The October 2021 Issue
30+ ideas for STEAM-theme gifts for kids of all ages!
Learn how to use HTML and CSS with this great book!
An introduction to using MicroPython with Pico Boards!
Everyone knows the difference between saying, “Let’s eat, grandma!” and “Let’s eat grandma!” Computers don't.
Join us as we explore how computers and people can create art differently!
Join us as we weave together how looms and binary data are comparable!
Time for lesson two on how to make a fairy tale in Scratch!
Meet Pyret! Pythons colorful and graphically based cousin!
Discussing the importance of presenting problem-solving to students as a process.
Meet a 'Colossus' in the world of early encryption!
We all use fonts yet rarely notice they are designed. Here are some interesting details to help you notice fonts.
Looking to put your programming skills to the test? It may be time to join an open-source project!
Exploring the relationship between operators, negation, and exclamation points!
Learn how sports and technology are becoming more and more linked!
What is a G and why do we have 5 of them now? Find out through this quick history of cellular data!
Links from the bottom of all the October 2021 articles, collected in one place for you to print, share, or bookmark.
A collection of fun and inspiring stories from October 2021.