Labs / MicroPython tutorials

03 REPL Interactive Interpreter —— Draft Document Tutorial

Author: Li Zisheng · Source: FreakStudio open documentation, v1.1.0 · License: CC BY 4.0

Contents

1. Basic Concepts of the REPL Interactive Interpreter

After we finish flashing the firmware to our Raspberry Pi Pico, if we use a remote terminal to connect the computer to the Raspberry Pi Pico via serial port, USB or other methods at this point, we will find that some information is output in the terminal:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Connecting to a MicroPython device with mpremote

This is because after the firmware of the Raspberry Pi Pico is burned, the built-in MicroPython will run in the REPL interactive interpreter mode, which is similar to the terminal of Windows system or the shell of Unix/Linux. We can enter commands in the terminal and receive responses from the system.

Interactivity means that you can get immediate feedback on code execution. In an editor like PyCharm, after you finish writing a piece of code, you have to run it to see how it works; if you make a small modification, you have to run it again to check the result. In an interactive interpreter, by contrast, it’s like a program that’s always running — you can make changes at any time and get feedback right away.

As shown in the figure, by entering the following commands, we can turn on the onboard LED of the Raspberry Pi Pico, turn off the onboard LED, and output messages in the terminal:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Controlling LED hardware in the REPL

REPL is the abbreviation for Read-Evaluate-Print Loop. Many programming languages come with a REPL, which acts like a miniature Shell that facilitates interaction between the interpreter (core) and commands. It allows users to conveniently input various commands to be executed by MicroPython's internal interpreter and observe the running status, thus playing a very significant role in program debugging.

Through MicroPython 's REPL interactive environment, we can accomplish the following tasks:

However, it should be noted that once the code in the REPL has been executed, it cannot be rolled back or undone. Moreover, when you close the REPL environment, all variables, functions, etc. defined in the REPL will be lost, and a brand new session will be started the next time you launch the REPL.

1.1 Workflow of REPL

The workflow of REPL is illustrated in the following figure:

Diagram: 03 REPL Interactive Interpreter —— Draft Document Tutorial

1.2 Keyboard Shortcuts for REPL

shortcut key

Features

Ctrl-C

Stop the running program or terminate the current command line, and return to the REPL

Ctrl-D

Implement a soft reset when the command line is empty.

Ctrl-B

When the command line is empty, the system prompts are displayed: version number, and REPL operation instructions

Ctrl-E

When the command line is empty, enter paste mode. You can press Ctrl-C to exit paste mode, and Ctrl+ D to complete pasting.

Tab

command auto-completion

Up and down keys

Recall previously entered commands. MicroPython can save the last 6 entered commands.

Left and right arrow keys

Move and edit commands in the current command line

Except for Ctrl-C, all other shortcut keys only take effect when the command line is empty (i. e., no characters have been entered yet).

2. Connect the computer to the Raspberry Pi Pico

2.1 Connect via USB

We can use multiple remote control terminals to connect and communicate with the Raspberry Pi Pico via USB cables, and common remote control terminals include MobaXterm, Putty, mpremote, Xshell, etc.

The mpremote command-line tool provides a set of integrated utility functions that enable remote interaction and automation with MicroPython devices via serial connection. We can install mpremote via pip in the command line after downloading and installing Python to use it:

pip install mpremote

The simplest way to use this tool is to call it without any parameters, which can be done by directly entering the following command in the command line:

mpremote
03 REPL Interactive Interpreter —— Draft Document Tutorial
Connecting to the REPL of a MicroPython device with mpremote

This command automatically detects and connects to the first available serial port device and provides an interactive REPL. Serial ports are opened in exclusive mode, so when you run a second (or third, etc.) instance, mpremote will connect to subsequent serial devices if any are available.

For more information on how to use mpremote, please refer to the link:

MicroPython Remote Control: mpremote

https://listenai.github.io/MicroPythonDocCN/reference/mpremote.html

2.1.2 Connect Using the PuTTY Tool

PuTTY is a remote login tool that serves as a Telnet, SSH and RLOGIN Client for Windows and Unix platforms. It supports cross-platform usage and can run on systems including Windows, Linux and macOS; it is a lightweight program that can be run as a standalone executable without installation; it supports multiple protocols including SSH, Telnet, Rlogin and serial port, and can be used for remote login and management of remote servers, network devices and routers.

For the download and installation of the Putty software, you can refer to the following link:

[Tool] Tutorial on Downloading, Installing, Using PuTTY and Common Settings

https://blog.csdn.net/crr411422/article/details/131135853

When connecting to a port using PuTTY, we need to make the following settings:

  1. First, open Connection-Serial to configure the parameters for serial port connection:
03 REPL Interactive Interpreter —— Draft Document Tutorial
PuTTY serial port configuration
  1. Set the Port Number according to the Port Number displayed in the device manager. For parameters such as the baud rate, stop bits, data bits, and parity bits of the serial port connection, you can select them based on the attribute values of the port. Finally, disable the serial port flow control:
