How can you make a micropython program on a raspberry pi pico autorun?
Asked Answered
S

3

6

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.

Sidonia answered 13/2, 2021 at 9:27 Comment(1)
Name the script main.py to make it run on power-up.Bosky
P
5

Name the program you want to run main.py

Prescind answered 22/4, 2021 at 16:16 Comment(1)
Nice concise answer, I would add ... create a simple, hardened main.py that runs your program. I loathe multiple projects, all with a generic main.py. Its just me.Tades
W
3

Tested on Rpi Pico W, Ubuntu 22.04 host.

Command line approach

  1. Install the MicroPython firmware.

    1. Download the latest prebuilt firmware, it is a UF2 file:

    2. Plug the Pi USB to computer while holding the BOOTSEL button.

      A filesystem should appear on host.

    3. 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/
      
    4. When flashing ends, the filesystem automatically unmounts, leaving you in non-boot mode. The firmware has been installed.

  2. Install your program as main.py on the board.

    1. 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
    1. Install rshell on host:

      python3 -m pip install --user rshell
      
    2. 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

    3. 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 :-)

  1. Install Thonny:

    python3 -m pip install --user thonny
    
  2. Install the MicroPython firmware

    1. Plug the Pico while holding BOOTSEL

    2. Open Thonny from the command line:

      thonny
      

      I tested with version 4.0.1.

    3. Click Python version on bottom right of the screen (bad UI)

    4. Install MicroPython

    5. MicroPython variant: Raspberry Pi Pico (choose W vs non W)

    6. Install

    7. Unplug the Pico, close Thonny, replug the Pico without BOOTSEL, reopen Thonny

  3. Install your program as main.py on the board

    1. Paste our blink.py on the main Thonny editor window

    2. File > Save (or Ctrl + S)

    3. A popup opens, choose: "Raspberry Pi Pico" (instead of "This computer")

    4. Save the file as main.py on the Pico

    5. Unplug USB and replug. main.py starts running.

Waldgrave answered 15/10, 2022 at 9:13 Comment(1)
A "Let there be light" response - the OP stated he was using Thonny already, just name the main program main.py.Tades
A
0

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.

Amorete answered 9/6 at 16:41 Comment(2)
To undo the autostart, you have to delete the main.py program. Not so easy. The simplest thing is to download flash_nuke.uf2 to the pico. It will erase all the user files in flash and allow you to re-download any .uf2 file to continue using the rpi pico.Amorete
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Paco

© 2022 - 2024 — McMap. All rights reserved.