In today's class, we'll learn how to use Python to control our micro:bits. This is super awesome because it lets us do a few things:
Remember how we had to import the micro:bit extension in Scratch in order to control it in our program? We have to do something similar in Python. At the top of every program that works with the micro:bit, you must include this line:
from microbit import *
.
Let's dissect what each part of this line means. In general, in Python, import
tells the script to add a package; this is comparable to adding an extension in Scratch. Sometimes we use the line import [package]
, but in this case, we're using from [package] import
. This is because we want to specify what we want to import from the package. In Python, using the asterisk in the import line, *
, means "all." So what we're telling the program with from microbit import *
, we're really saying, "take the microbit package and add all of its functions to my code."
We can now move onto the fun codelabs! Here are a few things to note before we dive in:
#
in front of a line of code.score = 0
and then increase it later in the program with score += 1
. This would add one point to the score.You can go to this website, the micro:bit Micropython API, to find a list of functions that you can use and a description of what it does and examples of how to use them. On the side bar, you can click on which part of the micro:bit you want to operate and under "Functions," you'll find a webpage with all of the functions/actions that part can use.
Start with the Clapping Lights project with your breakout room. Afterwards, choose any of the other projects that sound interesting by pressing the slide from the list on the left side of this website.
Turn on all of the LEDs/some image on the LEDs when you clap once. The next time you clap, the lights should turn off.
You will likely want to use the following functions in your code:
display.show()
- this tells the LEDs to make a shape.Image()
- this tells the LEDs what shape to make. You control the brightness by the number in each spot (0 is off, 9 is the brightest). You must add 5 strings to specify the image, each one ending with a colon :
.display.clear()
- turn all of the LEDs in the display off.microphone.was_event()
- listen to hear if the microphone detected a noise.SoundEvent.LOUD
- the microphone heard a LOUD sound.This image shows how to display three vertical lines on the micro:bit, where the first and third are brightest and the middle is at half-brightness.
while True:
, which means that it will execute as long as True
is true, which is always.You can make this project your own by customizing different elements of the program. For example, you can change:
Introduce yourself with the micro:bit. Have it flash your name.
You will likely want to use the following functions in your code:
display.scroll()
- this tells the LEDs write something in scrolling letters. This takes a string, or text that is surrounded by quotation marks.display.clear()
- turn off all of the LEDs.sleep(int)
- stop the micro:bit function for int amount of time (int
should be a number) in milliseconds. 1000 milliseconds = 1 second. You can make this project your own by customizing different elements of the program. For example, you can change:
Flash a happy face on the display 5 times when the A button is pressed. Flash a sad face on the display when the B button is pressed.
You will likely want to use the following functions in your code:
display.show()
- this tells the LEDs to make a shape.Image.HAPPY
- tells the LEDs to show a smiley face.Image.SAD
- tells the LEDs to show a sade face.display.clear()
- turn all of the LEDs in the display off.button_a.is_pressed()
and button_b.is_pressed()
- detect whether button A or B (respectively) have been pressed.sleep(int)
- stop the micro:bit function for int amount of time (int
should be a number) in milliseconds. 1000 milliseconds = 1 second. You can make this project your own by customizing different elements of the program. For example, you can change:
Image()
might be a useful function for this.Have the micro:bit track how many steps you take. Create a steps variable and add to it every time a step was taken. Display the current steps.
You will likely want to use the following functions in your code:
display.show()
- this tells the LEDs to make a shape.accelerometer.was_gesture('shake')
- check if the micro:bit was shaken.You can make this project your own by customizing different elements of the program. For example, you can change:
Create a stopwatch with your micro:bit. Start the timer when you touch the pin logo and stop it once it's pressed once more. After the clock is stopped, display how long it ran with scrolling text on the LEDs.
You will likely want to use the following functions in your code:
display.scroll()
- this tells the LEDs write something in scrolling letters. This takes a string, or text that is surrounded by quotation marks.pin_logo.is_touched()
- detects if the pin logo on the micro:bit was touched. Returns True
if it was.running_time()
- reports how long the micro:bit has been running at the instant that it is called. In milliseconds.running_time()
does not change automatically. To check how much it has been updated, it needs to be called again.running_time()
is in milliseconds. To get the time in seconds, divide by 1000.You can make this project your own by customizing different elements of the program. For example, you can change:
Display the current temperature that the micro:bit is sensing with the LEDs.
You will likely want to use the following function in your code:
temperature()
- returns the current temperature, in Celsius.You can make this project your own by customizing different elements of the program. For example, you can change:
Turn on all of the LEDs when the micro:bit is in a dark room. Turn off all of the LEDs when the room is bright again.
You will likely want to use the following function in your code:
display.read_light_level()
- returns the light exposure it detects; the lower the number, the darker the room.You can make this project your own by customizing different elements of the program. For example, you can change:
Use the compass element of the micro:bit to display the direction that it is pointed towards on the LEDs.
You will likely want to use the following function in your code:
compass.calibrate()
- calibrates/tunes the internal compass of the micro:bit to make sure the readings are accurate.compass.heading()
- returns the degree that the micro:bit is pointed toward. There are 360 degrees total.You can make this project your own by customizing different elements of the program. For example, you can change:
Display how loud the noise around the micro:bit is with a bar on the LEDs. If the sound is the loudest it could possibly be, all of the LEDs should be lit up. If it is in the mid-range of noise, then the bottom three horizontal lines of lights should be on. If it is dead silent, none of the LEDs should be on.
You will likely want to use the following function in your code:
microphone.sound_level()
- returns a number representing how loud the current sound is. 0 is dead silent, 255 is the loudest possible reading.Image()
- tell the LEDs what to display with 5 strings of numbers, each ending with a colon :
.microphone.sound_level()
. Store this in some variable.Image()
configurations to variables, such as graph0
, graph1
, etc. and setting them all as follows:all_graphs = [graph0, graph1, graph2, graph3, graph4, graph5]
.You can make this project your own by customizing different elements of the program. For example, you can change:
Have your micro:bit act as a metronome to help you practice your music. It should make a sound once a measure at whatever tempo you want.
You will likely want to use the following function in your code:
music.set_tempo(bpm=int)
- defines the music class with a certain BPM (beats per minute) at the desired tempo (int
).music.play([])
- have the micro:bit make noise at a certain pitch for a given duration. For example, to play the note C for one beat and then to rest for 3, this function would be music.play(["C4:1", "r:3"])
where "C4" is the note "C" and it plays for one beat (":1"), and "r" means rest for 3 beats (":3").import music
.You can make this project your own by customizing different elements of the program. For example, you can change:
Create your own little micro:bit pet. Have your micro:bit smile when you give it attention (by touching the pin logo). It should giggle when you shake the micro:bit. It should be sad if you don't play with it for 10 seconds. It should get tired and yawn if you don't play for 15 seconds. It should die if you don't play with it for 20 seconds. It should wake back up and smile if you touch it again!
You will likely want to use the following function in your code:
display.show()
- makes the LEDs turn on in the configuration you add.Image.HAPPY
- turn the LEDs on to a smiley face.Image.SAD
- turn the LEDs on to a sad face.Image.SURPRISED
- turn the LEDs on to a surprised face.Image.ASLEEP
- turn the LEDs on to a sleeping face.Image.SKULL
- turn the LEDs on to a skull.accelerometer.was_gesture('shake')
- detects if the micro:bit was shaken.audio.play()
- make the given noise.Sound.HAPPY
- make a noise showing that the micro:bit pet is happy.Sound.GIGGLE
- make a noise showing that the micro:bit pet is giggling.Sound.SAD
- make a noise showing that the micro:bit pet is upset.Sound.YAWN
- make a noise showing that the micro:bit pet is tired.Sound.MYSTERIOUS
- make a scary noise to show that the micro:bit pet died.import audio
.running_time()
or sleep(int)
and timer += int
.You can make this project your own by customizing different elements of the program. For example, you can change: