I have used the software Thonny to send programs to my raspberry pi pico. I am trying to make a specific program auto run when my pico is plugged in. At the moment another program which is on the pico auto runs but I want another program to run instead.
Name the program you want to run main.py
Tested on Rpi Pico W, Ubuntu 22.04 host.
Command line approach
Install the MicroPython firmware.
Download the latest prebuilt firmware, it is a UF2 file:
Plug the Pi USB to computer while holding the BOOTSEL button.
A filesystem should appear on host.
Copy the file into the filesystem. On Ubuntu it automounts something like:
cp ~/Downloads/rp2-pico-w-20221014-unstable-v1.19.1-544-g89b320737.uf2 /media/$USER/RPI-RP2/
When flashing ends, the filesystem automatically unmounts, leaving you in non-boot mode. The firmware has been installed.
Install your program as
main.py
on the board.- Ensure the board is not in boot mode:
- after installing firmware, this happens automatically
- otherwise just unplug the USB and plug it back in without holding the BOOTSEL button
Install
rshell
on host:python3 -m pip install --user rshell
Copy your program to the board as
main.py
. Supposing you have a blinker program atblink.py
in the current working directory, run:rshell -p /dev/ttyACM0 --buffer-size 512 cp blink.py /pyboard/main.py
/pyboard
is a magic path to rshell, not actually present on the host. Terrible API!!!This is what I tested with:
import machine import time led = machine.Pin('LED', machine.Pin.OUT) # For Rpi Pico (non-W) it was like this instead apparently. # led = Pin(25, Pin.OUT) while (True): led.on() time.sleep(.5) led.off() time.sleep(.5)
The
main.py
thing is documented e.g. at: https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/9Unplug and replug the USB without holding BOOTSEL. Every time you do this, the
main.py
program starts running automatically.
After installing, you can also use rshell
to check the contents of main.py
with:
rshell -p /dev/ttyACM0 --buffer-size 512 cat /pyboard/main.py
Bibliography:
- https://forum.micropython.org/viewtopic.php?t=3610
- https://forum.micropython.org/viewtopic.php?t=6005
- https://forums.raspberrypi.com/viewtopic.php?t=301927
Thonny editor UI approach
This is the procedure described on the official docs at: https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/0
It does the same steps as the CLI approach, but as usual with UIs making it more obscure what is actually happening behind the scenes :-)
Install Thonny:
python3 -m pip install --user thonny
Install the MicroPython firmware
Plug the Pico while holding BOOTSEL
Open Thonny from the command line:
thonny
I tested with version 4.0.1.
Click Python version on bottom right of the screen (bad UI)
Install MicroPython
MicroPython variant: Raspberry Pi Pico (choose W vs non W)
Install
Unplug the Pico, close Thonny, replug the Pico without BOOTSEL, reopen Thonny
Install your program as
main.py
on the boardPaste our
blink.py
on the main Thonny editor windowFile > Save (or Ctrl + S)
A popup opens, choose: "Raspberry Pi Pico" (instead of "This computer")
Save the file as
main.py
on the PicoUnplug USB and replug.
main.py
starts running.
Even simpler. To make your micropython autostart, in a recent Thonny (I am using 3.3.14), go to the file menu and "save as" choose Pi Pico as the target and name the save file as main.py.
Boom! power cycle and your python app is stand alone and will start on power ups.
Note: Thonny will not access your pico anymore until you reload the micropython.uf2 file.
© 2022 - 2024 — McMap. All rights reserved.
main.py
to make it run on power-up. – Bosky