Home Home Astronomy Chemistry Electronics Mathematics Physics Field Trips Home  

Raspberry Pi Pico on Linux Mint

 

Setup for Linux Mint

A Raspberry Pi running Raspberry Pi OS (formerly called Raspbian) might come fully set up to work with a Pico, but other versions of Linux do not. Setting up Linux Mint was quite tricky.

  • Update and upgrade software.

    • sudo apt update
    • sudo apt upgrade
  • Install Thonny.

    • The version of Thonny available in apt is old, so don't bother with it. Instead, download and compile the installer directly from the Thonny website.
    • bash <(wget -O - https://thonny.org/installer-for-linux)
    • I think this installs any required Python dependencies.
  • We then needed to install snapd.

    • First have to remove a preference which prevents the snapd package being found. (This is Linux Mint; there are a lot of Ubuntu users angry with Canonical for moving the Ubuntu release of Firefox to snap.)
    • sudo rm /etc/apt/preferences.d/nosnap.pref
    • sudo apt update
    • sudo apt install snapd
    • sudo reboot
  • Install MicroPython, a version of Python programming language designed for use with microcontrollers.

    • sudo snap install micropython
  • To be able to program a microcontroller with Thonny it required we approve adding the present user to the dial-out group, followed by another restart.

 

Using Thonny

  • Plug in the Raspberry Pi Pico using a micro-USB cable.

    • If the Pico is new it should appear on the desktop.
    • If it doesn't appear and you haven't installed MicroPython on it already, press and hold the BOOTSEL (boot selection) button as you plug it in.
    • The button can be released when the Pico appears on the desktop.
  • Open Thonny. At the bottom right corner there is a Python version number. Click on that and select "MicroPython (Raspberry Pi Pico)". If the Pico has been mounted on the desktop, a dialog box should appear asking if you want to install MicroPython on the Pico. Select yes.

    • Thonny will go ahead and do the installation, then the Pico will disappear from the desktop.
    • If the Raspberry Pi Pico has MicroPython installed on it, it will not appear on the desktop when plugged in unless the BOOTSEL button is pressed while plugging it in.
    • The Pico not being visible on the desktop is fine. You don't want the Pico to appear on the desktop if you are saving a program to it.
  • The first time a program is saved, Thonny will ask where you want it to be saved. Select "MicroPython device" to save to the Pico.

  • Multiple programs can be saved to the Pico at once using different names, but only the program named main.py will run automatically when the Pico is supplied with power.

    • Programs saved to the Pico do not appear in the 2 MB flash drive portion of the Pico (accessible when the Pico is mounted on the desktop).

This was disappointingly complicated. Getting up and running with the Arduino IDE is much simpler.

 

Blink

I have to say, the official Raspberry Pi Pico program to blink the on-board LED is very obscure.

Raspberry Pi Pico:

from machine import Pin, Timer
led = Pin(25, Pin.OUT)
timer = Timer()
def blink(timer):
    led.toggle()
timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)

Arduino:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

What was the Raspberry Pi Foundation thinking? Arduino easily wins here for code clarity. (Also, the advice was to save it as blink.py, meaning it would not run on its own.)

There are simpler Python versions of the program, like this one, Pico Blink (save as main.py so it can run on its own when the Pico has powered connected):

from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
    led.value(1)
    time.sleep(1)
    led.value(0)
    time.sleep(1)

For beginners, that's much better than the official version of the program. Come on, RPi Foundation, we're talking about the first ever RPi Pico program most new Pico programmers will use. Start with something simple that works and is as clear as possible how it works. Build up* to more complicated and capable versions later.

* No pun intended on building/compiling. (Python programs are not compiled, but it could be argued that compiled programs are more capable simply because they'll run faster.)