Script: led_blink
Let's make the LED blink using a micro:bit script.
- If it's not already connected, connect your micro:bit module to your computer with its USB cable.
- If you do not already have the micro:bit Python Editor open, browse to python.microbit.org in a Google Chrome or Microsoft Edge browser.
- Set the project name field to led_blink.
- Enter led_blink into the micro:bit Python Editor, then click Save.
(See Save & Edit Scripts.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# 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. To test a change to the script, click Send to micro:bit. Then, check the LED light for a changes.
- 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.