The REPL (Read-Eval-Print Loop) is a fundamental tool in MicroPython that allows us to interact directly with the board in real time.
In our first program already (previous tutorial) we saw how the REPL took the program sent by the IDE and executed it with the interpreter that is in the MicroPython firmware.
The REPL is an interactive environment that allows you to execute MicroPython commands directly on the board.
Basically, it allows us to communicate with the Python interpreter we have on the device, from outside of it, through serial communication.
Its name comes from the acronym Read-Eval-Print Loop, which describes its functioning:
- Read (Leer): The REPL reads the command you enter.
- Eval (Evaluar): It evaluates the command and executes it.
- Print (Imprimir): It shows the result of the execution.
- Loop (Bucle): It repeats the process, allowing you to enter new commands.
The REPL is not exclusive to MicroPython. It is a concept that exists in other languages and technologies. They are useful for running quick tests, debugging code, and exploring functionalities without the need to load complete programs.
In this article, we will see how it works, its main features, and how to make the most of it in project development with MicroPython.
Accessing the REPL in MicroPython
To access the REPL, you simply need to connect your device to your computer via a USB cable (that’s how easy it is!).
Then, you can use a terminal tool like Arduino Lab for MicroPython, Thonny, PuTTY (Windows only) to establish communication with the board.
In Arduino Lab for MicroPython, the REPL is always present, occupying the bottom part of the program.
Similarly, in Thonny we also find the REPL at the bottom of the program.
If you want to use PuTTY, simply select the serial port corresponding to your board. Set the baud rate to 115200 and press Open to view the REPL.
Testing the REPL
Let’s test the REPL simply. In the IDE you have chosen, type the following in the REPL.
print("Hello from LuisLlamas.es")
You will see that, very similar to what happens with a Python PC interpreter, MicroPython responds through the REPL,
>>> print("Hello from LuisLlamas.es")
Hello from LuisLlamas.es
But you can try more things! For example
# Sum of two numbers
a = 10
b = 5
sum = a + b
print("The sum is:", sum)
# Read a text variable from user input
name = input("Enter your name: ")
print("Hello,", name)
And it will respond
>>> a = 10
>>> b = 5
>>> sum = a + b
>>> print("The sum is:", sum)
The sum is: 15
>>> name = input("Enter your name: ")
Enter your name: Luis
>>> print("Hello", name)
Hello Luis
Isn’t it cool? Well, it’s just a small part of what we can do.
Advanced Features of the REPL
The REPL in MicroPython supports command autocompletion. This is especially useful when you are exploring modules or functions. To use it, type part of a command and press Tab
. For example:
>>> import machine
>>> machine.Pin. #<-- here you press Tab
This will display a list of methods and attributes available in the Pin
class.
You can navigate through the command history using the ↑
and ↓
arrow keys. This allows you to reuse previous commands without having to retype them.
The REPL allows you to edit lines of code before executing them. You can move the cursor with the ←
and →
arrows, and delete characters with Backspace
or Delete
.
Although the REPL is designed for single-line commands, you can execute more complex code blocks using indentation. For example:
>>> for i in range(5):
... print(i)
...
0
1
2
3
4