5. LEDs_with_resistors
The examples below use definition blocks to better organize the code.
5.1. Connections
5.2. Model
Place the resistors first.
Place the LEDs with the long lead (leg) so that it is closest to the pin side of the circuit. In this model, the long lead is on the left side of the breadboard.
Check that the red LED is connected to pin0, yellow to pin1, and green to pin2.
Connect with the jumper wires.
5.3. Write digital
pin0.write_digital(1)
for the LED on pin0.pin0.write_digital(0)
for the LED on pin0.pin0
with pin1
or pin2
.5.4. Turn on and off pin0
from microbit import *
def turnon_0():
pin0.write_digital(1)
def turnoff_0():
pin0.write_digital(0)
while True:
if button_a.is_pressed():
turnon_0()
elif button_b.is_pressed():
turnoff_0()
sleep(500)
5.5. Blink All
from microbit import *
def blink_all_in_sequence():
pin0.write_digital(1)
sleep(1000)
pin0.write_digital(0)
pin1.write_digital(1)
sleep(300)
pin1.write_digital(0)
pin2.write_digital(1)
sleep(1000)
pin2.write_digital(0)
def blink_all():
pin0.write_digital(1)
pin1.write_digital(1)
pin2.write_digital(1)
sleep(1000)
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(0)
while True:
if button_a.is_pressed():
blink_all_in_sequence()
elif button_b.is_pressed():
blink_all()
sleep(500)
5.6. Blink using for i in range
from microbit import *
def blink0():
for i in range(3):
pin0.write_digital(1)
sleep(1000)
pin0.write_digital(0)
sleep(1000)
while True:
blink0()
sleep(3000)
Tasks
Remember that the red LED is on pin0, yellow on pin1, and green on pin2.
Write code so that pressing A turns on the green LED only and pressing B turns on the yellow LED for 3 seconds then turns on the red LED only.
Write code so that pressing A blinks red and yellow 3 times, while pressing B blinks yellow and green 3 times.
Write code to turn on each of the LED’s separately with button presses. e.g. button-A turns on red and turns the others off.
Write code so that pressing A turns on the green LED only and pressing B turns on the yellow LED for 3 seconds then turns on the red LED only.
from microbit import *
def do_A():
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(1)
def do_B():
pin0.write_digital(0)
pin1.write_digital(1)
pin2.write_digital(0)
sleep(3000)
pin0.write_digital(1)
pin1.write_digital(0)
while True:
if button_a.is_pressed():
do_A()
elif button_b.is_pressed():
do_B()
sleep(500)
Write code so that pressing A blinks red and yellow 3 times, while pressing B blinks yellow and green 3 times.
from microbit import *
def blink_A():
for i in range(3):
pin0.write_digital(1)
pin1.write_digital(1)
sleep(1000)
pin0.write_digital(0)
pin1.write_digital(0)
sleep(1000)
def blink_B():
for i in range(3):
pin1.write_digital(1)
pin2.write_digital(1)
sleep(1000)
pin1.write_digital(0)
pin2.write_digital(0)
sleep(1000)
while True:
if button_a.is_pressed():
blink_A()
elif button_b.is_pressed():
blink_B()
sleep(500)
Write code to turn on each of the LED’s separately with button presses. e.g. button-A turns on red and turns the others off.
from microbit import *
def red_on():
pin0.write_digital(1)
pin1.write_digital(0)
pin2.write_digital(0)
def yellow_on():
pin0.write_digital(0)
pin1.write_digital(1)
pin2.write_digital(0)
def green_on():
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(1)
while True:
if button_a.is_pressed() and button_b.is_pressed():
red_on()
elif button_a.is_pressed():
yellow_on()
elif button_b.is_pressed():
green_on()
sleep(500)
5.7. Write analog
pin0.write_analog(1023)
for the LED on pin0.pin0.write_analog(0)
for the LED on pin0.write_analog
can have values from 0 to 1023.write_analog
can be used to dim the LED.write_analog
on pin0.from microbit import *
brightness = [0, 205, 511, 716, 1023]
sleep_time = 250
def pulse_on():
for i in brightness:
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(0)
def pulse_off():
for i in brightness
pin0.write_analog(1023-i)
sleep(sleep_time)
pin0.write_analog(0)
while True:
if button_a.is_pressed():
pulse_on()
elif button_b.is_pressed():
pulse_off()
sleep(500)
from microbit import *
def pulse_on():
sleep_time = 40
step_size = 30
for i in range(0, 1024, step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(0)
def pulse_off():
sleep_time = 40
step_size = 30
for i in range(1023, -1, -step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(0)
while True:
if button_a.is_pressed():
pulse_on()
elif button_b.is_pressed():
pulse_off()
sleep(500)
Tasks
Modify the code to pulse on and off all 3 LEDs together.
Write code to pulse all 3 LEDs but with an analog difference of about 340, so that when the red LED is at 1023 the yellow is at (1023 - 340) and the green LED is at (1023 - 340 -340).
Modify the code to pulse on and off all 3 LEDs together.
from microbit import * def pulse_all_on(): sleep_time = 40 step_size = 30 for i in range(0, 1024, step_size): pin0.write_analog(i) pin1.write_analog(i) pin2.write_analog(i) sleep(sleep_time) def pulse_all_off(): sleep_time = 40 step_size = 30 for i in range(1023, -1, -step_size): pin0.write_analog(i) pin1.write_analog(i) pin2.write_analog(i) sleep(sleep_time) while True: if button_a.is_pressed(): pulse_all_on() elif button_b.is_pressed(): pulse_all_off() sleep(500)Write code to pulse all 3 LEDs but with an analog difference of about 340, so that when the red LED is at 1023 the yellow is at (1023 - 340) and the green LED is at (1023 - 340 -340).
from microbit import * def pulse_all_diff_on(): sleep_time = 50 step_size = 30 for i in range(0, 1704, step_size): pin0.write_analog(min(1023, i)) pin1.write_analog(max(0, min(1023, i - 340))) pin2.write_analog(max(0, min(1023, i - 680))) sleep(sleep_time) def pulse_all_diff_off(): sleep_time = 50 step_size = 30 for i in range(1704, -1, -step_size): pin0.write_analog(min(1023, i)) pin1.write_analog(max(0, min(1023, i - 340))) pin2.write_analog(max(0, min(1023, i - 680))) sleep(sleep_time) pin0.write_analog(0) while True: if button_a.is_pressed(): pulse_all_diff_on() elif button_b.is_pressed(): pulse_all_diff_off() sleep(500)
Exercises
Investigate the use of the randrange function for creating random light displays. See: https://www.w3schools.com/python/ref_random_randrange.asp
Investigate the use of the choice function for creating random light displays. Use
pin_list = [pin0, pin1, pin2]
to make a list of pins to choose from. See: https://www.w3schools.com/python/ref_random_choice.asp.
from microbit import *
import random
def random_colors():
rand_val = random.randrange(0, 1024)
rand_pin = random.randrange(0, 3)
if rand_pin = 0:
pin0.write_analog(rand_val)
elif rand_pin = 1:
pin1.write_analog(rand_val)
elif rand_pin = 2:
pin2.write_analog(rand_val)
while True:
random_colors()
sleep(100)
from microbit import *
import random
pin_list = [pin0, pin1, pin2]
def random_pin_brightness():
rand_val = random.randrange(0, 1024)
rand_pin = random.choice(pin_list)
rand_pin.write_analog(rand_val)
while True:
random_pin_brightness()
sleep(100)
5.8. Advanced: deep sleep
- Imports:
from microbit import *: Imports all functions and classes from the micro:bit module.
import random: Imports the random module for generating random numbers.
import power: Imports the power module for managing power states.
- Pin List:
Creates a list of pins (pin0, pin1, pin2) that will be used to control the micro:bit’s pins.
- Function: `random_pin_brightness`:
Generates a random value between 0 and 1023 (rand_val).
Selects a random pin from pin_list (rand_pin).
Writes the random value to the selected pin using analog output.
- Function: `turnoff`:
Turns off all pins (pin0, pin1, pin2) by setting their digital output to 0.
- Decorator and Function: `wakeup_call`:
The @run_every(s=3) decorator schedules the wakeup_call function to run every 3 seconds.
wakeup_call calls random_pin_brightness to set a random pin to a random brightness.
Sleeps for 2 seconds (sleep(2000)).
Calls turnoff to turn off all pins.
- Main Loop:
Continuously checks if button B is pressed.
If button B is pressed, it sleeps for 300 milliseconds, then puts the micro:bit into deep sleep mode for 10 minutes (600 * 1000 milliseconds), waking up on button A press.
Sleeps for 1 second before checking again.
Exercises
V2 microbit: Use power module so that the B-button puts the microbit into a deep sleep for 10 minutes. Wake it on pressing the A-button. Turn on random LEDS at random brightness every 3 seconds, then off.
from microbit import *
import random
import power
pin_list = [pin0, pin1, pin2]
def random_pin_brightness():
rand_val = random.randrange(0, 1024)
rand_pin = random.choice(pin_list)
rand_pin.write_analog(rand_val)
def turnoff():
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(0)
@run_every(s=3)
def wakeup_call():
random_pin_brightness()
sleep(2000)
turnoff()
while True:
if button_b.was_pressed():
sleep(300)
power.deep_sleep(wake_on=button_a,ms=600 * 1000,run_every=False)
sleep(1000)