when people think of ‘programming’ or ‘coding’, they’re usually and naturally thinking about software. Coding can be about more than just software, though: itcan affect the real world through hardware. This is known as physical computing. As the name suggests, physical computing is all about controlling things in the real world with your programs: hardware, rather than software. For starting with physical computing the device that is used for sensing and changing the things in real world is a microcontroller. For physical computing lots of microcontroller or microcontroller development boards are available in today market. i.e. Arduino, ESP32, ESP8266 and STM32 etc.
Raspberry pi is known for their computer boards they developed but before few years ago raspberry pi has developed its first microcontroller chip RP2040 in UK. The Raspberry pi pico is a development board based on this chip. Raspberry pi pico can be programmed by connecting it to a computer through a micro USB cable. This development board supports following programming languages for programming:
C/C++
Micro python
Circuit python
Assembly
Here In this tutorial I am using only micro python. I shall discuss on other programming languages in my future projects. So let's get started.
After reading this complete tutorial carefully.
What you will make?
You will connect a Raspberry Pi Pico to your computer, install the Thonny Python IDE, and write a MicroPython program to blink the onboard LED. If you have additional components available, then you can also try out some more examples.
What you will learn?
- How to load the MicroPython firmware onto a Raspberry Pi Pico
- How to program a Raspberry Pi Pico using MicroPython
- How to connect additional components to a Raspberry Pi Pico and write MicroPython programs to interact with them
Hardware Requirements:
You need some hardware for getting started. The list is given below:
- A Raspberry Pi Pico with soldered headers
- A computer that can run the Thonny IDE and program a Raspberry Pi Pico
- A micro USB cable
- A selection of electronics components, such as an LED with appropriate resistor.
- A breadboard and M-M jumper leads for connecting additional components (optional)
Software Requirements:
You need following software:
- MicroPython firmware for Raspberry Pi Pico
- The Thonny Python IDE
This is a Raspberry Pi Pico. Hopefully your device has already had the header pins soldered on, but if not, you might like to have a look at our Getting started with soldering resource.
If you need to know the pin numbers for a Raspberry Pi Pico, you can refer to the following diagram.
In this step, you will install Thonny or make sure you have the latest version. Then you will connect to a Raspberry Pi Pico and run some simple Python code using the Shell.
- On Windows, macOS, and Linux, you can install the latest Thonny IDE or update an existing version
- In a web browser, navigate to thonny.org
- In the top right-hand corner of the browser window, you will see download links for Windows and macOS, and instructions for Linux
- Download the relevant files and run them to install Thonny
Open Thonny from your application launcher. It should look something like this:
You can use Thonny to write standard Python code. Type the following in the main window, and then click the Run button (you will be asked to save the file).
print('Hello World!')
You’re now ready to move on to the next step and connect your Raspberry Pi Pico.
Step-2: Add the MicroPython firmwareIf you have never used MicroPython on your Raspberry Pi Pico, you will need to add the MicroPython firmware.
Find the BOOTSEL button on your Raspberry Pi Pico.
Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable to your raspberry pi pico.
This puts your Raspberry Pi Pico into USB mass storage device mode.
In the bottom right-hand corner of the Thonny window, you will see the version of Python that you are currently using.
Click on the Python version and choose ‘MicroPython (Raspberry Pi Pico)’:
If you don’t see this option, then check that you have plugged in your Raspberry Pi Pico.
A dialog box will pop up to install the latest version of the MicroPython firmware on your Raspberry Pi Pico.
Click the Install button to copy the firmware to your Raspberry Pi Pico.
Wait for the installation to complete and click Close.
You can also access the firmware installation menu if you click on ‘MicroPython (Raspberry Pi Pico)’ in the status bar and choose ‘Configure interpreter …’.
The interpreter settings will open.
Click on Install or update firmware.
Wait for the installation to complete and click Close.
You don’t need to update the firmware every time you use your Raspberry Pi Pico. Next time, you can just plug it into your computer without pressing the BOOTSEL button.
Step-3: Use the ShellIn this step, you will use the Thonny Shell to run some simple Python code on your Raspberry Pi Pico.
Make sure that your Raspberry Pi Pico is connected to your computer and you have selected the MicroPython (Raspberry Pi Pico) interpreter.
Look at the Shell panel at the bottom of the Thonny editor. You should see something like this:
Thonny is now able to communicate with your Raspberry Pi Pico using the REPL (read–eval–print loop), which allows you to type Python code into the Shell and see the output.
Now you can type commands directly into the Shell and they will run on your Raspberry Pi Pico.
Type the following command.
print("Hello")
Tap the Enter key and you will see the output:
MicroPython adds hardware-specific modules, such as machine
, that you can use to program your Raspberry Pi Pico. Let’s create a machine.Pin
object to correspond with the onboard LED, which can be accessed using GPIO pin 25.
If you set the value of the LED to 1
, it turns on. Enter the following code, make sure you tap Enter after each line.
from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)
You should see the onboard LED light up.
Type the code to set the value to 0
to turn the LED off.
led.value(0)
Turn the LED on and off as many times as you like.
Tip: You can use the up arrow on the keyboard to quickly access previous lines.
If you want to write a longer program, then it’s best to save it in a file. You’ll do this in the next step.
Step-4: Blink the onboard LEDThe Shell is useful to make sure everything is working and try out quick commands. However, it’s better to put longer programs in a file.
Thonny can save and run MicroPython programs directly on your Raspberry Pi Pico.
In this step, you will create a MicroPython program to blink the onboard LED on and off in a loop.
Click in the main editor pane of Thonny. Enter the following code to toggle the LED.
from machine import Pin
led = Pin(25, Pin.OUT)
led.toggle()
Click the Run button to run your code.
Thonny will ask whether you want to save the file on This computer or the MicroPython device. Choose MicroPython device.
Enter blink.py
as the file name.
Tip: You need to enter the .py
file extension so that Thonny recognises the file as a Python file.
Thonny can save your program to your Raspberry Pi Pico and run it. You should see the onboard LED switch between on and off each time you click the Run button.
You can use the Timer
module to set a timer that runs a function at regular intervals.
Update your code so it looks like this:
from machine import Pin, Timer
led = Pin(25, Pin.OUT)
timer = Timer()
def blink(timer):
led.toggle()
timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)
Click Run and your program will blink the LED on and off until you click the Stop button. Save your project
Step-5: Use digital inputs and outputsNow you know the basics, you can learn to control an external LED with your Raspberry Pi Pico, and get it to read input from a button.
Use a resistor between about 50 and 330 ohms, an LED, and a pair of M-M jumper leads to connect up your Raspberry Pi Pico as shown in the image below.
In this example, the LED is connected to pin 15. If you use a different pin, remember to look up the number in the pinout diagram in the Meet Raspberry Pi Pico section.
Use the same code as you did to blink the onboard LED, but change the pin number to 15
.
from machine import Pin, Timer
led = Pin(15, Pin.OUT)
timer = Timer()
def blink(timer):
led.toggle()
timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)
Run your program and your LED should start to blink. If it’s not working, check your wiring to be sure that the LED is connected.
Comments