Simple MicroBit Python demos

BBC MicroBit

Here are two VERY simple bits of code you can copy & paste into Mu to demo the awesomeness of Python on the MicroBit, and it’s easy for pupils to mess about with them and make them their own.

The first uses both buttons, a touch button and a shake sensor to trigger events.

For output it displays images and plays a tune if you connect a speaker to pins 0 and GND. You can use crocodile clip leads like you’d find in a MakeyMakey kit to do this.

I’ve included some other image and tune names in the comments to encourage children to tinker.

Enjoy!

from microbit import *
import music

while True:
    if button_a.is_pressed():
        display.show(Image.PACMAN)
    if button_b.is_pressed():
        music.play(music.NYAN)
    if accelerometer.was_gesture('shake'):
        display.show(Image.GHOST)
    if pin1.is_touched():
        display.show(Image.SURPRISED)

# Connect a speaker to pins 0 and GND to hear the music

# try these images: HEART, MEH, RABBIT, COW, DUCK, SWORD, GIRAFFE
# TARGET, HAPPY, HOUSE, TORTOISE, UMBRELLA, SNAKE, SKULL, BUTTERFLY

# try these musics: ENTERTAINER, PRELUDE, ODE, BIRTHDAY, PYTHON, WEDDING

…and here’s the MicroBit shaky dice program in Python:

from microbit import *
import random

one = Image("00000:"
            "00000:"
            "00900:"
            "00000:"
            "00000")

two = Image("00000:"
            "09000:"
            "00000:"
            "00090:"
            "00000")         

three = Image("90000:"
              "00000:"
              "00900:"
              "00000:"
              "00009")            

four = Image("00000:"
             "09090:"
             "00000:"
             "09090:"
             "00000")

five = Image("00000:"
             "09090:"
             "00900:"
             "09090:"
             "00000")           

six = Image("09090:"
            "00000:"
            "09090:"
            "00000:"
            "09090")

display.scroll('Shake me!')

while True:
    if accelerometer.was_gesture('shake'):
        throw = random.randint(1, 6)
        if throw == 1:
            display.show(one)
        if throw == 2:
            display.show(two)
        if throw == 3:
            display.show(three)
        if throw == 4:
            display.show(four)
        if throw == 5:
            display.show(five)
        if throw == 6:
            display.show(six)
This entry was posted in computers and tagged , , , . Bookmark the permalink.

4 Responses to Simple MicroBit Python demos

  1. Paul Woolley says:

    Nice starter examples, going to be a lot of teachers this September looking for these to get their pupils going.

  2. Hi there,

    may I kindly ask you for advice what impedance I should use with Microbit to prevent damage?

    Thanks, all the best

    Vladimir

  3. How can i put the Note in array and let program do just one note and wait for keypress Thank very much.I am from THAILAND.
    from microbit import *
    import music

    i= ["C4:4", "D4:4", "E4:4", "C4:4", "C4:4", "D4:4", "E4:4", "C4:4",
    "E4:4", "F4:4", "G4:8", "E4:4", "F4:4", "G4:8"]
    for i in range(14): # Add 100 temperature values
    music.play(i)
    pressed key

Leave a Reply