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

Unlock This Lesson

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

Unlock This Lesson

REPL - Workflow

00:00 MicroPython Workflow. If you’re familiar with Arduino or PIC programming, then you might expect to talk first about compilers, development environments, and the toolchain. However, MicroPython is a little different at first glance.

00:15 There are three options that you’re going to take a look at here. Firstly, a direct connection to the MicroPython board and using the REPL. Secondly, using file transfer to put your code onto the board to run it.

00:30 Thirdly, integration with an IDE. The IDE will connect directly to the board, allowing you to run a REPL on it in many cases, and also allowing you to access the file storage which the board offers.

00:43 REPL. With MicroPython, the language may have come pre-installed on your hardware, and you may have the option of working with it interactively. This harks back to the golden era of home computers, such as the Commodore Vic 20 and 64, Sinclair’s ZX81 and Spectrum, and the BBC Micro, where the initial access to the computer is via a language.

01:04 That would have been BASIC in the case of these 1980s machines, and the MicroPython REPL for the boards we will look at here. Accessing the REPL will typically be performed using a serial connection over USB.

01:17 How this is achieved in practice depends on the operating system, so you’re going to see how to do it on macOS, on Linux, and also on Windows. Firstly, using macOS. Once the board is connected, finding the device’s serial port is necessary.

01:34 This is done in a terminal using the following command, and while there are two possible candidates, the one that’s wanted is the one on the right. Next, using the screen command to connect to the board is as simple as issuing this command seen here.

01:51 It’s very similar in Linux, the only difference being the name of the serial port. As you can see here, there’s only one candidate, ttyACM0, and the screen command is modified to use that port.

02:06 Now let’s see that in action on macOS.

02:11 So here, you can see the macOS desktop and at the top right, the Pyboard’s flash drive has been mounted. We want to connect to it via serial, so the first step is to find out which serial port the Pyboard has presented itself as. Opening up a new terminal… and listing the possible devices…

02:34 shows two. The Bluetooth port is part of the Mac’s hardware, but the USB modem is the Pyboard, so we’re now going to use screen to connect a virtual terminal to it using this serial port…

02:49 and specifying the speed at the end.

02:53 We’re now connected to the Pyboard. To give it a quick run through its paces, importing the pyb library will allow access to the onboard LEDs.

03:06 There are four of them, of different colors…

03:15 and they can easily be turned on or off using .on(), .off(), or the .toggle() command.

03:26 screen has a useful feature in that you can detach from it but leave it running. You do this with Control + A and then tapping D to detach, and we’re back in the macOS terminal.

03:39 We can reconnect using screen -r,

03:45 and as you can see, we’re back in the terminal as we left it. To kill that screen session, do so with Control + A and then K for kill, and confirm the prompt at the bottom.

04:01 Accessing the REPL via USB serial in Windows is slightly different, but we still have to find the device serial port, and that is done in Device Manager. Here, you can see the USB Serial Device is presenting as COM4 to the system.

04:17 Secondly, connecting to the REPL is done using third-party software, such as PuTTY. Here, the PuTTY settings have been specified to suit the COM port which we previously saw, and now you’re going to see that in action on Windows.

04:33 Here we are on the Windows desktop, and the first task is to find the serial port. On Windows, you can do that by holding down the Windows key and tapping X, and then selecting Device Manager. Once Device Manager appears, you can then go down to Ports and expand that, and if you’ve installed the driver, you should see a USB Serial Port. If not, you’ll need to install the driver before going any further.

04:59 As you can see, here on this machine we’re on COM6, so that’s the information we need. And now we’re going to run PuTTY. And to get PuTTY to connect, it’s as easy as picking Serial, changing to the appropriate COM port, and then entering the Speed. In this case, 115200, and clicking Open.

05:24 Here, we get a black screen, and we need to get the M5’s attention, which we typically do by pressing Control + C a couple of times—that stops the running program, and drops us into the REPL.

05:36 Now we can spend a bit of time looking at some of the features which the M5Stick gives us. First things first, from m5stack we’re going to import the lcd module. This gives access to the LCD screen, which we can clear using the lcd.clear() command.

05:54 Next, it’s our duty to print "Hello World!", so let’s start out using the lcd.print() command with the string "Hello", followed by two coordinates which set the top left of the text on the screen.

06:09 Here, 0, 0 is the top left. And the next line is going to be printed in much the same way, but down by 16 pixels.

06:21 The foreground color can be set with the following command. It takes a 24-bit hexadecimal number in the format red, green, and blue, with two digits for each. Here, we will set maximum red, and no green or blue.

06:38 We can do a similar thing for the background color for our print() command, here setting it to a dark gray color.

06:47 And now repeating the "World!" line, we’ll reprint that text with a new foreground and background color. There’s plenty more to explore with the M5Stick, but that’s a quick example of how to use it via the REPL from Windows.

07:02 Next, you’ll see how you can use command line tools to further your interaction with these devices.

Become a Member to join the conversation.