Micropython-IDE-and-Tools

From pymate.io wiki
Jump to navigation Jump to search


Abstract

There is no need for a specific development environment to work with a MicroPython plateforms.

A simple text editor is quite enough to write Python scripts.

Command line utility like mpremote can be used to copy/run python scripts on the board.

Newcomer usually prefers integrated environment like Thonny IDE specially designed for learning Python.

Thonny editor

Initially designed for Python usage on computer, the Thonny IDE can definitively improve the user experience and interaction with MicroPython.

ENG-CANSAT-PICO-THONNY-00.png

Thonny is a multi-plateform editor running on Linux, Windows, Mac.

The beauty about Thonny Python editor is that Thonny is written in Python.

Thonny IDE works great with many micro-controller running MicroPython, this includes the PymateIO platform.

Thonny offers great features for MicroPython micro-controller:

  • Transfer script to MicroPython micro-controller,
  • Manage files on the MicroPython micro-controller,
  • Run script against MicroPython micro-controller for computer (great for testing),
  • Using the MicroPython interactive command line (named REPL),
  • Generating graphical data from script results.

Install Thonny IDE

It is important to install a recent version of Thonny to gets the plug-ins connecting with MicroPython boards.

Thonny website contains the required instruction to install Thonny IDE.

On Raspberry-Pi

Just open a terminal and execute the following command line:

$ sudo apt update && sudo apt upgrade -y

This will upgrade the operating system and install the latest version of Thonny IDE.

On Linux

Python3 is available on almost every Linux operating system. So Thonny (which is written in Python) is installed with the pip3 Python Install Package software.

$ pip3 install thonny

On Linux you may need to uninstall the "modem manager" package (if any installed) as it monopolise any serial communication device appearing on the system.

The current user must also be part of the "Dialout" group allowing him to connect directly to any serial communication device. In such case of modification, logout and login again to update the user credentials.

Windows & Mac

Refer to the official Thonny.org documentation.

Working with Thonny

The Thonny environment look very sober at first sight.

Don't be fooled by this simplicity, Thonny do have everything you need under the hood.

THONNY-IDE-small.png

From top to bottom, the current configuration displays:

  • The Menu-bar,
  • The Toolbar (with icons),
  • The code editor as main window section.
  • The Shell interface (at the bottom) offering interactive access to test Python snipped/instruction/code.

Connect your PymateIO

First think first: The PymateIO must be connected to the computer prior to establish a connection between Thonny IDE and PymateIO.

Pymate-connect-USB.jpg

Few seconds after USB connexion, the PymateIO shows its internal filesystem as Pymate_IO drive.

Connect-Pymate-to-computer.png

Thonny and MicroPython

By default, Thonny use the computer's Python interpreter in the "Shell" section.

The PymateIO running MicroPython expose its REPL shell over the PymateIO board serial interface (Serial over USB).

In order for Thonny to work with a PymateIO MicroPython board, the user must inform Thonny where it find the the MicroPython Shell to be used.


To start: click the bottom right label showing the current "Python Shell" in use.

This action popup the "shell selection" popup at the bottom-right corner of the screen.

Then select the "MicroPython" entry attached to the MicroPython board.

Pymate-Thonny-Sel-Micropython.png

Once the entry selected, Thonny does establish a new connection with the MicroPython command line interpreter (REPL). This is is running on the PymateIO MicroPython board.

Pymate-Thonny-REPL.png

When the connection fails then the user can press the toolbar "stop" to make a new connection attempt.

The image here below MicroPython shell (REPL, as it is executed on the microcontroler). This would work the same for any of the RP2040 based microcontroler (like the MicroMod-RP2040, PYBStick RP2040, etc).

Pymate-Thonny-REPL-restart.png

LED Blinking

The MicroPython REPL act as an interactive shell.

This can be used to test Python code on the fly.

Let's make the PyMate LED 1 lighting on the Core.

Pymate-LED-Blinking.png

Note: Powering the Core via the USB-C is enough for this to work.

This can be done with few line of code.

from pymate.board import Board
brd = Board()

brd.led1.on()
brd.led1.off()


The following screen capture just show the results of entering the command lines.

Pymate-LED-Blinking2.png

The LED state can be retrieved with brd.led1.value() which returns the current state as 1 or 0.

Pymate-LED-Blinking3.png

Introducing a boolean expression to the brd.led1.value( boolean_param ) call will also change the LED state.

e.g. value( 1 ) , value( True ) , value( 5>1 ) are valid state change calls for the LED1.

Pymate-LED-Blinking4.png

Run & Stop a script file

With Thonny you can also write Python inside a file stored on your computer and execute it on the target PymateIO board.

In the following example, LED1 and LED2 blink each one after the other.

Key-in the following script into the Thonny code editor:

from pymate.board import Board
import time
brd = Board()
l1 = brd.led1
l2 = brd.led2
l1.on()
while True: # infinite loop
    # Invert LED1
    l1.value( not(l1.value()) )
    # LED1 is invert value of LED1
    l2.value( not(l1.value()) )
    print( 'LED1=%s , LED2=%s' % (l1.value(),l2.value()) )
    time.sleep(1)

Then save the file under the name "demo.py" either on the computer, either on the micro-controller board.

