The MicroPython interactive prompt “REPL” (as in Read-Evaluate-Print-Loop) is the easiest way to test your code and run commands: the code you type in the terminal is evaluated when you hit enter, and the result is displayed right away.
However, there is no persistance and everything you type is lost after a reset (soft reset or hard reset/power cycle).
You can use the interactive prompt to quickly test some commands, debug code and see what kind of object a specific function or line of code returns.
Remember to switch to paste mode using Ctrl+E before pasting multi-line sections of code into the REPL, otherwise the indentations will not be entered correctly, and the REPL will report an indentation error.
After pasting the code, hit Ctrl+D to run the code and switch back to normal REPL mode.
The interactive prompt REPL can be accessed with any terminal program, such as PuTTY, by opening the board’s USB serial port, which is automatically created when the board is connected to a PC:
On your computer open Control Panel > Device Manager and scroll down to Ports (COM & LPT).
With the Device Manager still open, connect the board to your computer with the USB cable and you’ll see the new COM port appear - remember this COM port number.
Open PuTTY, select Serial, insert the COM port corresponding to the board and then click the Open button.
(Speed is not relevant in this context of a USB virtual COM port and can be left to any value).
In the terminal window you’ll see printed the micropython version and compilation date first, followed by the board name and microcontroller type.
The characters >>> are the REPL prompt: try inputting some code like 1+1 or print("Helloworld!") and hit the Enter key!
Useful commands in REPL:
prints information about the board
gives information on a specific Micropython object
autocompletes commands as you type them
on an empty REPL prompt lists all the valid commands and objects currently in RAM
So let’s try this out! Connect the board to your computer via the USB cable. The board’s flash storage will show up as a normal USB drive named PYBFLASH.
(If File Explorer doesn’t pop up automatically, open it manually and you’ll see PYBFLASH under your computer’s drives).
Open main.py with Notepad++ (right mouse click -> Edit with Notepad++).
Copy paste the following code into Notepad++:
# main.py -- put your code here!importpyb# print a header lineprint('#### Test ####')# infinite loopwhileTrue:print('Hello world!')# wait for one second (1000 milliseconds)pyb.delay(1000)
Save the file (click save button on Notepad++ or hit Ctrl+S).
An LED on the board will briefly light up while the data is being written to the flash.
Now your code is saved on the board and will be run automatically as soon as you reset the board.
Go back to the interactive prompt REPL on Putty and perform a soft reset by hitting Ctrl+D.
As you see, the code you entered in main.py is executed automatically and the output is desplayed in the interactive prompt.
You can interrupt the execution of the program and go back to the REPL prompt by hitting Ctrl+C in Putty.
REPL keyboard shortcuts summary:
stop a running program, for instance the main.py script and go back to the REPL
force a soft reboot, for instance to run main.py after modifying it
switch to paste mode, to paste multiple lines of code without indentation errors
in paste mode, execute the code entered and go back to normal REPL mode