In this activity, you will:
(1) Setup from Connect and Blink a Light [1] including the green LED circuit parts.
(1) LED - yellow
(1) Resistor - 220 Ω (red-red-brown-gold)
(1) Jumper wire (black)
To build the LED circuits in the picture shown below:
This script will test your LED circuits by blinking them on/off together.
# led_blink_x2 from microbit import * while True: pin13.write_digital(1) pin14.write_digital(1) sleep(500) pin13.write_digital(0) pin14.write_digital(0) sleep(500)
These statements repeat indefinitely inside the while True loop:
Changing from simultaneous to alternating blinks is pretty simple. All you have to do is change the state argument in two of the pin14.write_digital calls.
Up to this point, it looks like both lights change at the same instant. In reality, the P13 light turned on a tiny fraction of a second before the P14 light. That’s because the micro:bit executes statements in a script sequentially—one after another. Since pin13.write_digital() comes before pin14.write_digital() in the script examples, the P13 light changes first. The P14 light changes an instant AFTER the P13 light.
In the current script, the time between light changes for P13 and P14 is not visible to the human eye. You can make it visible by adding a sleep statement between the two write_digital() calls (along with other changes).
Sequential blinking takes a few more changes since the script has to turn one light on/off, and then turn the other light on/off afterwards. In this approach, the script turns the P13 light on and off first, then follows with the P14 light.
In this activity, you learned how to use the digital_write() method to blink two lights in various arrangements to each other.
Create a script that holds the green LED on while the yellow LED turns on and off. Place the code for the green LED outside the while True() loop.
Solution:
from microbit import* pin13.write_digital(1) while True: pin14.write_digital(1) sleep(500) pin14.write_digital(0) sleep(500)
Links
[1] https://learn.parallax.com/tutorials/led-lights/connect-and-blink-ligh
[2] https://python.microbit.org/v/2