Built-in project · Raspberry Pi Pico

MicroPython serial print on a Pico: seconds since start

MicroPython on a Raspberry Pi Pico: print() sends the seconds since the board started to the Serial Monitor once a second, over USB. The whole build, a Raspberry Pi Pico, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $4. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sending text to the Serial Monitor with print
  • Measuring elapsed time with ticks_ms and ticks_diff
  • Integer division to turn milliseconds into seconds
  • Pausing with time.sleep
  • Difficulty: Beginner
  • Time: About 5 minutes
  • Board: Raspberry Pi Pico
  • Parts: 1

How it works

Nothing is wired. The script prints a start message, then notes the time with time.ticks_ms() and loops forever.

Each pass uses time.ticks_diff to get the milliseconds since the start, divides by 1000 for whole seconds, prints it, and sleeps one second. The text goes out over the USB cable to the Serial Monitor.

Parts list

1 part, plus the jumper wires. They retail for about $4; on Mokxi they're free.

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# Serial print: the seconds since the board started, once a second, in
# the Serial Monitor. print() goes out over the USB cable.
import time

print("The Pico is running")
start = time.ticks_ms()

while True:
    seconds = time.ticks_diff(time.ticks_ms(), start) // 1000
    print("running for", seconds, "s")
    time.sleep(1)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing shows up in the Serial Monitor This project
    Run the program and open the Serial Monitor. Output only appears there while the program is running.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.