Maker Advent Calendar Day #1: Getting Started!
Welcome to the first day of your 12 Projects of Codemas Advent Calendar. Excited? We are too!
This twelve-day advent calendar will teach you how to code with MicroPython using a Raspberry Pi Pico, using software along with a selection of fun, popular components along the way. We’re going to use code to make hardware interact with the real world!
Each day we’ll show you how to use the fun things you find in each box, explaining what they are, what they do, how to construct the circuit and then how to code them with MicroPython using simple methods.
The best bit – once you’ve completed the 12 projects of Codemas, you’ll have a Raspberry Pi Pico along with a great stash of components, with the knowledge and confidence to make your own projects with them!
Day #1: Getting Started
You’ll have already noticed that the first box is larger than most of the others. This is to give everything you need to build the foundations for the fun projects you’ll be making over the next 12 days.
Box #1 Contents
In this box you will find:
- 1x Raspberry Pi Pico H (with headers pre-soldered)
- 1x 1m Micro-USB cable
- 1x 400-point solderless breadboard
Today we’re going to get you set-up and familiar with the Raspberry Pi Pico, including installing the software required to program the board as well a running a couple of very easy projects to get to grips with MicroPython.
Introducing the Raspberry Pi Pico
The Raspberry Pi Pico is a different kind of Raspberry Pi though - it's a microcontroller board, not a microcomputer. The brain of the Pico is the RP2040 microcontroller chip, the little black square in the middle.
Whereas you might use a Raspberry Pi 4 (which has an operating system) to play games, write stories and browse the web, the Raspberry Pi Pico is designed for physical computing projects where it can control components such as LEDs, buttons, sensors, motors and even other microcontrollers.
The board has lots of pins around it called GPIO (General Purpose Input Output) pins, which allow you to connect all sorts of components to make projects. These are controlled by code that you write. A map of the Pico's pins is available here - don't worry too much about the different pins and labels just yet, we'll be covering this over the next twelve days.
The Pico can use many different programming languages, however the most popular is MicroPython which is what we'll be using over the next 12 days.
The board has a Micro-USB port which gives it power and allows it to communicate to our computer for programming. There's a lot more to this little board, but we'll keep to the basics for now.
We’re going to install software called Thonny, which will allow us to program the Pico with MicroPython.
MicroPython is an smaller, more efficient version of the Python 3 programming language, designed to be used with microcontroller boards such as the Raspberry Pi Pico. If you've programmed with Python previously, MicroPython will feel very familiar - but don't worry - we're going to guide you every step of the way over the next 12 days.
Thonny is a Python IDE. An IDE is an Integrated Development Environment. IDEs help us to write code by giving us lots of helpful features to avoid errors, make our code more readable and lots of other benefits.
So, let's get Thonny! Head over to https://thonny.org/ and look for this little box:
If you hover over the system you’re using (we’re using Windows in this example) you should see some options, with a recommended option based on the system you’re using. For us, using a 64-bit version of Windows, it’s recommending the 64-bit version. Your system may be different, so it’s best to just go with the version the website recommends:
Download the correct version for your computer then open the file to start the installation.
Thonny installs like most programs, and will ask you to accept the license agreement and confirm where to install it. We kept all the options as default and it took just under a minute to install.
Once installed, select ‘Finish’ and then open the program. In Windows 10/11 you can select the Windows Start button and just type in ‘Thonny’. The program should open and look like the example below:
Install MicroPython on the Raspberry Pi Pico
We now need to install the MicroPython firmware on the Raspberry Pi Pico. This gives the Pico the basic software it needs to run MicroPython code that we'll send to it later.
Important Note: A handful of customers are experiencing an issue when using the method we have suggested below (SSL errors and other unfestive nasties). Give it a try, but if you have trouble installing MicroPython on your Pico, try the other method here instead. Thanks to everyone who let us know about this issue.
Plug the Pico into the breadboard (the end where the number 1 is) then plug the Micro-USB cable into the Pico. Your Pico should be fully inserted so that you can't see any of the metal legs/pins. The black plastic on the underside of the Pico should sit flush with the top of the Breadboard. Apply strong pressure evenly at both ends:
Now locate the little button on top of your Pico. This is the ‘BOOTSEL’ button which, when held in whilst connecting the cable, allows us to install different firmware to the Pico.
Hold down that button whilst plugging in the USB cable into your computer’s USB port at the same time. You may see it pop up as a new device on your computer but just ignore that for now.
In Thonny, at the bottom-right-hand corner, you’ll see something like 'MicroPython (Raspberry Pi Pico)' and a COM port, like the example below (this may vary but it doesn’t matter at this stage). Click on this and you’ll see a few options. Select ‘Install MicroPython…’
You’ll now be presented with some options in a new dialogue box. Use the same options as per the screenshot below, selecting the RPI-RP2 target volume, Raspberry Pi Pico / Pico H variant, and whatever the latest version is available (at the time of writing it was 1.19.1):
Select ‘Install’ and you’ll see a small progress bar whilst it installs. Once complete, select ‘Close’.
Setting up Thonny
Thonny can be used with a range of development boards, so we need to tell Thonny that we are using a Raspberry Pi Pico.
From the top menu, select ‘Run’ then ‘Configure interpreter’:
Now just make sure that ‘MicroPython (Raspberry Pi Pico)' is selected in the first drop-down box, select the available device in the ‘Port’ drop-down, then select ‘OK’.
Tip: If you see multiple options in the Port drop-down box, it just means you have other devices connected to your computer. If you’re not sure which one is the Pico, make a note of what COM ports are showing, disconnect the Pico from the USB port, close the Thonny options box, then try again. Whichever one is now missing was the Pico, so connect it again and select that device next time (it’s a lot easier than disconnecting everything else!)
You should now see a ‘Raspberry Pi Pico’ panel on the left-side of Thonny, and information in the bottom-right 'Shell' panel.
Tip: If you don't see your Pico on the left, try selecting 'View' from the top navigation bar, and selecting 'Files'.
That’s the setup out of the way, we’re ready to code!
Activity 1: Your First Program - Print
Let’s make a very simple program and run it on your Pico.
We’ll tell the Pico to ‘print’ some words (known as a 'string' of text). These words/strings are then 'printed' to the Shell window. The print function is something you’ll use a LOT when programming. It’s a useful tool to see what’s happening with a program, what part of a program is running and can help when debugging projects.
In the top panel (which should be called <untitled> at the moment as we haven't saved it yet), copy/paste or type in enter the following line and then select the green run icon (it looks like a ‘Play’ button) from the menu.
print("This is my Pico talking")
You should now see the following in the Shell window:
You did it! Your first program!
A few things to note:
- Print text strings always sit within inverted commas, within brackets
- Letter case is very important with MicroPython and other code languages. Our print is always 'print' and never 'Print'.
Now try changing the text in between the inverted commas - you can write anything you want…“This calendar is going to be awesome” or “I don’t like pineapple on pizza”…or even the famous "Hello, World!".
You can even add multiple print lines one after the other - give it a try!
Activity 2: Light the Onboard LED
We now know where to write code in Thonny and how to run a program. We'll be doing a lot more of that over the coming days, so for now let's jump straight into a bit of blinky!
The Pico comes with a little LED onboard, which is attached to one of the Pico’s RP2040 pins, GPIO25. This pin isn't one of the physical pins available around the edge of the board.
We can tell our program to turn this pin on and off whenever we like, which is a nice easy first hardware project and can be handy as an indicator for your own projects later on. This is the beginning of getting your code to control physical things!
Inputs and outputs
Our GPIO pins (the green ones from the pin map) can be set as inputs or outputs. We use inputs when we want something to send signals/voltage in to the Pico, like a button or a sensor, and we use outputs when we want the Pico to send signals/voltage out to something else, like LEDs or buzzers.
We’re going to use an output here, which will send voltage to the onboard LED to light it up.
Take a quick glance at the first line of the example code below. When we write code in Thonny, a lot of the time we need to add imports at the start.
Imports bring in code from other places (called 'modules' or 'classes' - but let's not get bogged down with that for now) that we need to make our program work. Usually these are from built-in modules.
To use the GPIO pins, we always need to import Pin.
Take another look at the example below. So, what is it doing?
- The first line imports ‘Pin’ from the 'machine' module on the first line, which simply allows us to use the GPIO pins.
- The second line is setting up one of the pins to allow us to use it in our code. This sets up GPIO25 with the name ‘onboardLED’. We use Pin.OUT at the end there to tell MicroPython to set this pin as an output.
- The third line then finally sets that pin value to ‘1’ which is HIGH (on). 1 is always used for HIGH (on) and 0 is used for LOW (off).
from machine import Pin onboardLED = Pin(25, Pin.OUT) onboardLED.value(1)
Copy that code to your top-right panel, then hit the run button. The LED should light up – hooray – you’ve controlled your first physical component!
Now try changing the 1 to a 0 - run the code again and it should turn the LED off. You can also try adding a print line at the bottom to show a message at the same time.
Day #1 Complete!
Well done fellow maker, today you have:
- Become an owner of your very own microcontroller!
- Learnt about the Raspberry Pi Pico
- Set up your Raspberry Pi Pico with Thonny
- Created your first program
- Learnt about the print function
- Controlled a physical component with code
Now, get some rest and a good night’s sleep, for tomorrow we will continue the fun with day #2 (no spoilers here, and no peeking until the morning!).
We used Fritzing to create the breadboard wiring diagram images for this page.