top of page
IMG_20170503_173438.jpg

Post

Introduction to Raspberry Pi Pico programming and MicroPython


Raspberry Pi Pico.
Raspberry Pi Pico.




Introduction


When discussing embedded system prototyping, two popular platforms come to mind: these are Arduino and Raspberry Pi platforms. Arduino is usually the recommended option for beginners because it's easy to set up and used for simple embedded prototyping projects, while the Raspberry Pi is for more seasoned users and for more advanced and high-processing projects.


Raspberry Pi is popular for making single-board computers (development boards with the capability of a full-sized computer in a tiny form factor): with each upgrade getting more powerful than the last. But, in January 2021, the Raspberry Pi in the UK released the Raspberry Pi Pico board. This time, instead of a single-board computer running Linux, the Pico is a microcontroller board like the Arduino boards.


Let's take a look at the breakdown of today's topic:


What we will learn

  1. Raspberry Pi Pico hardware

  2. Installation and configuration of Thonny IDE

  3. What is MicroPython?

  4. Adding MicroPython firmware to the Pico board

  5. Program Raspberry Pi Pico onboard LED to blink using MicroPython


Hardware components we will need

  • Laptop computer or PC (with an internet connection)

  • Raspberry Pi Pico board

  • Micro USB cable



1. Raspberry Pi Pico Hardware specs:


Raspberry Pi Pico board
Raspberry Pi Pico board

  • 21mm x 51mm form factor.

  • RP2040 microcontroller chip designed by Raspberry Pi in the UK.

  • Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133MHz.

  • 264KB on-chip SRAM

  • 2MB on-board QSPI Flash

  • 26 multifunction GPIO pins, including 3 analog pins.

  • 2 x UART, 2 x SPI controllers, 2x I2C controllers, 16 x PWM channels.

  • 1 x USB 1.1 controller and PHY, with host and device support.

  • 8 x Programmable I/O (PIO) state machines for custom peripheral support.

  • Supported input power 1.8-5.5V DC.

  • Operating temperature -20*C to +85*C.

  • The castellated module allows soldering directly to carrier boards.

  • Drag-and-drop programming using mass storage over USB.

  • Low-power sleep and dormant modes.

  • Accurate on-chip clock.

  • Temperature sensor.

  • Accelerated integer and floating-point libraries on-chip.


The one downside of the Raspberry Pi Pico is the absence of wireless connectivity

(no on-board Bluetooth and Wi-Fi modules), but this can always be handled by connecting to external wireless modules. (or you can use the Pico W)


Fortunately, on 30 June 2022 Raspberry Pi launched the Raspberry Pi Pico W (W for WiFi). The Pico W is exactly the same as the previous Pico board: but with the added feature of Bluetooth and WiFi.


We will discuss the Pi Pico W in subsequent posts (remember to tune in)



Raspberry Pi Pico Pinout diagram.
Raspberry Pi Pico Pinout diagram.




2. Install and configure Thonny IDE

  • Install the latest version of Thony on your OS (Windows, macOS, and Linux)

  • Head to the Thonny website and download the Thonny version for your OS. (We'll be using a Windows PC for this tutorial.)

Once you have successfully installed Thonny on your computer, open Thonny and you should see a similar screen to the one below:


Thonny IDE Home window.
Thonny IDE Home window.


3. What is MicroPython?


MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers and in constrained environments.


MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling, and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.


MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.



4. Adding MicroPython firmware to the Pico board



If you have never used MicroPython on your Raspberry Pi Pico, you will need to add the MicroPython firmware. You can do this by just dragging and dropping a file onto the board. A downloadable UF2 file has been made available to let you install MicroPython more easily, with the following steps below:


Adding MicroPython firmware to Raspberry Pi Pico board (raspberrypi.org)
Adding MicroPython firmware to Raspberry Pi Pico board (raspberrypi.org)



Steps (in the demo above)
  • Download the MicroPython UF2 file (provided above).

  • 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 computer. Release the BOOTSEL button after your Pico is connected.

  • It will mount as a Mass Storage Device called RPI-RP2.

  • Drag and drop the previously downloaded MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython on your Pico.

  • Follow the simple steps in the images below to select (MicroPython) in your Thonny IDE:


Click on "Tools" then "Options" for interpreter configuration.
Click on "Tools" then "Options" for interpreter configuration.


Set the connected USB port to be selected automatically.
Set the connected USB port to be selected automatically.


Select MicroPython then click OK.
Select MicroPython then click OK.



5. Blink Raspberry Pi Pico onboard LED with MicroPython


Now, we will program the LED on the Pi Pico board (connected to pin 25) to turn ON and OFF with MicroPython codes written with Thonny. MicroPython adds hardware-specific modules, such as machines, that you can use to program your Raspberry Pi Pico (for example - accessing the GPIO pins). Firstly, we need to import needed libraries: machine for accessing GPIO pins and sleep to add delay to the code.


Next, we will create a led object where we initialize the onboard LED pin (GP25).


For the blink to take effect continuously, we will introduce a loop:

  • If we set the value of the LED to 1, it turns on and if we set the value to 0, it will turn off.

  • Then we pause the operation for 1 second using the "sleep function".


Enter the code below in your Thonny IDE:


from machine import Pin   # for accessing GPIO pins
from time import sleep    # for adding delay

led = Pin(25, Pin.OUT)    # set pin 25 for output signal

while True:
    led.value(1)    # turn on LED
    sleep(1)        # delay for one second
    led.value(0)    # turn off LED
    sleep(1)        # delay for one second
    


Run code

Follow the simple steps below to save and run your code:

Click "File" then "Save" - a new window should pop up:
Click "File" then "Save" - a new window should pop up:


Click "Raspberry Pi Pico" to save your code inside the microcontroller.
Click "Raspberry Pi Pico" to save your code inside the microcontroller.


Save your code as "main.py". This will make your code run automatically once the Pico is connected to a power source.
Save your code as "main.py". This will make your code run automatically once the Pico is connected to a power source.


Finally, click on the "Play button" to run your code.
Finally, click on the "Play button" to run your code.

Demo video


LED beside the USB port

Raspberry Pi Pico onboard LED blinking.




That's it for today. I hope we learned something new as always.



Stay tuned for more Raspberry Pi Pico projects and MicroPython tutorials!





Online summer camp and summer camps in Takoma Park Maryland


You and your children are looking for nice activities to have fun and learn new things and skills. Come join us starting in May and this summer for more online coding courses for kids such as:

  • Python coding for kids and teens

  • Coding for elementary school students

  • Make video games

  • Electronics

  • Digital modeling (Create cars, rockets, rings, etc) for 3D printing and much more with the following link: Online summer camp

And the STEM summer camp in Maryland for your kids is organized in Takoma Park Maryland nearby Washington DC to have fun and learn more about robotics, coding, droning, and more. Check the link right here:



Other cute things to make and hand-crafts for kids


If you are looking for more electronics programming and STEM tutorials to do with your kids, take a look at these other activities: