Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

IDE - Workflow

00:00 IDE. A great deal of software development now takes place in IDEs, integrated development environments, combining an editor with other desirable features such as debugging.

00:13 The precise definition of IDE will vary from person to person, so here we will look across the spectrum of available options from an online browser-based solution through to heavyweight dedicated IDEs.

00:26 The most important element of IDE use with MicroPython is integration of the board into the IDE. What is desirable is a seamless integration of the board into the workflow—allowing creation, saving, and running of the code to be as easy as your normal Python operations.

00:44 In some cases, this provides a plug-and-play experience. In others, this will involve more setup. The exact set of features is dependent on what is available in the IDE, as you’ll see next. Micro:Bit Online.

01:01 The online Micro:Bit Python editor provides useful functions for the Micro:Bit. In addition to the Python editor, it also provides the ability to download the code as a .hex file, which can be dragged to the Micro:Bit for ingestion by it, and running on subsequent boots of the system. When used with a Chrome-based browser, it also offers the ability to connect to a REPL via WebUSB and flash code directly to the Micro:Bit, as you will see next.

01:30 As you can see, we’re at the Micro:Bit editor online. It’s possible to connect to the Micro:Bit just by clicking the Connect button. We pick the device—in this case, there’s only one option—and click Connect, and now we’re ready to either disconnect or—more importantly—flash the code.

01:52 Here, I’m going to change the already running default code, and then Flash that straight to the Micro:Bit.

02:04 And as you can see, straight away, it’s already running on the Micro:Bit, and that text is scrolling. Clicking Open Serial will allow us to open a REPL just by clicking in the window and sending Control + C. As you can see, the code has stopped executing on the Micro:Bit, and we now have a REPL, as we’ve seen previously.

02:30 help() gives us Micro:Bit-relevant information, and we can perform all the actions we would normally do in the REPL.

02:44 Once we’re finished, we can click Close Serial, and we can re-flash the project, as we did before.

02:53 There’s also useful code snippets, which can be really helpful if you’re finding some of the syntax just escapes your memory at the moment. So, there’s some boilerplate for creating classes, including—usefully—a reminder to create a docstring for any class that you create—something you should be doing anyway, but we all forget now and again.

03:15 Mu. Nicholas Tollervey’s Mu editor is presented as a beginner-friendly Python editor, but that belies the growing support that it has for hardware-based devices, such as Micro:Bit, ESP, and as you’ll see here, CircuitPython.

03:32 So here you can see Mu running in Linux, but it will look very much the same whether you’re running it on Windows, Linux, or macOS. And currently, as you can see at the bottom right of the screen, it is in Python mode, but that is soon changed easily. If you insert a new device which it supports, it will often also detect it.

03:52 As you can see here, it’s detected that a CircuitPython device has been inserted and asked if we want to change mode, so we will. However, if that doesn’t happen, you can click on Mode and then pick the appropriate mode from this screen here.

04:10 As you’ll see onscreen, the device is a CircuitPlayground Express, and it’s running some demo code which takes the NeoPixels through a variety of different colors in different modes. CircuitPython devices run code.py, which we can load easily in Mu.

04:28 The device is presented as a flash drive, and loading up code.py shows us the code that’s running. This particular code has four different modes, and if we change them to not be played, and then save, the device resets and reloads the code, so now it’s showing us just the flash_demo.

04:47 One function which may not be immediately apparent in Mu is how to do Save as, and that’s done by double clicking the name of the file in any open tab.

04:59 Here, I’m going to save this as neo2,

05:04 and that will now be saved on the CircuitPlayground Express for my use later on. Let’s reload code.py

05:16 and load up different code, which we’re going to paste in—so, code.py.

05:22 As you may see from the code onscreen, the CircuitPlayground Express has a number of other sensors, and we’re going to use the light and temperature sensors to output some values.

05:33 One quick and easy way to get this to run is to copy it from this tab into code.py. Paste it in, and then save it to get that running. As you can see, the NeoPixels are now off, and if we open up the Serial window, we’ll see that the CircuitPlayground Express is reporting the temperature and light readings to us as a printed-out tuple.

05:58 One really useful feature of Mu is that it has a plotter which works with these numbers so if you’re printing out tuples, it will graph them for you automatically.

06:08 All we need to do is open up the Plotter window, and now you can see those numbers are being plotted. One value represents the light, the other is the temperature.

06:21 If the light on the CircuitPlayground Express is turned down, you can see that the value of the light sensor drops. Turning it back up to maximum puts the value higher again.

06:34 Being able to quickly plot data without having to implement an API can make application development much quicker. In this case, it’s easy to see a spike in the light data, which probably has something to do with the frequency of the mains lighting that I’m using. Spikes like these can typically be removed by averaging the values over a few samples.

06:58 Thonny. As with Mu, Thonny is described as a Python IDE for beginners, and once again, this belies its usefulness with MicroPython. It has the ability to select the interpreter that’s being used with Micro:Bit, ESP, generic MicroPython, and CircuitPython all being available.

07:17 Let’s take a look at it running on macOS with an ESP32-based board. Here, you can see Thonny running on the desktop, and if you want to know more details about the editor, Real Python has a full course available. For MicroPython, we need to go to the Run menu and then pick Select Interpreter… Now, it’s the case of picking the appropriate interpreter type—in this case, ESP32and then picking the port if necessary. Here, M5Stack has already been selected.

07:51 Any other suitable ports will be available. Once you do this, you can hit OK, and hopefully, you should drop into a REPL. If you don’t, try pressing Control + C a couple of times, and generally, the REPL will appear.

08:08 We can now see that we’re running on the ESP32 from the information given in the REPL, and hitting help() gives us useful information, including sample code for a basic WiFi configuration and also Control commands.

08:22 The Files area gives us access to the files which are stored on the MicroPython device, and in the case of this M5Stick, there are apps which appear in the device’s menu. Opening the file up allows us to edit it, change things such as—let’s say—the color here,

08:44 and we can save directly back to the device. If we create a new file or use Save as…, in this case,

08:55 Thonny gives us the option of whether to save to the computer or the MicroPython device. Let’s choose the MicroPython device. It gives us a file selector, allowing us to pick the path.

09:06 I’m going to put this in the apps/ folder and give it a new name.

09:14 We have full file management, so I can delete this file if I want to. And note that Thonny tells you there is no recycle bin, so if you delete, this is gone.

09:26 Thonny’s IDE approach with built-in file management gives you a complete solution for developing on MicroPython devices that it supports. VS Code. Microsoft’s Visual Studio Code has become extremely popular in the last few years, and there’s a wide range of plugins available for it, including several for MicroPython.

09:49 Each will be different in their implementation, but next, you’ll see how to install one and get up and running in the shortest time. Here you can see we are in VS Code, and the first thing to do is to install the MicroPython extension.

10:03 So, extensions are summoned with Control or Command + Shift + X and then you can search for the extension you want. Here, searching for “micropython” leads us to see quite a few which are available.

10:17 The one we’re going to look at today is RT-Thread, so I’m going to click that. There is an English document available on GitHub, but I’m going to click Install, and now that’s ready to run.

10:30 We can close the EXTENSIONS sidebar with Control or Command + B and close the information about the extension. So, you may have noticed that we have two new controls here.

10:43 The first one is to create a new MicroPython project, and the second one is to connect to the device. So, first things first, let’s create a MicroPython project, and let’s create a new one. Let’s make it blank.

10:58 And let’s call it vscodemp. We’ll select the project location. In this case, I’ll make that inside the micropython/ folder that I’ve already created…

11:18 and now we’re ready to start coding. We have some boilerplate code here. We’re going to add some imports that we’ll need…

11:30 and then make our main program loop, which is going to be a straightforward LED flashing loop that will run indefinitely.

11:45 Now that the code’s complete, we can save it. And now we need to pick our MicroPython device, which is done by clicking the little plug icon. And you’ll be familiar with this, having seen this previously, so we pick the usbmodem and we can see we have a connection to our device. Close that window.

12:10 Now, running the program is as simple as right clicking and picking Run the MicroPython File Directly on the Device, or Alt + Q on the keyboard.

12:21 And there you can see our LED flash program is running, we see the printed feedback from it, and we can use this workflow to continue developing throughout the program’s lifecycle. We can stop the code running by clicking stop,

12:38 or we can run it again by clicking the play button. In the next section, you’ll see some examples of code interacting with a variety of different hardware for input and output.

Avatar image for Peter

Peter on Oct. 31, 2020

The RT-Thread extension for VSC looks very promising. However, connecting a Pyboard D-serie microcontroller (micropython v1.12) on a Raspberry PI 4, the device is not detected by the extension. Is there a solution or workaround known? Note: VSC on Raspberry PI is pretty new (September 2020).

Avatar image for seanorear

seanorear on Feb. 27, 2021

I also tried using VSC following this tutorial and could not connect to my pyboard D. The command being issued from the plugin gave this as an output:

PS /Users/sean/.vscode/extensions/rt-thread.rt-thread-micropython-1.0.7/ampy> ./cli_mac -p /dev/cu.usbmodem3164395C32382 repl                    
Traceback (most recent call last):
  File "cli.py", line 34, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "/Users/summer/Desktop/work/python3_venv_cli/venv/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
  File "site-packages/serial/tools/list_ports.py", line 29, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "/Users/summer/Desktop/work/python3_venv_cli/venv/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
  File "site-packages/serial/tools/list_ports_posix.py", line 31, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "/Users/summer/Desktop/work/python3_venv_cli/venv/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
  File "site-packages/serial/tools/list_ports_osx.py", line 32, in <module>
ValueError: dlsym(RTLD_DEFAULT, kIOMasterPortDefault): symbol not found
[47580] Failed to execute script cli

My system is:

PS /Users/sean> sw_vers                
ProductName:    macOS
ProductVersion: 11.2.1
BuildVersion:   20D74

Any tips would be very helpful, as this looks like a good MicroPython setup and I would really like to use it for my workflow.

Become a Member to join the conversation.