03 REPL Interactive Interpreter —— Draft Document Tutorial
PuTTY serial port parameter configuration
03 REPL Interactive Interpreter —— Draft Document Tutorial
Verifying system serial port properties
  1. Return to the Session window, select serial port serial in Connection type, and click Open
03 REPL Interactive Interpreter —— Draft Document Tutorial
PuTTY session save configuration
03 REPL Interactive Interpreter —— Draft Document Tutorial
Opening a PuTTY session
  1. When the following window appears, just press the Enter key. Here, enter the help () command for testing:
03 REPL Interactive Interpreter —— Draft Document Tutorial
MicroPython REPL help screen in PuTTY

2.1.3 Connect Using the MobaXterm Tool

MobaXterm, also known as MobaXVT, is an enhanced terminal, X server and Unix command set (GNU/Cygwin) toolbox. It features the following characteristics:

You can refer to the following link for the download and installation of MobaXterm:

MobaXterm (Terminal Tool): Download, Installation and Usage Tutorial

https://blog.csdn.net/qq_44074697/article/details/118544904

Open MobaXterm, click "Session" in the upper left corner, select "Serial" in the pop-up interface, select the Port Number (COM3, the Port Number displayed in the previous Device Manager), baud rate (Speed 9600), and flow control (Flow Control: none) as shown in the following figure, and finally click "OK".

03 REPL Interactive Interpreter —— Draft Document Tutorial
MobaXterm serial session configuration entry
03 REPL Interactive Interpreter —— Draft Document Tutorial
MobaXterm serial port parameter configuration
03 REPL Interactive Interpreter —— Draft Document Tutorial
MicroPython REPL interaction in MobaXterm

2.2 Connect via Serial Port

By default, RP2040 MicroPython port does not expose UART port REPL.

However, you can modify the values of the following macros in the source code file: micropython/ports/rp2/mpconfigport.h to connect to the Raspberry Pi Pico via the serial port:

#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB
03 REPL Interactive Interpreter —— Draft Document Tutorial
MicroPython source serial REPL configuration

Then recompile the source code and flash the generated firmware to the Raspberry Pi Pico. After that, the Raspberry Pi Pico can be connected to the computer via the serial port.

By default, UART 0 is used to connect to the computer, and the specific parameters are as follows:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Raspberry Pi Pico UART pin definitions

3.1.1 Help Interface

Using the help () function, you can bring up the basic help interface, which contains information including Quick Start, Pin Definitions, Shortcut Keys, etc.

03 REPL Interactive Interpreter —— Draft Document Tutorial
MicroPython REPL help screen

You can view more detailed help by using help (module_name/function_name), for example, to view the detailed help of the machine module:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Detailed help for the machine module

You can also use the help () function to view the help information of the internal modules of machine:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Help for the machine.Pin class
03 REPL Interactive Interpreter —— Draft Document Tutorial
Raspberry Pi Pico pin definition help

By using the help function, we can check the methods and attributes that most modules have.

3.1.2 View the properties and methods contained in the module

Use dir () function to check what variables and functions are inside a module:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Pin and module queries in the MicroPython REPL

You can also check what variables and functions are inside these modules:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Module and object queries in the MicroPython REPL

3.1.3 Pasting Long Blocks of Code

When pasting large blocks of code via the REPL of MicroPython, the auto-indentation function will cause the code to fail to be recognized correctly. In this case, we can use the shortcut key Ctrl+ E to enter paste mode, which disables the auto-indentation function, and the prompt will change from >>> to ===:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Entering multi-line code in MicroPython REPL paste mode

We can paste the following code to check the running status:

def foo():
    print('This is a test to show paste mode')
    print('Here is a second line')
foo()
03 REPL Interactive Interpreter —— Draft Document Tutorial
MicroPython REPL paste mode

Press the Ctrl+ D shortcut key to exit paste mode, and you will see that the REPL has executed the code we entered:

03 REPL Interactive Interpreter —— Draft Document Tutorial
Code execution result in MicroPython REPL paste mode

3.2 Terminal Print Output

We can enter the following code in the REPL to output the string "Hello world" using the Print function:

print("Hello World!")
03 REPL Interactive Interpreter —— Draft Document Tutorial
Running Hello World in the MicroPython REPL

3.3 Driving an LED

The LED on the Raspberry Pi Pico is connected to GPIO pin 25, and we can enter the following code to control the turning off and on of the LED:

from machine import Pin
led = Pin("LED", Pin.OUT)
led.value(1)
led.value(0) 
03 REPL Interactive Interpreter —— Draft Document Tutorial
Controlling the Raspberry Pi Pico LED with mpremote

References

Version Record

Version Number

Modifier

Time

Content

v1.0.0

Li Zisheng

2025/09/16

Document initialized, based on MicroPython version V1.23.0.

v1.1.0

Li Zisheng

2025/12/05

Figure captions have been added.