No matter where it is saved (computer or micro-controller), it can be executed against the micro-control-er and shows the results into the REPL/shell section.

To execute the script, you can either:

  • Press the "Start" button Pymate-Thonny-start-btn.png from the toolbar
  • Select the menu "Run | Run current script"
  • Press the "F5" key

Once running, the script execution will displays information message with the print() function. The messages are visible in the "Shell" section of Thonny

The onboard LEDs are also changing their state every seconds.

Pymate-thonny-run-script.png

To stop the script (since it run infinitely), you can either:

  • press the "Stop" button on the toolbar
  • Press the "Ctrl+C" combination into the "Shell" section

Use the plotter

The plotter is a graphical tool displaying script outputs generated with print() as chart.

The plotter feature may be quite useful to monitor sensor values (e.g. temperature, luminosity, ...) while testing your script.

The chart is updated each time a new line with data is received in the "Shell" section.

The Plotter is activated with the "View | Plotter" menu. The action displays the plotter tool aside the shell section.

Pymate-thonny-plotter-10.png

The Plotter catch the numeric values sent in the REPL/Shell (with print statements) then update the chart. Only lines containing numeric values are used by the plotter.

As this section didn't yet covers measurement with Pymate, the Plotter explanations are based on the usage of Thonny with a Pico microcontroler (running MicroPython).
Otherwise, handling the plotter with PymateIO board if identical.

On the following example, tge script use the gamma function to make the LED pulsing in a natural way. Indeed, a progressive PWM signal increase would not offers a proportional luminosity increase on the LED (see book MicroPython et Pyboard, Edition ENI, in French).

Using the gamma() function to rectify the apparent proportionality between the PWM signal and emitted light (as perceived by the eyes.

# MicroPython example for Raspberry-Pi Pico
#
from machine import Pin, PWM
import time
led = PWM( Pin(25 ))

def gamma( pc ):
    return pow(pc/100,2.2)*100

while True:
    for i in range( 0, 100, 5 ):
        pwm_val = int(gamma(i)*65534/100)
        led.duty_u16( pwm_val )
        print( pwm_val )
        time.sleep_ms( 20 )
    for i in range( 0, 100, 5 ):
        pwm_val = int(gamma(100-i)*65534/100)
        led.duty_u16( pwm_val )
        print( pwm_val )
        time.sleep_ms( 20 )

Which produce the following results in the Thonny Plotter.

PymateIO-Thonny-Plotter-0.png

Drawing several curves

By modifying the script, we can return the PWN value and the gamma corrected value by using a Python tuple. Then the plotter will draw several curves.

from machine import Pin, PWM
import time
led = PWM( Pin(25 ))

def gamma( pc ):
    return pow(pc/100,2.2)*100

while True:
    for i in range( 0, 100, 5 ):
        pwm       = int( i*65534/100 )
        pwm_gamma = int(gamma(i)*65534/100)
        led.duty_u16( pwm_gamma )
        print( (pwm, pwm_gamma) )
        time.sleep_ms( 20 )
    for i in range( 0, 100, 5 ):
        pwm       = int((100-i)*65534/100)
        pwm_gamma = int(gamma(100-i)*65534/100)
        led.duty_u16( pwm_gamma )
        print( (pwm, pwm_gamma) )
        time.sleep_ms( 20 )

Which produce the following results in Thonny.

ENG-CANSAT-PICO-THONNY-12.png

As seen in the REPL/Shell output, the data are return under the format (pwm, pwm_gamma) . The first value is the proportional curve and the second value the gamma fixed value.

The plotter does show the curve correspondence between tuple value position and drawn curve (see to bottom right corner).

System Shell - Pure REPL

It is also possible to start a Pure REPL session inside an external terminal. By using a external terminal, the user escape from the Thonny shell panel and being directly connected to the microcontroler via an USB serial connection.

The "Tools | Open System Shell.." menu will open the terminal Window.

ENG-CANSAT-PICO-THONNY-20.png

Notes:

  • Thonny must be connected to the target platform to be able to open the External Terminal.
  • The file manager can helps to check, whether or not, when Thonny is connected to the MicroPython board.
  • It is not possible to open two terminal session (at the same time) to the MicroPython micro-controller.

File manager

It is common to copy library/libraries on the board.

This will requires to transfer one (or several) files to the MicroPython board.

Such libraries are usually python scripts (eg: this bme280.py file) coded for target sensors (eg: BME280 atmospheric & temperature I2C sensor).

Thank to Thonny you also have a dedicated "file manager" tool used to transfer files between the computer and the MicroPython board.

The file manager is available via the "View | Files" menu.

ENG-CANSAT-PICO-THONNY-30.png

Which would display the Thonny file manager.

ENG-CANSAT-PICO-THONNY-31.png

To transfer a file from the computer to the MicroPython board, you will need to :

  • Select the file into the "Computer" pane
  • Activate the contextual menu (mouse right click)
  • Select the option "Upload to" in the menu
The "/" drawed into the menu match the root folder on the target (the microcontroler in this case).

ENG-CANSAT-PICO-THONNY-32.png

It would be easy to identify the features and behaviour of the file manager by exploring and testing the various entries in the contextual menu. Please note that contextual menu are different in the board pane and computer pane.


PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO