Hello!
I can't find any documentation on writing my microPython script to the device so it runs at startup. I am using thonny and can make my code work when I push the green play button. However, if you reboot the Pico it just does nothing.
Thanks for the help!
-
- Posts: 2
- Joined: Tue Feb 02, 2021 1:58 am
Re: How do I run a script at Pico poweron?
I believe this is in the official documentation but in brief:
- Save you python program on whatever device you're running thonny on.
- Make a copy of it called main.py
- Disconnect the pico.
- Hold down the bootsel button and reconnect the pico.
- Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
- Copy main.py to the mass storage device provided by the pico.
I'm a volunteer. Take me for granted or abuse my support and I will walk away
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
Re: How do I run a script at Pico poweron?
archlinuxusa wrote: ↑Tue Feb 02, 2021 2:02 amHello!
I can't find any documentation on writing my microPython script to the device so it runs at startup. I am using thonny and can make my code work when I push the green play button. However, if you reboot the Pico it just does nothing.
Thanks for the help!
viewtopic.php?f=144&t=300539#p1805666
- aallan
- Raspberry Pi Trading Employee & Forum Moderator
- Posts: 277
- Joined: Mon Feb 09, 2015 11:30 am
- Location: Exeter, UK
Re: How do I run a script at Pico poweron?
No. If you're running MicroPython you should never ever push the BOOTSEL button. This drops you into boot mode, which is lower level that the MicroPython firmware. The mass storage device you are offered when in boot mode is unrelated to the MicroPython filesystem. You can not access the MicroPython file system from boot mode.5. Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
6. Copy main.py to the mass storage device provided by the pico.
The only thing you can do in boot mode is to copy another UF2 file (for instance a new version of the MicroPython firmware) onto Pico.
The only way to get files into and out of the MicroPython file system is via Thonny, another MicroPython editor, or with tools like rshell.
Head of Documentation
Raspberry Pi Ltd
About me, http://alasdairallan.com
Raspberry Pi Ltd
About me, http://alasdairallan.com
Re: How do I run a script at Pico poweron?
I think this is likely to be a common misconception. BOOTSEL has nothing to do with uPython once you initially load that UF2 onto the Pico.thagrol wrote: ↑Tue Feb 02, 2021 10:23 amI believe this is in the official documentation but in brief:
- Save you python program on whatever device you're running thonny on.
- Make a copy of it called main.py
- Disconnect the pico.
- Hold down the bootsel button and reconnect the pico.
- Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
- Copy main.py to the mass storage device provided by the pico.
Thereafter save to Pico from Thonny (presumably there are other tools to do that but Thonny works well.)
Re: How do I run a script at Pico poweron?
I stand corrected. Thanks.aallan wrote: ↑Tue Feb 02, 2021 12:21 pmNo. If you're running MicroPython you should never ever push the BOOTSEL button. This drops you into boot mode, which is lower level that the MicroPython firmware. The mass storage device you are offered when in boot mode is unrelated to the MicroPython filesystem. You can not access the MicroPython file system from boot mode.5. Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
6. Copy main.py to the mass storage device provided by the pico.
The only thing you can do in boot mode is to copy another UF2 file (for instance a new version of the MicroPython firmware) onto Pico.
The only way to get files into and out of the MicroPython file system is via Thonny, another MicroPython editor, or with tools like rshell.
Apologies for the misinformation.
I'm a volunteer. Take me for granted or abuse my support and I will walk away
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
Re: How do I run a script at Pico poweron?
thagrol wrote: ↑Tue Feb 02, 2021 10:23 amI believe this is in the official documentation but in brief:
- Save you python program on whatever device you're running thonny on.
- Make a copy of it called main.py
- Disconnect the pico.
- Hold down the bootsel button and reconnect the pico.
- Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
- Copy main.py to the mass storage device provided by the pico.
I have the solution to your problem, if you have a MacBook.
Step 1: Install rshell (pip3 install rshell)
Step 2: Connect to your Pico ( rshell --buffer-size=30 -p /dev/tty.usbmodem0000000000001 -a ) # NOTE: Your /dev/ naming may differ)
Step 3: Copy your main.py code to the Pico ( cp main.py /pyboard )
Step 4: Confirm your code is on the Pico ( ls /pyboard )
Step 5: Disconnect ( Control-D)
Step 6: Disconnect your Pico / Reconnect your Pico. Your code is now running.
Test Code to Blink the Onboard LED:
Code: Select all
from machine import Pin
import utime
led = Pin(25, Pin.OUT)
led.low()
while True:
led.toggle()
utime.sleep(0.2)
Notes from my example:
Code: Select all
my_macbook$ pip3 install rshell
Collecting rshell
Using cached rshell-0.0.28.tar.gz (45 kB)
Collecting pyserial
Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB)
Collecting pyudev>=0.16
Using cached pyudev-0.22.0.tar.gz (85 kB)
Requirement already satisfied: six in /usr/local/Cellar/protobuf/3.14.0/libexec/lib/python3.9/site-packages (from pyudev>=0.16->rshell) (1.15.0)
Building wheels for collected packages: rshell, pyudev
Building wheel for rshell (setup.py) ... done
Created wheel for rshell: filename=rshell-0.0.28-py3-none-any.whl size=40988 sha256=da5d057dddefe40aeb5228a6871069a9361cdaf03047f9f739f7dbd011cee699
Stored in directory: /Users/alby/Library/Caches/pip/wheels/71/d6/51/255857bac77921d458fb3b1259bea0681097fc7fa8c76c8b69
Building wheel for pyudev (setup.py) ... done
Created wheel for pyudev: filename=pyudev-0.22.0-py3-none-any.whl size=63469 sha256=7ceb3c14d26a2fbdd1a59e9e4646821ff7a20bc7035cb356db18860b987c7906
Stored in directory: /Users/alby/Library/Caches/pip/wheels/7a/ec/98/3b45a64ff006ead76ff041ecc06e4f0aee5ed477abac26e786
Successfully built rshell pyudev
Installing collected packages: pyudev, pyserial, rshell
Successfully installed pyserial-3.5 pyudev-0.22.0 rshell-0.0.28
my_macbook $ rshell --buffer-size=30 -p /dev/tty.usbmodem0000000000001 -a
Using buffer-size of 30
Connecting to /dev/tty.usbmodem0000000000001 (buffer-size 30)...
Trying to connect to REPL connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ...
Setting time ... Feb 02, 2021 17:15:11
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 1970
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
/Users/alby/py_code>
/Users/alby/py_code> cp main.py /pyboard
/Users/alby/py_code> ls /pyboard
main.py
/Users/alby/py_code>
Re: How do I run a script at Pico poweron?
I couldn't get rshell to connect to my board on macOS Big Sur, kept getting the error below:
No MicroPython boards connected - use the connect command to add one
So instead I took the Thonny route, to Save As main.py
I followed these steps to run and test my Python code with Thonny: https://desertbot.io/blog/raspberry-pi-pico-setup-mac
(for interest I'm using the pimoroni Keypad Demo example, https://github.com/pimoroni/pimoroni-pi ... ad/demo.py )
I ran this demo code in Thonny, then did File > Save as, and then click "Raspberry Pi Pico" on the "Where to save to" dialog.
The file is saved as main.py
Now disconnect/reboot, the original demo.py (now saved as main.py) is running on startup.
Code: Select all
$ rshell -p /dev/tty.usbmodem0000000000001 --buffer-size 512
So instead I took the Thonny route, to Save As main.py
I followed these steps to run and test my Python code with Thonny: https://desertbot.io/blog/raspberry-pi-pico-setup-mac
(for interest I'm using the pimoroni Keypad Demo example, https://github.com/pimoroni/pimoroni-pi ... ad/demo.py )
I ran this demo code in Thonny, then did File > Save as, and then click "Raspberry Pi Pico" on the "Where to save to" dialog.
The file is saved as main.py
Now disconnect/reboot, the original demo.py (now saved as main.py) is running on startup.
Re: How do I run a script at Pico poweron?
There are (at least) two files executed, main.py as well as boot.py
I just tested with these simple files (the sleep is needed to show message on startup):
As you can see after opening minicom Micropython session, disconnecting and reconnecting USB cable, as well as soft reboot by pressing CTRL-D later, both execute boot.py first, then main.py:
I just tested with these simple files (the sleep is needed to show message on startup):
Code: Select all
pi@raspberrypi400:~/rshell $ cat boot.py
import time
time.sleep(2)
print("boot.py")
pi@raspberrypi400:~/rshell $
pi@raspberrypi400:~/rshell $ cat main.py
print("main.py")
pi@raspberrypi400:~/rshell $
As you can see after opening minicom Micropython session, disconnecting and reconnecting USB cable, as well as soft reboot by pressing CTRL-D later, both execute boot.py first, then main.py:
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html
Re: How do I run a script at Pico poweron? .uf2 not 'sticking'
I had a lot of troubles because my .uf2 did not 'stick' to the Pico. I had to retransfer each time I reconnected. After a couple of days of trying and searching, I have discovered that you need to specify on Thonny which port to use by clicking on the bottom right or on "tools". The port you choose depends on the OS you are using. Any more advice on the meaning of all this and general guidance on how to deal with this would be greatly appreciated. Many thanks.
-
- Posts: 6042
- Joined: Sat Aug 18, 2012 2:33 pm
Re: How do I run a script at Pico poweron?
when you boot the pico in BOOTSEL mode, it will ONLY accept UF2 filesthagrol wrote: ↑Tue Feb 02, 2021 10:23 amI believe this is in the official documentation but in brief:
- Save you python program on whatever device you're running thonny on.
- Make a copy of it called main.py
- Disconnect the pico.
- Hold down the bootsel button and reconnect the pico.
- Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
- Copy main.py to the mass storage device provided by the pico.
any other file you write to it, will be 100% ignored and just thrown directly into the trash bin
Re: How do I run a script at Pico poweron? .uf2 not 'sticking'
Thonny needs to know the virtual serial port the Pico is connected to so it can communicate with it. Once the Run -> Select Interpreter menu option has been set to "MicroPython (Raspberry Pi Pico)", Thonny should be able to automatically determine the correct serial port. If not that can be manually selected, is normally "/dev/ttyACM0" when run under Raspberry Pi OS and likely the same for other Linux systems.vsenso wrote: ↑Fri Mar 05, 2021 10:34 amI have discovered that you need to specify on Thonny which port to use by clicking on the bottom right or on "tools". The port you choose depends on the OS you are using. Any more advice on the meaning of all this and general guidance on how to deal with this would be greatly appreciated. Many thanks.
Settings should persist between Thonny sessions so it shouldn't need to be done again unless the Pico is on a different serial port than it was.
At least that's been my experience using Thonny on a Pi. What computer and OS are you using, what version of Thonny do you have ?
Re: How do I run a script at Pico poweron?
I have no Mac experience but I have found 'ampy' easier to use than 'rshell' and, written in Python, it should be platform independent. You could try ...
Code: Select all
sudo pip3 install adafruit-ampy
ampy --port /dev/tty.usbmodem0000000000001 put main.py
ampy --port /dev/tty.usbmodem0000000000001 ls
Re: How do I run a script at Pico poweron?
viewtopic.php?p=1831290#p1811392cleverca22 wrote: ↑Fri Mar 05, 2021 2:27 pmwhen you boot the pico in BOOTSEL mode, it will ONLY accept UF2 filesthagrol wrote: ↑Tue Feb 02, 2021 10:23 amI believe this is in the official documentation but in brief:
- Save you python program on whatever device you're running thonny on.
- Make a copy of it called main.py
- Disconnect the pico.
- Hold down the bootsel button and reconnect the pico.
- Release the bootsel button once the pico has booted and the USB host has detected it as a mass storage device.
- Copy main.py to the mass storage device provided by the pico.
any other file you write to it, will be 100% ignored and just thrown directly into the trash bin
I'm a volunteer. Take me for granted or abuse my support and I will walk away
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
-
- Posts: 6042
- Joined: Sat Aug 18, 2012 2:33 pm
Re: How do I run a script at Pico poweron?
ah, didnt review the entire thread, my bad
- RoosterCogburn
- Posts: 5
- Joined: Thu Apr 01, 2021 10:24 am
Re: How do I run a script at Pico poweron?
For the benefit of anyone reading this, who was like myself, deeply confused by this process......
The official documentation instructions I read agree exactly with what thagrol thought, i.e. you boot the Pico with BOOTSEL pressed, copy the main.py to the Pico, then disconnect/reconnect to run the main.py. This clearly doesn't work though, because it is wrong. BOOTSEL mode is ONLY to update the UF2 file.
If you boot the Pico in BOOTSEL mode, you get to see a volume mounted, and it's obvious (BUT WRONG) to think that this is where you dump your main.py file.
This was source of my confusion
. If you connect your Pico without BOOTSEL pressed, you don't get to see the Pico as a mounted device, and Thonny doesn't initially appear to give the option to save (or run) a program on the Pico. The answer is behind that unassuming button (that doesn't look at all like a button) in the bottom right-hand corner of Thonny, which allows one to Configure Interpreter. It will just read "Python 3.7" or something like that. That button is the key.
The following procedure assumes that you have already booted your Pico in BOOTSEL mode, copied on a UF2 file, and then disconnected your Pico. BTW, I must have copied twenty or more UF2 files onto my Pico before I figured out the next bit, so don't worry if you've done that a few times already - it's not a bad thing, you won't have hurt the Pico, and you probably should do this from time to time anyway to keep the firmware up to date.
The Procedure
You should now be able to write scripts, and save them to the Pico. If you have an existing code window open and find that the Save button does not give that option, try copying all of the code to a fresh window and trying to save it from there. Don't forget, your Pico program must be called main.py even if it imports code from other .py scripts.
I strongly suggest you run the code after saving to be sure it executes. If there is a bug, either in the logic or syntax, it is easy to spot now. It may not be later.
After that you should be able to disconnect your Pico and then plug it in to a power source (either the same computer you just programmed it from, or for example a USB power bank, or USB mains adapter), and the code will just run.
When you reconnect the Pico to your (programming) computer, the Pico code will start executing, and when you open Thonny and then open the main.py on the Pico you can click the Stop button to end execution of main.py on the Pico
Final Thoughts
I have programmed my Pico from an 8GB Pi4, and also from a PiZero. I figured my Pi4 is a great computer, and now I've ditched my TV licence, I can leave it permanently attached to my TV to watch YouTube and Netflix etc, and just use the PiZero to program the microcontrollers. I would hate to wreck my Pi4 with some horrible wiring mistake building a breadboard circuit. If I wreck my PiZero doing that, at least it's cheap to replace.
Because all Pico programs are called main.py, I keep a folder called PicoPrograms which contains subfolders for each main.py program where the subfolder name is a meaningful program name. Each main.py file shares its folder with photos of the breadboard (and in a few cases circuit diagrams) so I can reconstruct any circuit and load its associated program code.
Hope this helps someone.
The official documentation instructions I read agree exactly with what thagrol thought, i.e. you boot the Pico with BOOTSEL pressed, copy the main.py to the Pico, then disconnect/reconnect to run the main.py. This clearly doesn't work though, because it is wrong. BOOTSEL mode is ONLY to update the UF2 file.
If you boot the Pico in BOOTSEL mode, you get to see a volume mounted, and it's obvious (BUT WRONG) to think that this is where you dump your main.py file.
This was source of my confusion

The following procedure assumes that you have already booted your Pico in BOOTSEL mode, copied on a UF2 file, and then disconnected your Pico. BTW, I must have copied twenty or more UF2 files onto my Pico before I figured out the next bit, so don't worry if you've done that a few times already - it's not a bad thing, you won't have hurt the Pico, and you probably should do this from time to time anyway to keep the firmware up to date.

The Procedure
- Connect your Pico using a USB Data cable*, NOT in BOOTSEL mode - Don't touch that button! Don't even look at it aggressively.
- Start Thonny
- Left-click the Python button in the bottom right-hand corner, and choose 'Configure interpreter'
- Select interpreter - "MicroPython (Raspberry Pi Pico)"
- Select Port - "Board in FS mode - Board CDC (/dev/ttyACM0)
- Click OK
You should now be able to write scripts, and save them to the Pico. If you have an existing code window open and find that the Save button does not give that option, try copying all of the code to a fresh window and trying to save it from there. Don't forget, your Pico program must be called main.py even if it imports code from other .py scripts.
I strongly suggest you run the code after saving to be sure it executes. If there is a bug, either in the logic or syntax, it is easy to spot now. It may not be later.
After that you should be able to disconnect your Pico and then plug it in to a power source (either the same computer you just programmed it from, or for example a USB power bank, or USB mains adapter), and the code will just run.
When you reconnect the Pico to your (programming) computer, the Pico code will start executing, and when you open Thonny and then open the main.py on the Pico you can click the Stop button to end execution of main.py on the Pico
Final Thoughts
I have programmed my Pico from an 8GB Pi4, and also from a PiZero. I figured my Pi4 is a great computer, and now I've ditched my TV licence, I can leave it permanently attached to my TV to watch YouTube and Netflix etc, and just use the PiZero to program the microcontrollers. I would hate to wreck my Pi4 with some horrible wiring mistake building a breadboard circuit. If I wreck my PiZero doing that, at least it's cheap to replace.
Because all Pico programs are called main.py, I keep a folder called PicoPrograms which contains subfolders for each main.py program where the subfolder name is a meaningful program name. Each main.py file shares its folder with photos of the breadboard (and in a few cases circuit diagrams) so I can reconstruct any circuit and load its associated program code.
Hope this helps someone.
Re: How do I run a script at Pico poweron?
That is wrong, doesn't and can't work.RoosterCogburn wrote: ↑Thu Apr 01, 2021 12:13 pmThe official documentation instructions I read agree exactly with what thagrol thought, i.e. you boot the Pico with BOOTSEL pressed, copy the main.py to the Pico, then disconnect/reconnect to run the main.py. This clearly doesn't work though, because it is wrong.
Could you please indicate exactly where you found such instructions so they can be reviewed and corrected as appropriate.
- makerportal
- Posts: 8
- Joined: Fri Jun 12, 2020 5:02 pm
- Location: New York, NY
Re: How do I run a script at Pico poweron?
I think what you're looking for is to save your script as main.py on the Pico. That should do the trick.
We have a full tutorial on our blog about how to run on boot: https://makersportal.com/blog/raspberry ... icropython
Here's a screenshot of what you want to do:

We have a full tutorial on our blog about how to run on boot: https://makersportal.com/blog/raspberry ... icropython
Here's a screenshot of what you want to do:

Re: How do I run a script at Pico poweron?
Just to note that your "Step 1 : Download here" link to https://www.raspberrypi.org/documentati ... 210121.uf2 doesn't work, gives a 404makerportal wrote: ↑Sat Apr 03, 2021 2:13 pmWe have a full tutorial on our blog about how to run on boot: https://makersportal.com/blog/raspberry ... icropython