After completing this guide, you’ll have your first LED blinking using MicroPython and Thonny IDE. We’ve experimented with several IDEs to program the ESP32 and ESP8266 boards using MicroPython, and Thonny seemed a good choice. Although there are some bugs, it is constantly being updated and improved. The best way to program in MicroPython on your Raspberry Pi Pico is with the Thonny Python IDE (integrated development environment). Open the Raspberry Pi menu and choose Programming Thonny Python IDE. Changing the interpreter in Thonny to MicroPython (Raspberry Pi Pico). Bill was using Thonny, a Python IDE that is popular in the education community. Thonny was introduced in 2015 by Aivar Annamaa of the University of Tartu in Estonia. Thonny was designed to address.
I’ve been playing with a few MicroPython projects recently on several different embedded platforms, including a couple of ESP32 WiFi modules. There are various ways to program these modules:
- The buttons you’ll see here allow you to debug and run a script step by step in order to see what’s happening. These step buttons are really powerful because.
- Thonny is a simple Python IDE with features useful for learning programming. See for more info.
- Use a serial terminal and ampy (maintained by [devxpy] since being dropped by Adafruit in 2018).
- If you use Pycom boards or WiFy firmware, there are the
pymakrplugins for Atom and Visual Studio.
- If you prefer the command-line like me, there is rshell by one of the top MicroPython contributors [Dave Hylands].
For over a year, I have been quite happy with
rshell until I started working on these wireless nodes. Being lazy, I want to tinker with my ESP32 modules from the sofa, not drag my laptop into the kitchen or balcony to plug up a USB cable. Can’t I work with them wirelessly?
Well, you can use WebREPL. While its functional, it just didn’t strike my fancy for some reason. [Elliot] mentioned in a recent podcast that he’s using telnet to access his wireless nodes, but he’s using esp-link on an ESP8266, which means throwing another chip into the mix.
The Thonny IDE
I had all but given up when by chance I saw this video on the Dronebot Workshop channel about running MicroPython on the new Raspberry Pi Pico boards. Bill was using Thonny, a Python IDE that is popular in the education community. Thonny was introduced in 2015 by Aivar Annamaa of the University of Tartu in Estonia. Thonny was designed to address common issues observed during six years of teaching Python programming classes to beginners. If you read about the project and its development, you’ll see that he’s put a lot of effort into making Thonny, and it shows.
Leaning about Thonny got me curious, and after a little digging I discovered that it has WebREPL support for MicroPython right out-of-the-box. Although this is a new feature and classified as experimental, I found it reasonably stable to use and more than adequate for home lab use.
Installation and Connection
Instructions are clearly given on the project’s website. There is a downloader link at the top of the page for Windows. For Linux and Mac simply doing
sudo apt install thonny or
brew install thonny was all I needed. If you have a Raspberry Pi, you will find Thonny is pre-installed — version 3.3.3 was found on a recent Pi 4 in my lab. On an older Pi 3 however, Thonny was too old and simply reinstalling it didn’t work for me. Doing a
sudo apt update,
sudo apt upgrade, and then reinstalling Thonny resulted in the latest version 3.3.6.
For some reason, the Thonny found in the Ubuntu 20.04 and Debian Buster package repositories also weren’t recent enough. Rather than building from source, I used the provided Linux installation script that downloads and installs the latest release.
In order to connect to a module, you will use the
Run -> Select interpreter.. menu. Here you pick which type of module to use, and which interface (USB or WebREPL). Note that you have to connect to your module over USB first and make sure that WebREPL is enabled in
boot.py. This is the normal method of enabling WebREPL in general — if your module already has WebREPL setup, you don’t need to change anything. Fill in the WebREPL URL and password — you should connect and be good to go.
By default, Thonny’s windows are stacked vertically. If you’re using a modern wide-screen monitor, you will probably want to rearrange the windows in Thonny so the editor and shell are side-by-side (see the feature image). This is done by adding
ShellView.location = e to the
[view] section of the
configuration.ini file (see here). The
East, and other compass points like
se can be specified as well. For the systems I tested, the configuration file can be found in the following places:
- Raspi, Ubuntu, Debian
I had no problems using the REPL manually, but I discovered an issue once you start testing self-starting applications with forever loops. If you are not careful, your application’s main loop can prevent you from regaining REPL control. The solution is to make sure your application sleeps for several milliseconds in the main loop. I tested with 100 ms (see here) and was always able to regain control. This shouldn’t be an issue in most home sensor applications, where the module may sleep for tens of minutes between readings.
By default, a Thonny installation doesn’t make itself visible for command line execution. On the computers I tested, you will find the Thonny executable in the following locations:
- Ubuntu, Debian (installed via installer script)
Multiple Modules and Instances
Thonny Python Ide Opencv
Thonny can support a variety of different modules. But if you want to connect to multiple modules at the same time, you need to run several instances of Thonny simultaneously. By default, only one instance is allowed. In the
Tools -> Options -> General menu, untick the
Allow only single Thonny instance checkbox.
I noticed one quirk on MacOS, however. For some reason, unchecking that option doesn’t work when launching the app from the GUI. There is a workaround — you can get multiple instances running if you start them from the command line.
One more annoyance — by default, Thonny will use the previous interpreter / connection when starting up. This is reasonable behavior, but causes a hiccup when talking to multiple boards simultaneously. Just ignore the plethora of error messages when Thonny instance #2 tries to connect to an already connected module, and use the menus to connect to the second device. There apparently isn’t any command line argument for a specific connection, but I have an inquiry pending on the GitHub project page.
Tip of the Iceberg
I have only focused on the WebREPL connectivity aspect of Thonny here, a functionality which seems to be uncommon in other MicroPython programming workflows. But this only scratches the surface of Thonny. Under the hood, it has a lot to offer that is hidden when you first open it.
And while I have been using its MicroPython mode of operation, Thonny was designed to aid students who are learning to write and debug “normal” Python programs running on desktop computers. If you frequently work in Python, I would encourage you to have a look at these aspects of the program — an object inspector, debugger, single-stepping, and a whole slew of other useful tools.
Thonny is also a multi-platform tool. While preparing this article, I tested it on MacOS, Windows 10, Debian, Ubuntu, and Raspberry Pi computers with no problems. Basic sigils and their meanings. The project is open-sourced under the MIT License.
At the very least, Thonny is a great solution for remote MicroPython programming of WiFi embedded modules. Check it out and give it a test run, especially if you’re lazy like me.
When you are developing a Python 3 application with Thonny, you may want to get Thonny to use a virtual environment to run and debug your application.
With this in mind, this post shows you how to associate a Python 3 virtual environment with Thonny.
Creating a virtual environment with python3-venv
This tutorial was created with Thonny running on Raspbian Stretch. The steps to associate a Python 3 virtual environment with Thonny running on Windows should be similar.
Before proceeding further, make sure that you had created a virtual environment. In case you need it, you can follow one of the following tutorials to create your Python 3 virtual environment:
- Create a virtual environment with python3-venv on Linux or Unix.
For the purpose of this post, let's assume that we had created a Python 3 virtual environment at /home/pi/my-venv.
Steps to associate a Python 3 virtual environment with Thonny
Firstly, open up your Thonny IDE.
If you see that your Thonny IDE does not have the menu bar, click on Switch to regular mode on the top right corner. After you had done so, Thonny will show you a confirmation message:
Click on Ok and reopen Thonny. After Thonny had been reopened, it will show a menu bar at the top:
Given that, go to Tools ->Options:
Click on the Interpreter tab:
As shown above, Thonny is using the same Python interpreter which runs Thonny to run your Python 3 application. Click on the combo box and choose Alternative Python 3 interpreter or virtual environment:
Click on Locate another python executable.. and a file dialog will appear for you to choose the python executable inside your virtual environment:
Click Ok to confirm your selection:
Click Ok again to confirm your configuration changes. Once you had done so, you will see that Thonny restarts the shell below with the Python executable from your virtual environment:
Given that, Thonny will run your Python 3 application inside your virtual environment.
Thonny Ide Python Download
Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.