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:

Steps to access the CodeIt micro:bit Python classroom:

  1. Open the micro:bit classroom website here. You should arrive at a page that looks like:
  2. Select our classroom name elements from the list. Our classroom name is
  3. The "Classroom Name" section on your website should look like the image below.
  4. Enter the classroom PIN in the "PIN" section. Our PIN is 538143.
  5. Move onto the next page by pressing "Continue" and enter your name.

Set up your Python editor

  1. Press the "connect" tab and choose your micro:bit to connect it to the Python editor. You must be connected to the computer using a USB cord.
  2. Flash the code onto your micro:bit to connect it. Do this by pressing the "flash" tab in the top left. This will only show up if your micro:bit is connected to the editor (from the step above).
  3. Now, you should be ready to code!

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:

The functions you can use in Python with the micro:bit

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.

Have fun!

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.

Goal of this lab

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.

Relevant functions

You will likely want to use the following functions in your code:

Useful example

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.

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

Introduce yourself with the micro:bit. Have it flash your name.

Relevant functions

You will likely want to use the following functions in your code:

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following functions in your code:

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following functions in your code:

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following functions in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

Display the current temperature that the micro:bit is sensing with the LEDs.

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

Use the compass element of the micro:bit to display the direction that it is pointed towards on the LEDs.

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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.

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change:

Goal of this lab

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!

Relevant functions

You will likely want to use the following function in your code:

Tips

Add your own spin to it

You can make this project your own by customizing different elements of the program. For example, you can change: