Introduction: Uploading Code With Thonny

About: Retired due to health. Oldish. My background is in Structural Engineering. Also smith of many trades. The majority of my project will be what can be made sat in a chair within arm's reach, on a plotter, 3D pri…

This is just a small Instructable, about using Thonny to upload MicroPython - Python for microcontrollers to a Microcontroller.

  • It is something I can refer to from other Instructables that I do that requires uploading code to a Microcontroller running MicroPython.
  • It is a Reference Instructable for people to use if some MicroPython code needs to be installed to a microcontroller.
  • I hate repeating my self (haha), I hope to do more than one project that will require code uploading to a Microcontroller running MicroPython.
  • I hope others will find it useful.
  • In this Instructable I have a Raspberry Pi Pico but it will apply for several types of Microcontroller, the Microcontroller just needs to be one able to run MicroPython.

This is what the Thonny Site says:

Thonny: A Python IDE for Beginners

  • Thonny is a free and open-source Python Integrated Development Environment (IDE) designed specifically for beginners.
  • It was created by Aivar Annamaa.
  • Thonny comes with Python 3.10 built in, so just one simple installer is needed and you’re ready to learn programming.

Key Features of Thonny

  • Easy to get started: Thonny comes with Python 3.10 built in, so just one simple installer is needed and you’re ready to learn programming.
  • No-hassle variables: Once you’re done with hello-worlds, select View → Variables and see how your programs and shell commands affect Python variables.
  • Simple debugger: Just press Ctrl+F5 instead of F5 and you can run your programs step-by-step, no breakpoints needed.
  • Step through expression evaluation: If you use small steps, then you can even see how Python evaluates your expressions.
  • Faithful representation of function calls: Stepping into a function call opens a new window with separate local variables table and code pointer.
  • Highlights syntax errors: Unclosed quotes and parentheses are the most common beginners’ syntax errors. Thonny’s editor makes these easy to spot.
  • Explains scopes: Highlighting variable occurrences reminds you that the same name doesn’t always mean the same variable and helps spotting typos.
  • Code completion: Thonny offers code completion, which is a great help for beginners.

Supplies

Thonny

A Microcontroller.

  • MicroPython is a lean and efficient implementation of the Python 3 programming language, which is optimized to run on microcontrollers. Microcontrollers that have a substantial amount of memory can run MicroPython.
  • Thonny supports several microcontrollers (see screenshot for reference). When you use MicroPython, it’s akin to having an operating system (OS) on your microcontroller. The code you write is not just a script, but a full-fledged program. This program is read, compiled, and executed directly on the microcontroller, providing a powerful and flexible way to control hardware.

USB Driver

  • Depending on whether your Operating System (OS) detects the correct driver for your Microcontroller, You may need to install a USB Driver manually.
  • Usually when connecting Microcontrollers just a generic driver for USB devices is needed.
  • There is software called Zadig that makes it easy to install a generic driver USB Driver.

Step 1: USB Driver

First make sure you Microcontroller is recognised when you connect it to the USB to your computer.

  • Open "Windows Device Manager"
  • Usually it may be shown just as "USB Serial Device (COM?)". (1)
  • If you are lucky it may say what Microcontroller it is "Arduino Leonardo (COM?)" (2)
  • It could be using the cheap CH340 chip "USB-SERIAL CH340 (COM?)" (3)

If it is not recognized it may be shown as "Unknown USB Device" or similar. (4)

  • If this is the case you will need to find the right driver for it.
  • If it is a well known brand there may be a driver to download on there site.
  • If it is using a generic USB for communication then there is Zadig.
  • Zadig application makes it easy to install a Generic USB Driver to be used with a device that is not recognized.

If it does not automatically detect the unknown device, you can select "Options" and tick "Lest all Devices"

  • Select your Device.
  • Chose "WinUSB" Driver.
  • Click Install button.

It my take a little time.

Step 2: When You First Install

After installation of Drivers and Thonny.

  • Run Thonny.
  • Select "View" (1) and make sure "Files" (2) and "Shell" (3) are ticked.
  • This is the minimum we need to install a program.

