Raspberry Pi Pico Projects

Getting Started with Raspberry Pi Pico using MicroPython

Getting Started with Raspberry Pi Pico: Programming with MicroPython

Today you will learn how to get started with the powerful and versatile Raspberry Pi Pico using MicroPython. From setting up your board to writing your first program. we’ll guide you through a step-by-step process to help you get the most out of your new device.

The Raspberry Pi Pico is a microcontroller board that is based on the RP2040 microcontroller chip. It is a low-cost and powerful tool that can be used for a variety of projects, including Internet of Things (IoT) devices, robotics, and home automation. The Pico can be programmed using C/C++ and MicroPython.

Overview

The Pico was released by the Raspberry Pi Foundation as a lower-cost alternative to other microcontrollers, such as the Arduino, ESP32, and STM32. These microcontrollers can cost anywhere from $2 to $5, while the Raspberry Pi computer costs around $35 to $40. The Pico aims to be a competitive option with its RP2040 microcontroller, which is a dual-core ARM Cortex-M0 + chip.

In this tutorial, we will learn about the RP2040 microcontroller and the Raspberry Pi Pico board. We will also go over the different pin types on the Pico, such as ADC pins, I2C pins, SPI pins, and UART, and how they can be used to interface with external sensors or modules.

For this Raspberry Pi Pico tutorial, we will only be using Micropython for programming the device. There are two options for doing this: Thonny IDE or uPyCraft IDE. In future tutorials, we will explore how to program the Raspberry Pi Pico with C/C++ and the Arduino IDE. For now, we will start with a basic Raspberry Pi Pico LED blink code to test the board’s functionality.

Raspberry Pi Pico Starter kit

If you are a student, beginner, or maker who wants to learn Raspberry Pi Pico & MicroPython, you can buy the Raspberry Pi Pico Starter Kit and do almost 40 projects. The purchase link is provided below.

S.NCOMPONENTS NAMEQUANTITYPURCHASE LINKS
1Raspberry Pi Pico Basic Starter Kit1 Amazon | AliExpress

What is RP2040 Microcontroller?

The RP2040 is a microcontroller that was introduced by the Raspberry Pi Foundation on January 21, 2021. It is the first processor designed in-house by the foundation and is featured on the Raspberry Pi Pico board. The RP2040 is a 32-bit dual ARM Cortex-M0 + microcontroller and is made using 40nm silicon technology. It is packaged in a small 7x7mm QFN-56 package and is available for around $4.

RP2040 Microcontroller

The RP2040 has two ARM Cortex-M0+ cores that run at a clock speed of 133 MHz, as well as 264 KB of RAM. Its program memory is external and can support up to 16 MB of data. In terms of connectivity, the RP2040 has UART, SPI, and I2C ports, as well as timers, PWM, DMA, and a 12-bit analog-to-digital converter (ADC). These features make it a versatile and capable microcontroller for a variety of applications.

Meaning of RP2040

The name RP2040 has a specific meaning:

  • RP stands for Raspberry Pi
  • The number 2 indicates that it is a dual-core microcontroller
  • The number 0 indicates that it has ARM Cortex-M0+ cores
  • The number 4 represents the on-chip RAM, which is 264 KB. This value is calculated using the formula: floor (log2 (ram / 16k))
  • The number 0 represents the on-chip flash, which is not present on the RP2040.

RP2040 Key features:

The RP2040 has several key features:

  • It has 133 MHz dual ARM Cortex-M0+ cores
  • It has 264 KB SRAM in six independent banks
  • It can support up to 16 MB of off-chip flash memory through a dedicated QSPI bus
  • It has a DMA controller and a fully-connected AHB crossbar
  • It has interpolator and integer divider peripherals
  • It has an on-chip programmable LDO to generate the core voltage
  • It has two on-chip PLLs to generate USB and core clocks
  • It has 30 GPIO pins, 4 of which can be used as analog inputs.

Introduction to Raspberry Pi Pico

The Raspberry Pi Pico is a small, breadboard-friendly microcontroller board based on the RP2040 microcontroller. It features a micro-USB connector, a 3-pin debug connector, and a row of contacts along each side. It is similar in size to an ESP32 Pico Kit and slightly larger than an Arduino Nano or Micro. The Pico has 2 MB of QSPI Flash memory and 25 of the 30 GPIO pins of the RP2040 are accessible through the extension connectors. It is easy to use with breadboards and can be easily integrated into a variety of projects.

Raspberry Pi Pico

Features of Raspberry Pi Pico

The Raspberry Pi Pico board has several notable features, including:

  • The RP2040 microcontroller, which powers the board.
  • 2 MB of SPI Flash memory for storing data and programs.
  • A Type B micro-USB port for both power and programming.
  • 40 DIP style IO pins for connecting to external devices.
  • A 3-pin ARM Serial Wire Debug (SWD) interface for debugging and programming.
  • A 12 MHz crystal oscillator for generating accurate timing signals.
  • A boot selection button for choosing between different boot options.
  • A programmable LED connected to GPIO 25.
  • A 3.3V fixed output buck-boost SMPS converter for regulating the power supply.
