IOT

Programming Raspberry Pi Pico using Micro-Python

In this tutorial, we will learn how to use MicroPython to program the Raspberry Pi Pico. We will learn what MicroPython is, how to install it on a Raspberry Pi Pico, and how to program it using MicroPython.

Raspberry Pi Pico is a brand new interesting Microcontroller board based on the Raspberry Pi Foundation’s RP2040 Microcontroller. The Raspberry Pi Pico is an inexpensive Arm-based microcontroller that can be programmed in C/C++ and MicroPython.

Since its original release in 1991, the Python Programming Language has been a popular option among software developers. Python was designed to be used in the development of programs for desktops, laptops, and servers that have a powerful CPU and run an operating system. But what if you want to create Python apps for microcontroller-based embedded systems?

What is MicroPython?

MicroPython is a Python interpreter designed for microcontrollers and embedded devices. Damien P. George created it, and it is written in C. MicroPython is a Python 3 compatible compiler and runtime that may be run on microcontrollers.

Although it is a subset of Python 3, it does not include all of Python’s standard libraries because it was designed for limited systems. Only a few carefully chosen Python libraries are supplied, while the rest are created specifically for MicroPython (mostly for low-level hardware access).

If you’re a software developer who’s never dealt with hardware but wants to build programs for microcontroller-based embedded systems (for personal or professional projects), MicroPython is the way to go.

Introduction to Raspberry Pi Pico

Raspberry Pi PICO is a new device produced by the Raspberry Pi Foundation that is significantly less expensive than existing Raspberry Pi products. Raspberry Pi PICO is a Single Board Computer (SBC) with the smallest size development board, and it includes an RP2040 Microcontroller chip made by the Raspberry Pi Foundation. The Raspberry Pi Pico breakout board is specifically built for the RP2040. It resembles other microcontroller boards in appearance, with the MCU in the center, a micro-USB connector on one end, and a row of contacts on each side. At the opposite end of the board, there is a 3-pin debug connection.

 It combines the RP2040 with 2MB of Flash memory and a power supply chip that accepts input voltages ranging from 1.8 to 5.5V. This enables Pico to be powered from a wide range of sources. This comprises two or three AA cells connected in series, as well as a single lithium-ion cell.

Raspberry Pi Pico

The Raspberry Pi Pico is 51 by 21 mm in size, which is the same as an ESP32 Pico Kit and somewhat larger than an Arduino Nano or Micro. The Pico has 2 MB of QSPI Flash memory and 25 of the RP2040’s 30 GPIO pins have been extended out on extension connections. The board is breadboard compatible and will fit flawlessly on a breadboard.

Features of Raspberry Pi Pico:

  • Operating Voltage: 1.8-5.5 VDC
  • Processor: RP2040 (Dual-core Arm Cortex M0+) By Raspberry Pi
  • RAM: 264 KB
  • GPIO: 26 Pins
  • Clock Speed: 133 MHz
  • On-Board Port: Micro-USB 5V/2.5A DC Power Input Port
  • 2MB of onboard Flash memory
  • The castellated module allows soldering directly to carrier boards
  • USB 1.1 with device and host support
  • Low-power sleep and dormant modes
  • Drag-and-drop programming using mass storage over USB
  • 26 multi-function GPIO pins
  • 2 SPI, 2 I2C, 2 UART, 3 12-bit ADC, 16 controllable PWM channels
  • Accurate clock and timer on-chip
  • Operating Temperature Range: -20 to 85 degrees C
  • Temperature sensor
  • Accelerated floating-point libraries on-chip
  • 8 Programmable I/O (PIO) state machines for custom peripheral support

Raspberry Pi Pico Pinout

This is a top view of the pinouts on the Raspberry Pi Pico. The pin labels are on the bottom of the board.

Raspberry Pi Pico Pinout

The Raspberry Pi Pico has 40 pins. 26 of the 40 pins are Input-Output (I/O) (IO Pins). All 14 of the pins are analog, digital, and serial pins. There are 14 power and system-related pins on the board. The final three pins are utilized for SWD debugging. I2C0 and I2C1 are the two available I2C peripherals. Similarly, two SPI peripherals, SPI0 and SPI1, are available. There are also two UART Pins, UART0, and UART1. You can assign any of these to any of the available pins. You must solder 40-pin male headers, 20 on each side of the board before you can use the Raspberry Pi Pico.

 

Programming Raspberry Pi Pico:

The Pi Pico may be programmed in a variety of languages, including C/C++ and Python. Pico may be used for a wide variety of applications and skill levels, and getting started is as simple as dragging and dropping a file. If you are working with C, it is best to utilize a Linux-based system, such as a Raspberry Pi Computer, since it is simple to get the SDK and build C programs in Linux.

However, I recommend that you program the Raspberry Pi Pico Board with MicroPython. To program the Raspberry Pi Pico using Micropython, you can either use:

1. Thonny IDE

2. uPyCraft IDE

However, before you can use the Raspberry Pi Pico, you must first install MicroPython on the Raspberry Pi Pico Board.

