Script and Tests

Script: led_blink

Let's make the LED blink using a micro:bit script.

  • Enter the led_blink script (python.microbit.org/v/2 matches the screencaptures)
  • Set the Script Name field to led_blink.
  • Click Load/Save, and then click Download Project Hex to save your work.  Close the Load/Save dialog box after downloading.
  • In the python.microbit.org editor, click Connect, and then click Flash.   
# led_blink

from microbit import *

while True:
    pin14.write_digital(1)
    sleep(500)
    pin14.write_digital(0)
    sleep(500)

 

Tests

  • Verify that the green LED blinks on/off at about once every second.

There are lots of different ways to change the timing in the blinking pattern.  Try these examples to understand the cause and effect.

  • Try changing the sleep(500) calls to sleep(2500), then flash the modified script.  What’s the effect?
  • Try changing the sleep calls from sleep(2500) to sleep(250).  Again, flash the modified script and observe the effect.
  • Repeat using sleep(100) for both sleep calls.  Can you now predict what's going to happen?
  • How much can you reduce the sleep arguments until it’s hard to tell that it’s blinking?
  • Try sleep(200) after the pin14.write_digital(1) call and sleep(800) after the pin14.write_digital(0) call. What happened?
  • Try reversing the sleep values so that sleep(800) follows pin14.write_digital(1) and sleep(200) follows pin14.write_digital(0). Did it have the effect you anticipated?
  • .Finally, change both sleep calls back to what you started with: sleep(500) and compare it to the previous two options.