Raspberry Pi Pico Board Overview

Raspberry Pi Pico Pinout

The Raspberry Pi Pico has 40 pins in total, with 26 of them being Input-Output (IO) pins that can be used for a variety of purposes such as analog and digital communication. These IO pins are located along the sides of the board, and they are what allow you to connect the Pico to other devices and peripherals. In addition to the IO pins, there are also 14 power and system-related pins on the Pico. These pins are responsible for providing power to the board, as well as for interacting with the system-level functionality of the Pico. Finally, there are 3 additional pins on the Pico that are used for debugging purposes. These pins, known as the SWD (Serial Wire Debug) pins, allow you to connect the Pico to a debugger and program it using specialized software.

Raspberry Pi Pico Pinout

In addition to the various pin types mentioned above, the Raspberry Pi Pico also has several peripherals built-in. These peripherals include two I2C interfaces (I2C0 and I2C1), two SPI interfaces (SPI0 and SPI1), and two UART interfaces (UART0 and UART1). These peripherals allow the Pico to communicate with other devices and peripherals using these specialized communication protocols.

Before you can start using the Raspberry Pi Pico, you will need to solder 40-pin male headers onto the board. These headers should be placed along the sides of the board, with 20 pins on each side. This will allow you to easily connect the Pico to a breadboard or other prototyping platform. Once the headers are in place, you can start using the Pico to build your own electronic projects.


Programming Raspberry Pi Pico

The Raspberry Pi Pico can be programmed using a variety of programming languages, including C/C++ and Python. One of the easiest ways to get started with the Pico is to use MicroPython, a Python language interpreter designed specifically for microcontrollers and embedded systems. If you are familiar with Python, you will find that working with MicroPython is easy, as the syntax is very similar.

There are two main ways to program the Raspberry Pi Pico using MicroPython:

  1. Thonny IDE: This is a simple, easy-to-use Integrated Development Environment (IDE) that is well-suited for beginners.
  2. uPyCraft IDE: This is a more advanced IDE that provides a range of features and tools for developing and debugging MicroPython code.

It is generally recommended to use a Linux-based system, such as a Raspberry Pi computer, for programming the Pico in C or C++. This is because it is easier to download the SDK and write C programs in Linux. However, you can also use other operating systems if you prefer.


Installing MicroPython on Raspberry Pi Pico

  • Push and hold the BOOTSEL button on the Pico, and then immediately connect the Pico to your computer using a micro USB cable.
  • Release the BOOTSEL button once the RPI-RP2 drive appears on your computer.
  • Open the RPI-RP2 drive that appears in the drives list.
Installing MicroPython on Raspberry Pi Pico
  • Visit the Raspberry Pi Pico official documentation page and download the MicroPython UF2 file from the MicroPython tab.
Installing MicroPython UF2 on Raspberry Pi Pico
  • Drag and drop the UF2 file onto the RPI-RP2 drive. The Raspberry Pi Pico will reboot and will now run MicroPython.
Installing MicroPython on Raspberry Pi Pico MicroPython UF2
  • After following these steps, you will be ready to start programming the Pico using MicroPython. You can use an IDE such as Thonny or uPyCraft to write and debug your code.

Getting Started with Raspberry Pi Pico using MicroPython on Thonny IDE

To get started with the Raspberry Pi Pico using MicroPython on the Thonny IDE, follow these steps:

Download and Install Thonny
  • Download and install Thonny from https://thonny.org/
  • Connect the Raspberry Pi Pico to your computer and open Thonny.
  • Go to Tools > Options and click on the Interpreter tab. From the interpreter dropdown list, select MicroPython (Raspberry Pi Pico). Leave the port dropdown menu set to “Auto-detect”. Click Ok to close.
Install Mycropython on RPI Pico
  • When you plug in the Pico, a firmware installation tab will appear. Click on “Install” to download the necessary files.

After the installation is complete, the MicroPython version and Raspberry board will be displayed in the Python Shell. To test that everything is working properly, try running a simple “Hello World” program by typing “print(‘Hello World’)” and pressing Enter. You should see “Hello World” printed as a response.

Hello world on Micropython

To write a “blink program for the onboard LED of the Raspberry Pi Pico“, you can use the following code:

from machine import Pin
import utime

led = Pin(25, Pin.OUT)  # Set up the onboard LED on GPIO25
led.low()  # Turn the LED off initially
delay=1
while True:
    led.toggle()  # Toggle the LED on and off
    print("Toggle")  # Print a message for debugging
    utime.sleep(delay)  # Wait for 1 second before toggling again
