Scripts that Print to Terminal


As you’ve now seen, print function calls can be entered at the REPL prompt to display messages and values.  MicroPython scripts can also use print to display messages and values as the scripts run.  This can be helpful for displaying longer messages, and also for displaying what’s happening to values at certain points in your script.  And, since you flash the script to the micro:bit, your code travels with your device.

In this activity, you will print messages, values, and combinations of both in the terminal.

Make Sure Online Editor Is Connected

The Online Editor has to be connected to the micro:bit before it can display text messages.  If the second button from the left displays “Disconnect”, it means that the micro:bit is connected.  If it displays “Connect”, it means the micro:bit is not connected and you need to connect it.

  • Connect your micro:bit module to your computer with a USB cable.
  • Use your Chrome browser to navigate to python.microbit.org. (This link is to version 2 to match the screencaptures here).
  • Check the Connect/Disconnect button and make sure the label says Disconnect.
  • If the label says Connect, click the button to connect.   

  • If you physically disconnected the USB cable, when you reconnect, a “python-editor… wants to connect” dialog might appear.  If it does, select your micro:bit from the list, then click the Connect button in the dialog.

Now that the online editor is connected, you can run a script that prints a message and open the terminal to see that message.  With this connection, you can also flash scripts into the micro:bit by simply clicking the online editor’s Flash button.  (No more switching to a file browser and dragging files into the micro:bit drive!)

  • Enter this script:
# print_to_terminal.py

from microbit import *

sleep(1000)

print("Hello Terminal!")
  • Click Flash to load the script into the micro:bit.
  • Click the Open Serial button to open the terminal.
  • Verify that the Hello Terminal! message is displayed.
  • Want to see it again?  Click the Send CTRL-D to reset button.
  • Click the Close Serial button to get back to the script editor pane.


Inside the Terminal Display

After clicking “Send CTRL-D to reset”, the first message the terminal displays is soft reboot.  That’s letting you know that the terminal is doing the equivalent of pressing and releasing the micro:bit module’s Reset button, (which is next to its USB connector).  After that, the micro:bit runs the script and whenever it executes a print statement, the result appears in the terminal.  In this case, it’s the Hello Terminal! message.  

If the script ends by running out of statements, the terminal then displays technical information about the micro:bit, followed by a prompt to type help() for more info, and ends with the REPL prompt.  Whenever “CTRL-D to reset” is clicked, the micro:bit will restart the script and display these lines again.


To reset the micro:bit,  you can also click the black Terminal pane and press CTRL+D (CMD+D in Mac). 
To exit a running script and go straight to the REPL prompt, you can also click the “Send CTRL-C for REPL.”  

Print Text - Mu

In Mu, there’s a REPL button that opens a terminal pane and puts the micro:bit into REPL mode.  To run a script and see its print statements, simply click the terminal pane and then press CTRL + D.  You can also press and release the micro:bit module’s Reset button.  It’s the button right next to the micro:bit’s USB socket.  

  • Connect your micro:bit module to your computer via USB cable.
  • Open Mu.
  • Enter, save, and flash this script:
# print_to_terminal.py

from microbit import *

sleep(1000)

print("Hello Terminal!")
  • Click the REPL button.
  • Click next to the REPL prompt in the terminal pane that opens.
  • Press/release  the reset button on the micro:bit
    • - or -
  • Click next to the REPL prompt and then press CTRL + D on your keyboard (CMD + D for Mac).



You should see Hello Terminal! appear in the REPL terminal.  To learn more about the other messages, read the section titled Inside the Terminal Display earlier on this page.


Mum on Mu — This is the last time that the Cybersecurity lessons will refer to the Mu application, however Mu’s REPL is able to handle all of these lessons in the same way that the online editor’s terminal does.

Try This: Print Something Different

What if you want to display a different message in the online editor?  Try modifying the text in between the parentheses and quotation marks after the print command.

  • In print_to_terminal.py, replace the Hello Terminal! with your own new message.
  • Save and re-flash the program to the micro:bit.
  • Verify the results in the terminal.

Did You Know?

The micro:bit’s print function is completely different from the display object and its methods. One of those outputs is not as fast as the other.  The display methods are for the 5x5 LED grid, and each call has to display each character long enough for a human to read it.    In contrast, the micro:bit print function can send characters very rapidly, with each character taking only about 1/10,000th of a second to transmit.


Display Integer Values

You might have noticed that print can display values stored by variables, along with strings of characters.  That same functionality is also available when you add print calls to scripts.

  • If your editor is still displaying the terminal, click the Close Serial button to get back to the code editing pane.
  • Enter, save, and flash this script:
# print_value_to_terminal.py

from microbit import *

sleep(1000)

print("Hello Terminal!")

value = 10

print("value = ", value)
  • Click the Open Serial button.
  • Verify that the terminal displays Hello Terminal followed by value = 10 on the next line.


Print Values to Terminal in a Loop

Let’s try a countdown timer in the terminal using a while loop.  The while statement repeatedly executes statements that are indented immediately below it. The loop repeats “while” its condition is true. In this program, the while loop repeats while the value variable is greater than or equal to 0.

  • Select the Close Serial option in the editor.
  • Enter, save, and flash this script:
# print_looped_terminal_values.py

from microbit import *

sleep(1000)

print("Hello Terminal!")

value = 5

while value > 0:
    print("value = ", value)
    sleep(1000)
    value = value - 1

print("Blastoff!")
  • Click the Open Serial button.
  • Click the “Send CTRL-D to reset” button.
  • Verify that the terminal displays value = 5, value = 4, ...value = 1, Blastoff!
  • Click the Close Serial button.