MicroPython with Thonny
As a extra, I tried Seeed xiao RP2040 and IDE Thonny with MicroPython.
What is Thonny and MicroPython
- Thonny is an integrated development environment for Python. that is designed for beginners.
- MicroPython is a simple programming language derived from Python and created to run microcontrollers.
PC working environment
PC:MacBook Pro (13-inch, M1, 2020)
OS: macOS Ventura (ver 13.0.1)
Terminal:zsh
Setting : Rosetta
1. Programming (RGB LED) with Thonny, with MicroPython
At first, I refer to Seeed Studio XIAO RP2040 with MicroPython.
1-1) Process
Setup Thonny
- Download and Install the the latest version of Thonny editor.
After Install finished, below window appeared, then Click "Let's go". -
Launch the Thonny
-
Click "Tools→Options" to open the settings.
-
Chose the "Interpreter" interface and select the device as "MicroPython(Raspberry Pi Pico)" and the port as "Try to detect prot automatically"
Connect XIAO RP2040 to the PC
-
Press and hold the "BOOT" button and then connect the Seeed Studio XIAO RP2040 to the PC through the Type-C cable. If it works well, there is an "RPI-RP2" desk shown on the PC.
-
Click "Install or update MicroPython".
-
It will then automatically search for the device and display it on the Target Volume. In the version selection in Micropython below, I just leave the default.
-
Click on the Install button and close this page when the installation status says Done.
The following information will be shown on the interface once the firmware is complete.
Programming with LED
- Copy the following codes to Thonny.
quote code in Seeed Studio XIAO RP2040 with MicroPython
from machine import Pin, Timer
led = Pin(25, Pin.OUT)
Counter = 0
Fun_Num = 0
def fun(tim):
global Counter
Counter = Counter + 1
print(Counter)
led.value(Counter%2)
tim = Timer(-1)
tim.init(period=1000, mode=Timer.PERIODIC, callback=fun)
- Upload the codes by clicking the "Run current script" button.
NOTE: As "wiki.seeedstudio.com" said, "For the first time, Thonny will ask where you want to save your codes file. Both This Computer and Raspberry Pi Pico are fine. " but it isn't appeared in my mac M1.
- If it works well, I see the LED light turn on and off once a second. And the output of the increasing number will as well be displayed in the Shell.
The connection test is complete.
Then I proceed to the other project.
Programming with RGB LED
There is an RGB LED equipped in the Seeed Studio XIAO RP2040 and we are going to light it up by MicroPython. It is required a third-party library so we need to add an additional library first.
-
Download the ws2812.py library and open it with Thonny.
-
Click "File→Save as" and save the library.
-
Chose the "Raspberry Pi Pico" as the location we save.
-
Make sure the saved file name is "ws2812.py", otherwise it will not work.
-
Copy the following codes to Thonny.
quote code in Seeed Studio XIAO RP2040 with MicroPython
from ws2812 import WS2812
import utime
import machine
power = machine.Pin(11,machine.Pin.OUT)
power.value(1)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE)
led = WS2812(12,1)#WS2812(pin_num,led_count)
while True:
print("Beautiful color")
for color in COLORS:
led.pixels_fill(color)
led.pixels_show()
utime.sleep(0.2)
-
Upload the codes by clicking the "Run current script" button. For the first time, Thonny will ask where you want to save your codes file. Both This Computer and Raspberry Pi Pico are fine.
-
If it works well, you will see the RGB LED light convert and flash the lights. And the output of the text "Beautiful Color" will as well be displayed in the Shell.
2. Personal Impressions
- MicroPython may be a good fit For a beginning programmer like me, as it makes it easy to write code.
- I heard that it is somewhat inferior to C and C++ in terms of operating speed, but I guess it to matter much for the volume code I will make.
- I worked with the quoted code, so I need to learn about the program code.