MicroPython

Raspberry Pi Pico is the latest member of the Raspberry-Pi line-up, and it has many unique features that set them apart from their peers. On top of being able to program in C or C++, this microcontroller can also be programmed with MicroPython – a programming language created by Damien George! With its streamlined coding process and ability to run on smaller devices than most competitors, this little guy doesn’t just have great potential for all those who want easy access into computing; more importantly it presents an opportunity to broaden how code literacy will shape our future.

What is MicroPython?

MicroPython is a lean version of Python specially made for microcontrollers, It’s specially designed and written in an easy-to-understand syntax pattern just like regular python. Plus it includes full access over all of its inputs/outputs(GPIO’s), So that you can connect and control other programmable components like motors or sensors. Making it famous among computer programmers. This language follows the same syntax pattern as standard Python.

If you’re new to Micropython, it will be great for you because it’s simpler and more accessible compared to other programming languages but also providing enough power for industrial use. Packed with advanced features such as

  • Dynamic prompt(REPL)
  • Arbitrary precision integers
  • Closures
  • List comprehension
  • Generators
  • Exception handling.

Micropython remains compact enough to accommodate and run within 256k of code space and 16k of RAM.

Are you using Python? Don’t worry, Micropython also supports most of the Python syntax that Python’s regular users may find familiar with.

Requirements to get started with MicroPython on Raspberry Pi Pico 

The official Raspberry Pico documentation basically focuses on the use of the raspberry pi (read Rpi-4) as a host computer, and this is suitable for anyone who wishes to operate with micro python. If you’re operating through micro python (which I do), then any old pc will work – windows, mac or Linux – so long as it’s got an IDE named Thonny downloaded onto it!

But if you’re operating with C, then I suggest that you use a Linux-based computer like the Raspberry Pi-4 computer (if you have one), so it’s easy to download the SDK and write C programs to Linux. Also, if you’re interested in debugging the code, then Raspberry Pi will surely help you as we will use some of the Raspberry Pi GPIO for debugging Raspberry Pi Pico.

  • The first and foremost thing which you obviously require is a Raspberry Pi Pico microcontroller board.
  • A Windows/Mac/Linux computer device on which programming is possible.
  • A USB-A to micro USB cable for transferring the program from your computer to Pico.
  • Python of version 3.6+ to be there on the computer.

Installation of MicroPython on Pico

MicroPython

Firstly, you need to install MicroPython on Raspberry Pi Pico by first downloading the most recent version of MicroPython for Pico on the computer. You will find the file in the uf2 extension which you have to transfer to the pico board. For this, you need to press and hold the BOOTSEL on your pico while plugging in the micro USB end of the cable and the USB-A end to your computer. 

You then need to transfer the uf2 file by dragging and dropping it to this new disk drive(Your Pico). Once you are done with the file transfer, the Pico board will reboot itself and start again with MicroPython download in it.

Let’s discuss the installation with the following steps:

STEP1: Firstly install MicroPython Binary

Now we will start the installation of Raspberry Pico with MicroPython. The simplest and shortest way to run MicroPython on Raspberry Pi Pico is to download the pre-built binary from the official Raspberry Pi Pico website. Go to the Raspberry Pi Pico documentation page and press the “Get Started MicroPython” button.

The text below the tab varies according to the chosen tab and when you press “Get Started MicroPython,” a text related to Getting Started with MicroPython appears along with a short animation about how to install MicroPython on Raspberry Pi Pico.

Read all the details and press the “Download UF2 File” button. The MicroPython Binary in the form of a.uf2 file will be downloaded.

STEP2: Install MicroPython on Raspberry Pi Pico

After installing the MicroPython Binary, you have to firstly upload this firmware to the Raspberry Pico. For that, you have to put the Pico in bootloader mode first. To do this, plug in the micro-USB cable to the Raspberry Pico micro-USB port. Now keep the BOOTSEL button on the Pico and attach the other end of the USB cable to the USB port of the host device (while holding the BOOTSEL button).

After a few seconds, you can release the button when the Raspberry Pi Pico appears as a mass storage device with the name “RPI-RP2.” You can see a text file and an HTML file if you open it.

