Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Computer – micro:bit Talk

Computer – micro:bit Talk

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. 

Recall from Using the Serial Monitor that you can flash a script to the micro:bit that prints a message,  and open the Serial monitor to see that message. 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.

Example script: print_to_terminal.py

  • Connect your micro:bit module to your computer with its USB cable.
  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Enter, name, and save print_to_terminal.
    (See Save & Edit Scripts.)  
  • Click Send to micro:bit.
    (See Flash Scripts with Python Editor.)
# print_to_terminal.py

from microbit import *

sleep(1000)

print("Hello Terminal!")
  • Click Show serial to open the serial monitor.
    (See Use the Serial Monitor.)
  • Verify that the Hello Terminal! message is displayed.

  • Want to see it again?  Click the 3 dots  ⋮  symbol to the right of Show/Hide serial, then click Send CTRL-D to reset.
  • Click Hide serial button to close the serial monitor.

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.

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.

  • Change project’s name from  print_to_terminal to print_to_terminal_try_this.
  • Replace the Hello Terminal! with your own new message.
  • Save the modified script.
  • Click the Send to micro:bit button.
  • Check the results in the serial monitor.

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 5×5 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.


Example script: 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.

  • Enter, name, and save print_value_to_terminal.  
  • Click the Send to micro:bit button.
# print_value_to_terminal.py

from microbit import *

sleep(1000)

print("Hello Terminal!")

value = 10

print("value = ", value)
  • Check the results in the serial monitor.
  • 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.

Example script: print_looped_terminal_values.py

  • Enter, name, and save print_looped_terminal_values.  
  • Click the Send to micro:bit button.
# 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!")
  • If the serial monitor isn’t already open, click Show serial.
  • Click Send CTRL-D to reset.
  • Verify that the terminal displays value = 5, value = 4, …value = 1, Blastoff!

 


Printer-friendly version
Interactive Scripting with REPL
Prev
Input Messages
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress