Raspberry Pi Pico( Introduction)

This is an introductory session on Raspbeery Pi Pico.

What is Raspberry Pi Pico( RPi Pico)

RPi Pico is a microcontroller.

What is a microcontroller?

Microcontroller is a small device which is used to control other devices it is built into and execute specific defined task. For example, it can control LED lights, buzzers, sensors, displays, etc. when connected to it. Microcontrollers usually use less power.

Difference between microcontroller and microprocessor

Visually both microprocessor and microcontroller almost look identical. They are different in terms of the application in which they are used, processing power, memory, cost and power consumption. Microcontroller tend to use less power, where the task is pre-defined. While, your microprocessors are used in devices which requires intensive processing and where the task can be not pre-defined. For example, in a microwave, the task is pre-defined and you only need to input the time and temperature. Therefore, a microwave uses a microcontroller to do its job.
Another important difference is their structure. A microprocessor consists only of a CPU( Central processing unit), and all the other processing elements like RAM( Random access memory), ROM( Read-Only memory), I/O ports, etc. are connected externally to it( You can see this if you open your pc). Now, whereas, all the above-mentioned component are present internally in-case of microcontrollers( this is the big chip you see in your RPi Pico).

Difference between RPi Pico and other RPi models

Other RPi contain microprocessor and can be considered as a mini-computer than a microcontroller.

Which is better?( Arduino vs RPi Pico)

Arduino contains a wide range of microcontrollers( no microprocessors though). And for a small scale project a RPi Pico is way better than an Arduino nano( the smallest and cheapest model Arduino offers) in terms of memory, processing power, cost and other factors.

Design of your RPi Pico

Your RPi Pico contains RP2040 chip. Which contains 264KB worth of memory, which is really large when compared to other microcontrollers. And 2 Flash memories. The Microcontroller chip( RP2040) is connected to the pins on the either sides of the board. These pins are called GPIO( General purpose input output) pins, and can be used to connect other devices like sensors, LED display, etc. The bumps at the edge of the board are called cancellations and are used to solder other components( mentioned above). You also have a BOOTSEL button and a LED. And finally, you can write programs by connecting your RPi Pico to a computer using a micro USB cable.
Download the labelled pinout diagram of the board.

Doing things with your RPi Pico

You can add executable files on your RPi Pico using a micro USB cable, computer and a software called Thonny.

Accessing the RPi Pico as a memory device

Using micropython to write code

from machine import Pin
import utime
# Define the onboard LED pin
led = Pin(25, Pin.OUT)
# Loop to blink the LED
while True:
    led.toggle() #Turns the LED on 
    utime.sleep(0.2) #Keeps the LED on for 0.2s
    led.toggle() #Turn the LED off
    utime.sleep(1) #Waits for 1s before starting again

Why micropython?

MicroPython is an implementation of python3 using C for low-level Chip-specific hardware like microcontrollers. Since microcontrollers are small and have less computing power, they can only run C/CPP/bash kind of files. So directly running python3 on RPi Pico is extremely inefficient. Therefore, micropython helps us in tackling this problem, as python3 is a very easy language to learn with simple syntax.
Check out the official micropython documentation.

What exactly does the BOOTSEL button do?

<Credits:- Claude 1.2 Instant AI by Anthropic>
The bootsel button on the Raspberry Pi Pico has a simple but important function - it allows you to choose which firmware/program will run when the board powers on or resets.
Here’s a breakdown in simple terms:

What is a UF2 file?

UF2 stands for “MicroPython Firmware Update Format”. It was created by MicroPython for easy firmware updates for boards without a native bootloader. UF2 files contain both the firmware image and a small bootloader program. Always, the bootloader runs first then the firmware. Each memory bank of RPi Pico can hold seperate UF2 files. It’s small, effective and open source.

While adding your first micropython file and running it, we did not explicitly download and used any UF2 file because Thonny did it for us. But there is another way of doing it, which is to download the official UF2 file and pasting it in the RPi Pico while accessing it as a memory device.
Note:- The following steps are applicable for all operating systems. For macos users, make sure you have macOS Ventura version 13.1 or above.
To do that follow the follow the following steps:

Project Morse

We will be making a project to convert the input alphabetical string to morse code and use the LED to convey the message to us( in morse code).
You can write the code for that your self or copy the following code( credits:- ChatGPT)

import machine
import utime

# Define Morse code dictionary
morse_code = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
    'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
    'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
    'Y': '-.--', 'Z': '--..',
    '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
    '8': '---..', '9': '----.', '0': '-----'
}

# LED pin
led_pin = machine.Pin(25, machine.Pin.OUT)

def morse_code_translator(text):
    morse = ""
    for char in text.upper():
        if char == " ":
            morse += " "
        elif char in morse_code:
            morse += morse_code[char] + " "
    return morse

def blink_morse_code(morse_code):
    for symbol in morse_code:
        if symbol == '.':
            led_pin.on()
            utime.sleep(0.2)
            led_pin.off()
            utime.sleep(0.2)
        elif symbol == '-':
            led_pin.on()
            utime.sleep(0.6)
            led_pin.off()
            utime.sleep(0.2)
        elif symbol == ' ':
            utime.sleep(0.4)

def main():
    while True:
        user_input = input("Enter text to convert to Morse code (press Enter to exit): ")
        if user_input == "":
            print("Exiting...")
            break
        morse = morse_code_translator(user_input)
        print("Morse Code:", morse)
        blink_morse_code(morse)

main()

Use the same steps as before to add this main.py file to your RPi Pico.
This code will ask you for a input and converts the string to morse code and displays it by switching the LED on/off.
For any doubts, bugs or errors, contact Mukti Core from our telegram group or drop a text on the Tech-help section on our telegram group.

Credits: @san, MUKTI Core

Edit this page