Plug your Microcontroller into the USB on your computer.

  • Click the red "STOP" (4) button.
  • This may not do anything at the moment if this is the first time and nothing has been set up.
  • This button is always pressed before you do any file copying or updating.

Lets assume this is the first time.

  • Click "Run" (5)
  • Choose "Configure interpreter..." (6)

This opens "Thonny options" window on the "Interpreter" tab.

  • Click the Dropdown list at the top (7) and choose your Microcontroller.
  • Select the USB COM Port your Microcontroller is connected to. (8)
  • At this point, if your Microcontroller does not have MicroPython installed or you want to update it. You can do so by clicking the link "Install or update MicroPython" (9)
  • Click the "OK" Button when done (10)

You should be all good to go.

Step 3: Upload a Program

Plug in your Microcontroller to the USB on your computer.

As I have mentioned before.

  • When you want to copy, save or update files to the Microcontroller, you need to press the red "STOP" button. (1)

Lets say you are uploading a program I have written.

  • You will have the files saved somewhere on your computer.
  • It is best you have them saved in a folder just as they would be uploaded to the Microcontroller, that is; if some are to be in folders you should have them saved in folders.
  • That way the file tree for local and remote locations will look the same.
  • As Instructables only allows attaching single files, if I was refiring to this Instructable I would state how the files should be uploaded.
  • For this demonstration I have attached four files.
  • Two files need to be in a folder called "lib".

File Locations:

  • lib / framebuf2.py
  • lib / sh1107.py
  • Logo.py
  • main.py

Navigate to where you saved the files on your computer.

  • To do this Click "This computer" (3) link in the "Files tab" (2)
  • Navigate to the folder where you saved the files. (4) Just keep opening the tree in the direction you saved the files.
  • Once you have navigated to your saved files folder, you will see your files underneath the path. (5)
  • You will not see anything in the Microcontroller (6), unless something has already been uploaded to it on a previous occasion.
  • In the main window there may be an "<untitled>" tab. You can close this if you are not going to create your own new code.

To copy all the files to your Microcontroller:

  • Select the "Folder" then holding "Ctrl" Select the other two files outside the "Folder", this should select all files (7)
  • Right-Click over the selected Files. (8)
  • Click on "Upload to /". (9)

This should open a dialog telling you that the files are uploading. (10)

  • Once the files are uploaded, you should see them on the left under the Microcontroller you have connected. (11)

Step 4: View and Edit

Ok you was all done at Step 3, but I know there are some that may want to play and personalize the code.

As I have mentioned before.

  • When you want to copy, save or update files to the Microcontroller, you need to press the red "STOP" button.

You need to remember there are two versions of the code.

  • One is on your computer.
  • One is on the Microcontroller.

The following applies to both version of the code.

  • Double-click a file on the left to open it. (1)
  • Notice the "Name" in the tab. (2)
  • The one in brackets (a) is the one on the microcontroller.
  • The one without brackets (b) is the one on your computer.

You can edit these files.

  • To save just click the Save Icon. (3) The Save Icon, saves the current file/tab you have active.

You don't have to edit both versions of the file.

  • To up date the file that has not been edited, right click/activate the file that has been edited (4) and choose one of the following:
  • File on Microcontroller - "Download to ?????" (5a)
  • File on Computer - "Upload to /" (5b)

Step 5: Run the Program

The powerful thing about Python is; when the file is changed, it is instant.

  • MicroPython running on the Microcontroller will compile and run it as soon as you hit play.

The Program will run when you power you Microcontroller is powered on.

  • This is why when you want to copy, save or update files to the Microcontroller, you need to press the red "STOP" button.

But if you are editing some code and want to see the changes, you can do this instantly.

  • Work with the file that is on the Microcontroller. (1) It is selected and it is the active tab that you are editing.(2)
  • Click the Save Icon to to save the changes to the Microcontroller. (3)
  • Click the Play Icon to run the program. (4)

Remember

When you want to copy, save or update files to the Microcontroller, you need to press the red "STOP" button.

Notes!

The Save Icon will not be active if no saves are needed or the program is running.

The file tree gets updated when the red "STOP" button is pressed.