Now, go to your downloads folder and drag and drop the installed MicroPython UF2 file to the RPI-RP2 device. After copying, the Pi Pico Raspberry will automatically restart and run MicroPython. After copying the MicroPython UF2 file, the mass storage device will vanish.

Your Raspberry Pico will run the MicroPython now. You’re ready to program Raspberry Pi Pico with MicroPython.

Installing Thonny

If your host device is either Linux or Mac, you can use the terminal and Minicom to connect with Raspberry Pi Pico. But in this tutorial, we’ll see how to program Raspberry Pi Pico using Thonny IDE. Thonny is a basic Python IDE that is available for Windows, Mac, and Linux.

The Raspberry Pi OS comes with Thonny pre-installed. Since I’ve been using the Windows system, I’ve downloaded the Thonny version of Windows. An executable called “thonny-3.3.5.exe” is downloaded. Double click the downloaded executable and download Thonny. There’s nothing remarkable about this installation because it’s really simple. Optionally, you can choose to build a desktop shortcut.

Configuring Thonny

After you have downloaded and installed Thonny IDE, open it. Make sure the Raspberry Pico is still plugged into the host computer. The Thonny IDE is quite easy. The layout can be divided into four parts: Toolbar, Script Area, Shell, Interpreter.

  • Toolbar: contains icons to save, run, and interrupt programs.
  • The Script Area: This is where you are writing the Python programs.
  • The Shell: The Python Shell is an interactive REPL (Read-Evaluate-Print-Loop) block where you can send the interpreter individual commands and execute them.
  • Interpreter: Choose the correct interpreter from the bottom right of the IDE.
  • Thonny IDE is configured to interpret Python 3.x.x by default.

Click on Python 3.7.9 (or whichever version you have) and choose MicroPython (Raspberry Pi Pico) interpreter. As soon as you choose a MicroPython interpreter, the bottom shell switches to MicroPython.

Since MicroPython supports interactive REPL, you can insert commands in your shell, and Raspberry Pi Pico will execute them. Let’s do it. We’re going to start with Hello World programs that are going to print Hello World.

Programming the Pico

MicroPython

The Thonny Python IDE is one of the best-dedicated IDEs to program in MicroPython on your Raspberry Pi Pico. Generally, Thonny is used to build the codes that run on the same device on which Thonny is installed. Thonny also allows you to save and run MicroPython codes on your Raspberry Pi Pico. For this, you will need to change the python interpreter in the Thonny software to build codes that work on your Raspberry Pi Pico. 

You will see the ‘Python’ along with the version number which will be your current interpreter. If you click on Python, you can see the list of all available interpreters of Thonny from which you need to select ‘MicroPython (Raspberry Pi Pico)’. Once this interpreter is selected, you are good to go. 

If your interpreter doesn’t show ‘MicroPython (Raspberry Pi Pico)’ in the list, this would mean that you are running an old version of Thonny. For that, you will need to install the latest version of it.

Type the next “>>>>” symbol in the shell and click enter.

print(“Hello, World!”)

This is an instruction for the MicroPython Interpreter running on Raspberry Pi Pico. After receiving this command, the MicroPython will reply with the message “Hello, World!” and print that on the shell itself.

If you know the architecture of the Raspberry Pi Pico, the GPIO 25 is attached to the LED. We will attempt to switch the LED on and off from the shell.

To do this, we first need to import a special library named ‘machine.’ The machine library in MicroPython is used to manage the hardware of the board, in this case Raspberry Pi Pico. You can reboot the microcontroller, put it to sleep, enable or disable interrupts, and wake it from sleep using the machine module.

Some of the machine module classes are as follows:

  • Pin
  • Signal
  • ADC
  • UART
  • SPI
  • I2C
  • RTC
  • Timer
  • WDT
  • SD
  • SD Card

We’re going to learn about all the modules and their classes and when we use them. MicroPython documentation is a decent place to start if you want to explore more about MicroPython Libraries. If we want to use the GPIO block, we can import the ‘pin’ class from the ‘machine’ used to operate the IO pins of the Raspberry Pico.

from machine import Pin

Next, we create an object in the Pin class and set the GPIO number and its path, i.e. Input or Output.

