Main Menu

search

You are here

Thonny with RP2040

[last updated: 2024-09-18]
RP2040-Zero home page
MicroPython on RP2040
-----------------------------

      Disclaiamer: most of what's here came from my own trials and tribulations. FWIW.

  • Thonny is a python IDE, that has Python installed inside it. So you can use it to learn and develop python programs, and test them.
    I'm using it to interact with my RP2040.
    In this mode, I can execute single python commands, or I can execute a sequence of them,
    or I can create a python program then save it to the RP2040 main.py file
    • When you first open Thonny, with no USB device connected, The Shell section at the bottom will probably show the "unable to connect" error (see below).
    • The text line at the very bottom right of the screen will show the interpreter that has been selected (or default).
        Thonny needs to know what version of python interpreter to use to execute the commands you give it.
          It can use the default version embedded in Thonny,
          or it can perhaps use a version that you have separately loaded onto your pc,
          or it can use the version that is loaded onto your USB device (in my case the MicroPython on the RP2040).

        But in any case (unless whatever default comes up is OK with you) you must tell it which one you want to use.
        To do that: Click on the text line, then select "Configure Interpreter"

          For the RP2040, you'll select: MicroPython (Raspberry Pi Pico)
          but then, you must click Configure Interpreter, which will bring up the Thonny Options box
          In the Interpreter tab, in the Port field, select: Board in FS Mode - Board CDC (/dev/tty/ACM0)
          Click OK

  • The Thonny screen has two main sections: the upper code section, and the lower "Shell" section.
    • The Upper section will have a tab at the top that identifies the code displayed. This is the filename where the code comes from (or goes to). It will be a .py file, and may be located on your host PC or on your RP2040 or other USB device. Or, if you haven't yet saved or run it (which will automatically save it), it will say <untitled>
      • When you type a command or a program into Thonny (top, code section), and click Run > Run current script,
        It will execute the program in the selected version of python.
        If you have not yet selected one, it will prompt you to "Configure Interpreter"
      • If you have some python code that you have saved on your PC with the filename eg. p1.py, you can load that into the Thonny code window,
        and you can run current script to test it.
        However note that, in this process, you have not done anything to the main.py program stored on the RP2040.
        If you want to save the p1.py program onto the RP2040 and replace the main.py that's there,
        ...
    • The lower, "Shell" section:
      • There should be a >>> prompt, or at least it will if you have a valid interpreter selected,
        else it will show some kind of error, perhaps:
          "Unable to connect to /dev/ttyACM0: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'
          Backend terminated or disconnected. Use 'Stop/Restart' to restart."

          It will also show this error if you've been working with some device, then you unplug it - even if you plug it or another device back in.
          As it says, you must click Run (top menu bar) > Stop/Restart Backend, in order to reconnect.

            If you are connected and have a prompt, then any python command you type will be immediately executed when you press enter.
        • In the Shell window at the bottom of the Thonny screen, the top line may say something like:
            Python 3.10.12 (/usr/bin/python3)

          This is (apparently) the default python version that is associated with Thonny.

            --------------------------------------------------------------------------------

          • Thonny can be installed onto your Linux PC through the program manager.
          • adsfg