Campbell
Campbell

Reputation: 71

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

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

Answers (3)

user25495227
user25495227

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

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.

Upvotes: 3

cally
cally

Reputation: 51

Name the program you want to run main.py

Upvotes: 5

Related Questions