How To Program A Raspberry Pi Pico With MicroPython?

Arunkl
TheSecMaster
Published in
6 min readMar 16, 2023
Raspberry pi logo, Raspberry pi motherboard, Python logo on a yellow background with post titles.
Source: thesecmaster.com

If you read “How To Boot Raspberry Pi Pico for the First Time?” then you might have gotten an idea about the Raspberry Pi Pico. In that post, we covered most of the basic things required to know about the Raspberry Pi Pico. Please don’t skip reading that post if you are not aware of the Raspberry Pi Pico. Just to tell you about the Raspberry Pi Pico, in short, it’s not a computer. It’s a small microcontroller board that is powerful enough to run programming languages like C and Python and allows you to do pretty much anything that you need to do for small electronic projects. Well, you can’t run the standard Python compiler on the Pico to control its hardware. You need to have MicorPython installed on it to program a Raspberry Pi Pico. We will show you how to install MicroPython on a Raspberry Pi Pico and how to program a Raspberry Pi Pico with MicroPython, in this post. Let’s get started.

Table of Contents

· About MicroPython:
· How To Program A Raspberry Pi Pico With MicroPython?

About MicroPython:

MicroPython is a tiny open-source Python programming language interpreter that runs on small embedded development boards. MicroPython is designed to be as compatible with regular Python as possible (within reason) to allow you to transfer code with ease from desktop computers to microcontrollers and back.

MicroPython aims to be as practical as regular Python for everyday use on microcontrollers. MicroPython implements a subset of the Python 3 standard library and is extended to include modules for microcontroller-specific hardware. MicroPython runs bare-metal on microcontrollers and thus can be used to program them without an operating system. MicroPython is also packed with many features such as an integrated development environment (IDE), an object-relational mapper (ORM), and support for remote code repositories.

MicroPython is licensed under the MIT license, making it free and open-source software. MicroPython is currently developed by Damien George, with the latest releases available for download from MicroPython.org.

MicroPython is a great way to get started programming microcontrollers. By being able to program in MicroPython, you can develop code on your computer and then easily transfer it to a microcontroller to run. MicroPython is also very easy to use, thanks to its small size and feature-rich set of libraries. If you’re looking for a way to get started programming microcontrollers, MicroPython is a great option.

How To Program A Raspberry Pi Pico With MicroPython?

You can program a Raspberry Pi Pico using C, Python, or any other programming language that supports Pico. However, we will show you how to program a Raspberry Pi Pico with MicoroPython, a subsidiary of Python. Before you program a Raspberry Pi Pico, it is a must to install it. Let’s see how to install MicroPython on a Raspberry Pi Pico.

Time needed: 30 minutes.

How to install MicroPython on a Raspberry Pi Pico?

There are multiple ways to install MicroPython on Raspberry Pi Pico. Let’s see how to install MicroPython on a Raspberry Pi Pico using Thonny Python IDE in this post.

1. Install MicroPython on Raspberry Pi Pico using Thonny Python IDE.
2. Install MicroPython on Raspberry Pi Pico using BOOTSEL mode and flash the UF2 file.
3. Building MicroPython From Source.

  1. Boot the Raspberry Pi Pico in BOOTSEL mode

We are going to show you to boot the Raspberry Pi Pico in BOOTSEL mode by connecting to a Raspberry Pi. However, you can boot the Pico using any other Windows or Linux computer either if you don’t have Raspberry Pi with you. The reason to connect the Pico to the Pi is that Thonny Python IDE comes in Pi as part of the default packages. If you want to do this using Windows or Linux computer, then you should install Thonny Python IDE on your computer.

Connect your Raspberry Pi Pico to the Raspberry Pi through a micro USB cable by holding the BOOTSEL button (White color button near the micro USB connector) located on the Pico board.

Your Pico will be mounted as a mass storage drive on the Raspberry Pi as an RPI-RP2 drive.

Boot the Raspberry Pi Pico in BOOTSEL mode

2. Open Thonny Python IDE on the Raspberry Pi Pico

Click on the Raspberry Pi icon then go to Programming and click on Thonny Python IDE

Open Thonny Python IDE on the Raspberry Pi Pico

3. Connect your Raspberry Pi Pico to Thonny Python IDE

It is simple to connect your Pico to Thonny Python IDE. All you need to do is, click on the Python at the bottom right corner and select the MicorPython (Raspberry Pi Pico)

Connect your Raspberry Pi Pico to Thonny Python IDE

4. Install MicroPython on the Raspberry Pi Pico

Immediately after you select the MicorPython (Raspberry Pi Pico), the installation wizard will pop up. Click on the Install button to begin the installation of MicruPython.

Install MicroPython on the Raspberry Pi Pico

5. Raspberry Pi Pico is ready to program

After the completion of the installation of MicroPython, Raspberry Pi Pico is ready to program.

Raspberry Pi Pico is ready to program

6. Turn on the LED on the Raspberry Pi Pico board using Python

We will show you how to use Pin number 25 to operate LED on the Raspberry Pi Pico board.

You just need to import Pin and create a variable method name ‘led’. And, call the high method to turn the LED on.

>>> from machine import Pin
>>> led = Pin(25,Pin.OUT)
>>> led.high()
>>>

Turn on the LED on the Raspberry Pi Pico board using Python

7. Turn off the LED on the Raspberry Pi Pico board using Python

Call log method to turn the LED off

>>> from machine import Pin
>>> led = Pin(25,Pin.OUT)
>>> led.high()
>>> led.low()
>>>

Turn on the LED on the Raspberry Pi Pico board using Python

8. Blink the LED on the Raspberry Pi Pico using the Python script

Run this small script to blink the LED on your Pico board.

from machine import Pin
import utime

led = Pin(25,Pin.OUT)

delay = 0.25

while True:
led.high()
utime.sleep(delay)
led.low()
utime.sleep(delay)

Blink the LED on the Raspberry Pi Pico using the Python script

We have shown you how to blink the LED as an example to program your Raspberry Pi Pico board to control hardware. You can program any PIN using Python like this.

We hope this post would help you learn how to install MicroPython on a Raspberry Pi Pico and how to program a Raspberry Pi Pico with MicroPython. Thanks for reading this tutorial post. Visit our social media page on Facebook, LinkedIn, Twitter, Telegram, Tumblr, & Medium and subscribe to receive updates like this.

This post is originally published at thesecmaster.com

We thank everybody who has been supporting our work and request you check out thesecmaster.com for more such articles.

--

--