led_gpio25 = Pin(25, Pin.OUT)

To turn ON the LED, you have to set its value at 1.

led_gpio25.value(1)

Type the lines in the shell one after the other. You will see that the LED switched on. To switch the LED OFF, set the pin value to 0.

led_gpio25.value(0)

How to Code Hello World in MicroPython

MicroPython

Program for Raspberry Pi Pico can be written in the same manner as those for other Raspberry Pi boards. The advantage of programming in Thonny is that it allows live software debugging. If you want to run the program whenever you require, you can program it in the main part of the window. But if you want immediate execution, commands can be written in the shell area present at the bottom of the window.

To code ‘Hello World’ in MicroPython, follow these steps:

In the Shell area, click next to the >>>> symbols, and type:

print(“Hello, World!”)

After writing the code, press ENTER at the end of the line. Your MicroPython will respond as “Hello, World!” soon as you press enter. This output can be seen in the shell area only.

Now, write the same code on the main section above the shell area part of the Thonny window. Click on the ‘Run’ icon. An option will appear to save this code to ‘This Computer’ or your ‘Raspberry Pi Pico’. Choose  ‘Raspberry Pi Pico’ and then save the program as output.py and click OK. By this, you will save and run your code in MicroPython. The reason for naming the file as .py is that this helps Pico to identify the file received as a Python file.

Create a program that blinks Raspberry Pi Pico’s LED

With the help of MicroPython, codes can be built for your Raspberry Pi Pico that interacts with onboard and external hardware such as LEDs and buttons. like You need to first have write requests to any of Pico’s General Purpose Input/Output pins work with it. Pins in Pico allow you to connect external devices whereas some are internally connected.  

Raspberry Pi Pico comes with an onboard LED which is labelled as GP25. Follow the steps to build a code that will blink the onboard led at GP25:

Click on the ‘New’ icon and write the name of the program as lightblink.py. The code given below is used to turn the led on and off:

import machine
import utime
led_onboard = machine.Pin(25, machine.Pin.OUT)
while True:
    led_onboard.toggle()
    utime.sleep(1)

The above code is to be written in the main space(script area) above the shell area. Once you are done writing this code, click on ‘Run’ and save the code to your Raspberry Pi Pico. As soon as you save the program, you could see the LED on your Pico board turning on and turning off for one second and keep repeating itself. And thus, you will be able to operate the onboard hardware of your Raspberry Pi Pico.

Conclusion

In this article, we explored the new Raspberry Pi Pico board through Thonny IDE and coding in MicroPython. This was just a beginner guide on how to get started with this microcontroller, there is an extensive scope of its uses and applications which you will learn later. 

I hope now you have a clear idea of how to get started with programming in MicroPython on the Raspberry Pi Pico. You’ve discovered how to program Raspberry Pi Pico with MicroPython in this elaborate tutorial. You’ve learned a little bit about MicroPython, how to mount MicroPython in Raspberry Pi Pico, Raspberry Pi Pi Pico Programming with MicroPython and Thonny IDE, and Blink and LED.

0 0 votes
Article Rating
Previous articleRaspberry Pi Pico vs Arduino – Which is Better
Next articleRaspberry Pi Pico vs ESP-32 – Which One To Choose?
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Steven
2 months ago

I’m getting crazy ….. All I wanted was reading the state of a switch connected to a GPIO-pin. I needed RPi.GPIO as library. Well it was unpossible to install it on my Windows-application: Thonny. I spent a whole day in searching on the internet. I found no solution. How is it possible that such a simple function can’t be done!

abpreen
Admin
Reply to  Steven
2 months ago

Hi Steve,
I am assuming you are using Thonny IDE, on Raspberry Pi and can’t get it working …
Check if you have MicroPython interpreter installed… and selected. Attached the image.. if MicroPython interpreter is not installed it will not work…
Step1 :
sudo apt-get update
sudo apt-get install python3-thonny
Try again if micro-python option is visible..
otherwise do

Do a sudo apt update && sudo apt full-upgrade -y
steve-comments.JPG
abpreen
Admin
Reply to  Steven
2 months ago

Adding the better image here…
You need to click at Right bottom of Thonny-IDE.

steve-comments.JPG