Save in computer

To run the program, save it to your computer with a unique name like “testblink.py” and then run it using your preferred method. The program will run in a loop, toggling the onboard LED on and off every second. You should be able to see the LED flashing on the Pico board.

Raspberry Pi Pico built in LED blink using MicroPython
Getting Started with Raspberry Pi Pico using MicroPython

If you unplug the USB cable from the Raspberry Pi Pico and plug it back in, you might notice that the LED is no longer blinking. This is because the program is only saved on your computer, and is not stored on the Pico itself.

To make the program run continuously, even after unplugging and re-plugging the USB cable, you need to save the program to the Pico board. To do this, open a new tab in your IDE and paste the program code into it. Then, click “Save” and choose the Raspberry Pi Pico as the destination. When prompted, give the program a name such as “main.py“.

save file to raspberry pi pico storage

After saving the program to the Pico, unplug and re-plug the USB cable. The LED should continue to blink, even without a connection to your computer. This is because the program is now stored on the Pico board itself, and is able to run independently.

To write a “blink” program for an external LED connected to the Raspberry Pi Pico, you can use the following code:

from machine import Pin
import utime

led = Pin(28, Pin.OUT)  # Set up the external LED on GPIO28
led.low()  # Turn the LED off initially
delay=1
while True:
    led.toggle()  # Toggle the LED on and off
    print("Toggle")  # Print a message for debugging
    utime.sleep(delay)  # Wait for 1 second before toggling again

To run the program, make sure the external LED is connected to GPIO28 on the Pico board, with a 330ohm resistor in place. Then, save the program to the Pico board using the steps described in the previous code.

Getting Started with Raspberry Pi Pico using MicroPython on Thonny IDE

As soon as the program runs, the external LED should start blinking on and off at an interval of 1 second.

Raspberry Pi Pico external LED blink using MicroPython
Getting Started with Raspberry Pi Pico using MicroPython

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

To write a program that will blink both the onboard LED and an external LED connected to the Raspberry Pi Pico, you can use the following code:

from machine import Pin
import utime

led1 = Pin(25, Pin.OUT)  # Set up the onboard LED on GPIO25
led2 = Pin(28, Pin.OUT)  # Set up the external LED on GPIO28
led1.low()  # Turn both LEDs off initially
led2.low()
delay=1
while True:
    led1.toggle()  # Toggle both LEDs on and off
    led2.toggle()
    print("Toggle")  # Print a message for debugging
    utime.sleep(delay)  # Wait for 1 second before toggling again

To run the program, make sure both the onboard LED and the external LED are connected to their respective GPIO pins on the Pico board. Then, save the program to the Pico board and run it. Both LEDs should start blinking at the same time, with an interval of 1 second between each toggle.

Raspberry Pi Pico LED blink using MicroPython
Getting Started with Raspberry Pi Pico using MicroPython

Getting Started with Raspberry Pi Pico using MicroPython on uPyCraft IDE

The Raspberry Pi Pico can also be programmed using the uPyCraft IDE, which is similar to the Thonny IDE. To get started with the Pico and uPyCraft, follow these steps:

Select Raspberry Pi COM Port
  • From the Tools menu, select Board and then select “others”.
Select board on uPyCraft IDE
  • Copy a program, such as the LED blink code from the above code snip, and paste it into a new tab in the uPyCraft IDE.
  • Save the file with a name such as “main.py”.
save MicroPython script on uPyCraft IDE
  • Click on the “Download & Run” button on the right side of the window.
Getting Started with Raspberry Pi Pico using MicroPython on uPyCraft IDE

Once the program is downloaded, it will start running and the LED should start blinking. You should see “Toggle” messages appearing in the Shell window.

Note that you can use any of the programs provided in the previous code snip with the uPyCraft IDE. But make sure to save the file with a name such as “main.py” before downloading and running it.


Conclusion

In conclusion, Getting Started with Raspberry Pi Pico using MicroPython in Thonny IDE and uPyCraft IDE is a great way to begin experimenting with the capabilities of this powerful microcontroller. By using the built-in LED and an external LED, we were able to blink the LEDs, which is a simple yet powerful way to understand how the board works and how to control it using MicroPython. The Raspberry Pi Pico is a versatile device that can be used for a wide range of projects, from simple blinking LEDs to more complex projects.

With the help of Thonny IDE and uPyCraft IDE, the process of writing and uploading code to the board is made easy, making it accessible to both beginners and experienced makers. With this knowledge, you will be able to unlock the full potential of your Raspberry Pi Pico and create amazing projects.


Related Articles

3 Comments

  1. Pingback: Interfacing SSD1306 OLED Display with Raspberry Pi Pico
  2. Pingback: Interface BME280 with Raspberry Pi Pico using MicroPython
  3. Pingback: Raspberry Pi Pico Realtime Clock with Temperature Monitoring

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button