Learn electronics, coding, and projects — step by step.

Micropython Basics: Using the REPL (Interactive Mode)

Table of Contents

Before diving deeper into coding projects, it’s important to understand one of the most useful tools in MicroPython: the REPL, which stands for Read–Eval–Print Loop. It’s an interactive shell that allows you to execute Python commands directly on your microcontroller in real time.

The REPL is what makes MicroPython feel so dynamic and beginner-friendly — you can type commands, test hardware responses, and debug code instantly without uploading a full script every time.

What Is the REPL?

The REPL acts like a live Python console that runs on your microcontroller. Each command you enter is:

  1. Read: The REPL reads the line of code you type.
  2. Eval: It evaluates or executes that command on the board.
  3. Print: The result (if any) is printed back to your screen.
  4. Loop: The process repeats, waiting for your next command.

This continuous feedback loop makes the REPL perfect for experimenting, testing pin outputs, reading sensor values, and quickly learning how the board responds to your code.

💡 Example:
Try typing the following directly into Thonny’s Shell (not the editor):
print("Hello from the REPL!")
The REPL will immediately respond with:
Hello from the REPL!

Accessing the REPL in Thonny

Thonny automatically opens the REPL console whenever you connect your MicroPython board. It appears at the bottom of the IDE, labeled as the Shell. This is your live connection to the microcontroller.

If your board is properly connected, you should see a prompt that looks like:

>>>

This is the REPL prompt — it means your device is ready to receive commands.

For example, you can type:


>>> 2 + 3
5
>>> name = "ESP32"
>>> print("Hello,", name)
Hello, ESP32

Each line executes immediately. No saving, no uploading — just direct interaction.

⚙️ Tip: If you don’t see the REPL prompt (>>>), click Stop/Restart backend in Thonny or press Ctrl + D to restart the MicroPython shell.

Using the REPL for Hardware Interaction

One of the REPL’s biggest advantages is that you can control your hardware interactively. For instance, you can toggle GPIO pins or read sensor values directly from the console.

Let’s say you’ve connected an LED to GPIO2. You can control it like this:


>>> from machine import Pin
>>> led = Pin(2, Pin.OUT)
>>> led.value(1)  # Turn on the LED
>>> led.value(0)  # Turn off the LED

The LED will respond instantly as you enter each command — no need to upload or rerun a script.

Similarly, if you have a button connected to an input pin:


>>> button = Pin(12, Pin.IN)
>>> button.value()
0

You can press the button and retype button.value() to see its state change between 0 (not pressed) and 1 (pressed).

Special REPL Commands

The MicroPython REPL supports a few special control key shortcuts to make it more powerful.

  • Ctrl + C — Interrupt the current running program.
  • Ctrl + D — Soft reboot the board (restarts the interpreter).
  • Ctrl + E — Enter “paste mode” (useful for pasting multiple lines of code).
  • Ctrl + A — Move cursor to start of line.
  • Ctrl + E again — Exit paste mode and execute code.

Paste mode is especially handy when you want to test a few lines of code quickly without creating a file.

✅ Tip: When pasting multi-line scripts in the REPL, use Ctrl + E to enter paste mode, then Ctrl + D to execute. This ensures proper formatting and prevents syntax errors.

Exploring Your Device with the REPL

The REPL isn’t just for running simple code — it’s also a powerful tool for exploring your device’s environment.

Try typing:


>>> help('modules')

This lists all built-in modules available on your MicroPython firmware — such as machine, network, os, time, and more.

You can also check memory usage or file contents:


>>> import os
>>> os.listdir()      # Show files saved on device
>>> os.getcwd()       # Show current directory
>>> import gc
>>> gc.mem_free()     # Check free memory

These commands help you understand what’s happening under the hood of your microcontroller, which is especially useful for debugging or optimizing your programs.

Why the REPL Is So Important

The REPL provides a live development experience that traditional microcontroller platforms, like Arduino, can’t easily match. It bridges the gap between software and hardware experimentation by letting you:

  • Test individual lines of code in real time.
  • Prototype ideas before writing a full program.
  • Debug issues interactively, without constant re-uploading.
  • Monitor sensor data or pin states continuously.

In short, the REPL turns your development board into an interactive playground for learning, testing, and experimenting with MicroPython — instantly.

🚀 Pro Tip: Keep your REPL open while developing — you can use it to test snippets, confirm sensor readings, or even run diagnostics without leaving Thonny. It’s your best friend when learning MicroPython!
×



Related Articles: (by Series)