Reputation: 71
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.
Upvotes: 6
Views: 6822
Reputation: 1
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.
Upvotes: 0
Reputation: 383030
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.
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 at blink.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/9
Unplug 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:
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 board
Paste our blink.py
on the main Thonny editor window
File > Save (or Ctrl + S)
A popup opens, choose: "Raspberry Pi Pico" (instead of "This computer")
Save the file as main.py
on the Pico
Unplug USB and replug. main.py
starts running.
Upvotes: 3