Installing MicroPython on Raspberry Pi Pico

  • Press and hold the BOOTSEL button on the Pico, then connect the Pico Board to your computer with a micro USB connection right away. Once the drive RPI-RP2 appears on your PC, release BOOTSEL.

2. Select the RPI-RP2 drive from the Drives menu.

4. From the MicroPython tab, download the MicroPython UF2 file.

5. Move the UF2 file to the RPI-RP2 disc by dragging and dropping it. The Raspberry Pi Pico with RP will restart and begin running MicroPython.

Getting Started with Raspberry Pi Pico using MicroPython on Thonny IDE

1. Let’s get started using the Raspberry Pi Pico and MicroPython on the Thonny IDE. To begin, go to https://thonny.org/ and download Thonny.

2. Plug the Raspberry Pi Pico into your computer. Then, from Thonny, navigate to Tools > Options and select the Interpreter option. Select MicroPython from the interpreter dropdown menu (Raspberry Pi Pico). The port dropdown selection can be left empty to let the Pico to detect itself. To close, click OK.

3. When you plug in the pico Board, a firmware installation page for the raspberry pi pico will display. When you click Install, some files will be downloaded.

4. The MicroPython version and Raspberry board will display in the Python Shell after successful installation. To put this to the test, we can construct a short print function that says “Hello World.” To run the code, press Enter. As an answer, you will receive Hello World.

Writing a blink program for the onboard LED of pico

  • The Raspberry Pi Pico’s onboard LED is attached to GPIO25. Copy and paste the following code into the editor window.
from machine import Pinimport utimeled = Pin(25, Pin.OUT)led.low()while True:    led.toggle()    print(“Toggle”)    utime.sleep(1)
  • Save the program to your computer and give it a unique name, such as blink.py.
  • The program will execute, and you will notice the LED on the board blinking.
Getting Started Raspberry Pi Pico
  • Now, disconnect the Raspberry Pi Pico’s USB and reconnect it. You’ll see that the LED isn’t flickering. This is due to the fact that the application is saved on the computer rather than the Raspberry Pi Pico Board.

You must save the software to the Pico Board in order for the system to function even after plugging and removing the USB wire. To do so, open a new tab, copy the same application, and then save it. Save it on the Pico Board this time.

  • Give it the name main.py while saving, as illustrated in the image below.

This time, the LED will continue to flicker even after plugging and unplugging the power cord. This is due to the fact that the software is saved on the Pi Board.

Writing a blink program for the external LED

  • Let us now build a Raspberry Pi Pico LED Blink code that would blink an external LED attached to Raspberry Pi Pico GPIO Pin28. The diagram is shown below. Using a 330ohm resistor, connect an LED to GPIO28.
Raspberry Pi Pico LED Blink
  • Copy and paste the following code into the Pico Board.
from machine import Pinimport utimeled = Pin(28, Pin.OUT)led.low()while True:    led.toggle()    print(“Toggle”)    utime.sleep(1)
  • As soon as the code is executed, the LED will begin flashing at 1-second intervals.

Writing a program to blink both on-board & external LED

  • Now let us write a Raspberry Pi Pico Code to blink both the on-board and external LED. The code is very simple. Copy the following code and run.
from machine import Pinimport utimeled1 = Pin(25, Pin.OUT)led2 = Pin(28, Pin.OUT)led1.low()led2.low()while True:    led1.toggle()    led2.toggle()    print(“Toggle”)    utime.sleep(1)
  • When the code is executed, both the onboard and external LEDs will flash.

Getting Started with Raspberry Pi Pico using MicroPython on uPyCraft IDE

You may also use the uPyCraft IDE to program the Raspberry Pi Pico RP2040 Microcontroller. Thonny IDE is comparable to the uPyCraft IDE. We recently discussed the uPyCraft IDE on ESP32 using Micropython in depth. You are welcome to examine it.

First download uPyCraft IDE from the following link: https://github.com/DFRobot/uPyCraft. Now open the uPyCraft IDE.

  • Click Tools and then choose the COM Port from the Tools Menu.
  • From the tools, select Board and then select others.
  • Now, copy the Raspberry Pi Pico LED Blink Code and paste it into the New Tab in the uPyCraft IDE.
Raspberry Pi Pico LED Blink
  • Save the file by the name main.py.
  • Then, on the right, click the Download & Run Button. When the LED is downloaded, it will begin to blink. On the Shell Window, you will see a toggle.

The remaining scripts may be tested using the uPyCraft IDE. The code is compatible with any IDE.

CONCLUSION:

Programming on Pico using Micro Python is a simple procedure. Anyone may begin programming in a few minutes. The board is inexpensive and has a plethora of functions. I’m hoping that this board will stand out among dev boards like Arduino in the long run. We’ll see you next time.

2 thoughts on “Programming Raspberry Pi Pico using Micro-Python

  1. Javed says:

    Great Blog. Thanks for a very clear and nicely sequenced descriptive help.
    Javed

    1. mrudhula says:

      Thankyou so much for your valuable feedback.

Leave a Reply

Your email address will not be published.