2. Time and patterns
You will be able to
- pause a program with
utime.sleepandutime.sleep_ms, - measure how long something took with
ticks_msandticks_diff, - build a repeated pattern without copying and pasting.
You need: the LED from lesson 1. Problems: Double blink, Running lights, Countdown.
The idea
A program runs one line at a time, very fast. If you turn an LED on and off with no pause, it happens thousands of times per second — faster than your eye can see, so it looks dim.
A sleep gives time a chance to pass:

The pattern “on 0.5 s, off 0.5 s” is one cycle per second, or 1 Hz.
The exact tools
| What | MicroPython | Notes |
|---|---|---|
| Sleep in seconds | utime.sleep(0.5) | 0.5 is half a second |
| Sleep in milliseconds | utime.sleep_ms(500) | 500 ms = 0.5 s |
| A millisecond clock | utime.ticks_ms() | wraps around; use it with ticks_diff |
| Difference between two ticks | utime.ticks_diff(now, started) | safe across the wrap |
time or utime? | They are the same module on the Pico | Either import works |
Careful.
utime.sleep(500)is 500 seconds, not 500 milliseconds. If your LED takes eight minutes to blink, this is why.
Build it
from machine import Pin
import utime
led = Pin(13, Pin.OUT)
for _ in range(4):
led.on()
utime.sleep_ms(200)
led.off()
utime.sleep_ms(200)
Four short flashes, then stop. Now make it repeat forever:
while True:
led.toggle()
utime.sleep(0.5)
Fun fact. “Hz” means per second. A 1 Hz blink is once per second; a 2 Hz blink is twice. Your computer screen is around 60 Hz — it draws 60 pictures every second.
What you should see
The first program flashes four times and ends. The second blinks until you press Stop.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| The blink is far too slow | sleep(500) instead of sleep(0.5) | Seconds, not milliseconds |
| The app seems frozen | while True: with no sleep inside | Add utime.sleep_ms() in every loop |
| The LED looks dim | No sleep between on and off | Give each state a visible pause |
| The pattern drifts | Adding work inside the loop | Use ticks_ms to schedule, or keep the work small |
Check yourself
- How long is
utime.sleep(2)? - What is one cycle of a 2 Hz blink, in seconds?
- Why does
while True: passstop the board responding?
Answers: two seconds · 0.5 s · it never sleeps, so the program never gives anything else a turn.
Remember
sleepis in seconds,sleep_msin milliseconds.- A cycle is one full on+off; Hz is cycles per second.
- Every
while Trueloop needs a sleep.
Go further
- Make two flashes then a long pause — Double blink.
- Move a light along three LEDs — Running lights.
- Blink five times, then stay on — Countdown.
Next: Lesson 3 — Read a pin