Table of Contents
  • Home
  • /
  • Blog
  • /
  • How To Program A Raspberry Pi Pico With MicroPython?
January 24, 2024
|
7m

How To Program A Raspberry Pi Pico With MicroPython?


How To Install Micropython On A Raspberry Pi Pico And How To Program A Raspberry Pi Pico With Micropython

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 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.

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), 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.

Hardware Prerequisites:

Software Prerequisites:

  • Thonny Python IDE installed on a Raspberry Pi or other Linux/Windows computer

  • MicroPython firmware installed on the Raspberry Pi Pico

  • Basic knowledge of Python programming

The post covers the process of installing MicroPython firmware on the Pico using the Thonny Python IDE. This involves booting the Pico into BOOTSEL mode, connecting it to Thonny, and initiating the firmware installation process.

Once MicroPython is installed, the Pico can be programmed using Python code. The post shows a simple example of turning an LED on and off using Python code to control a pin on the Pico.

So in summary, the key prerequisites are:

  • Raspberry Pi Pico hardware

  • Thonny IDE with MicroPython support

  • Understanding of basic Python syntax

  • Ability to connect the Pico to Thonny in BOOTSEL mode

  • Installing the MicroPython firmware on the Pico

With these elements covered, you will have what you need to start programming the Raspberry Pi Pico using MicroPython, as covered in the blog post. Let me know if you need any clarification or have additional questions!

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.

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 

mode and flash the UF2 file.3. Building MicroPython From Source.

Step 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. 

Step 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

Step 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)

Step 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.

Step 5. Raspberry Pi Pico is ready to program

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

Step 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()>>>

Step 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()>>>

Step 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 Pinimport utime
led = Pin(25,Pin.OUT)
delay = 0.25
while True:led.high()utime.sleep(delay)led.low()utime.sleep(delay)

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 FacebookLinkedInTwitterTelegramTumblrMedium & Instagram, and subscribe to receive updates like this.

You may also like these articles:

Arun KL

Arun KL is a cybersecurity professional with 15+ years of experience in IT infrastructure, cloud security, vulnerability management, Penetration Testing, security operations, and incident response. He is adept at designing and implementing robust security solutions to safeguard systems and data. Arun holds multiple industry certifications including CCNA, CCNA Security, RHCE, CEH, and AWS Security.

Recently added

How To

View All

Learn More About Cyber Security Security & Technology

“Knowledge Arsenal: Empowering Your Security Journey through Continuous Learning”

Cybersecurity All-in-One For Dummies - 1st Edition

"Cybersecurity All-in-One For Dummies" offers a comprehensive guide to securing personal and business digital assets from cyber threats, with actionable insights from industry experts.

Tools

Featured

View All

Learn Something New with Free Email subscription

Subscribe

Subscribe