C122 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 2 · Blinking an LED in MicroPython

Pins, sleeps, and keeping time

Blinking an LED is the first program on any new board, because it proves the toolchain, the pin and the timing all work. In MicroPython it takes five lines. But even five lines hide a timing question: a loop that toggles a pin and then sleeps for 250 ms runs a little slower than 250 ms per step, because the toggle itself takes time. Over a minute the error adds up, and a clock or a data logger built that way drifts. The fix is to sleep until a deadline instead of for a fixed time, and it's the first of many places where embedded code has to think about where time goes.

  • Two LEDs share the same intended half-period, 250 ms. P1 toggles from a timer: the ideal grid. P0 toggles from a loop that does some work, then sleeps a fixed 250 ms.
  • The work is work calls into the hardware (reading a pin), about 40 µs each. With 100 calls, every pass of the loop is 4 ms longer than it should be.
  • Run it and compare the last edges: P0 has fallen behind P1, and the gap grows every pass. Set work = 0 and run again; the gap nearly vanishes, but not quite, because the toggle itself is a call.

Predict

A loop toggles an LED, does 2 ms of other work, and sleeps 250 ms. After 100 toggles, how far behind the ideal 250 ms grid is it?

Name it

Pins in MicroPython

from machine import Pin
led = Pin(0, Pin.OUT)       # direction: output
led.on(); led.off()         # level high or low
led.value(1); led.value()   # write, or read back
led.toggle()                # flip it

Each call becomes a read or write of the GPIO registers from lesson 1. On this board each call into the hardware costs about 40 µs, MicroPython's overhead at 10 MHz.

Blink timing

An LED on for ton and off for toff blinks with

T=ton+toff,f=1T,D=tonT

where D is the duty cycle.

Drift, and the deadline pattern

If each pass of a loop takes the sleep plus some work tw, the period stretches by tw every pass and the error grows without limit: after N passes the loop is Ntw behind. Sleeping until a deadline fixes it:

next = time.ticks_ms()
while True:
    led.toggle()
    next = time.ticks_add(next, 250)
    time.sleep_ms(time.ticks_diff(next, time.ticks_ms()))

ticks_ms() counts milliseconds and wraps around; ticks_add and ticks_diff do the arithmetic correctly across the wrap.

Worked example

An LED should blink at 2 Hz with a 20% duty cycle. How long is it on and off?

Show the solution
  1. Period. T=12Hz=500ms
  2. On time. 0.2×500ms=100ms
  3. Off time. The rest of the period is 400 ms.

Answer on for 100 ms and off for 400 ms.

Worked example

A loop toggles a pin (one 40 µs call) and sleeps 100 ms. How long is the blink period, and how far off is it after an hour?

Show the solution
  1. Half-period. Each half-period is the sleep plus the call, 100.04 ms.
  2. Period. 2×100.04ms=200.08ms instead of 200 ms.
  3. Periods in an hour. 3600s0.20008s=17993 instead of 18,000.
  4. How far behind. That's 7 periods short, about 1.4 s behind.

Answer the period is 200.08 ms, and after an hour the blink is about 1.4 s behind.

Worked example

Each MicroPython hardware call takes about 40 µs on this board. How fast can a loop that only toggles a pin make a square wave?

Show the solution
  1. One toggle. Each toggle is one call, 40 µs.
  2. One cycle. A full cycle of the square wave needs two toggles: 2×40μs=80μs
  3. Frequency. f=180μs=12.5kHz at best, with some jitter from the loop itself.
  4. What it's good for. That's plenty for blinking and slow signals, and nowhere near enough for generating audio or reading fast pulses, which need timers or C.

Answer about 12.5 kHz at best.

Check

An LED is on for 100 ms and off for 150 ms. At what frequency does it blink?

Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.

Check

On for 100 ms and off for 150 ms: what's the duty cycle?

Check

An LED should blink at 2 Hz with a 10% duty cycle. How long is it on in each period?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A fixed-sleep loop also spends 2 ms on work each pass. How far behind is it after 100 passes?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

A loop makes 50 hardware calls (40 µs each) and then sleeps 5 ms. How long does one pass take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

Which loop keeps an LED toggling every 250 ms for a day without drifting?

Check

Blink the LED on P0: on for 200 ms, off for 100 ms, forever.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

Toggle P0 every 125 ms without drifting, even though each pass also reads P4 (a pull-up input) ten times.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

An LED blinks at 2 Hz with a 10% duty cycle.

(a)

How long is it on each cycle?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

And off?

Check

A MicroPython loop makes 1 hardware call(s) of 40 µs, then sleeps 100 ms.

(a)

How long is each pass?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

After 8 hours, how far behind is a count of passes against one made with exactly 100 ms passes?

Explain why

Why does sleeping for a fixed time after doing some work make a periodic loop drift, and why does a deadline fix it?

Practice

Keep practicing

12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.