Musical quiz buzzer for BBC micro:bit: Python for beginners

Musical quiz buzzer for BBC micro:bit: Python for beginners

Follow this step by step guide to turn a BBC micro:bit into a musical quiz buzzer: use it in class or at home whenever you feel the urge to work out who’s ready to answer a question first out of two teams / individuals.

This tutorial is designed for beginners with little or no programming experience in python but it’ll cover:

Concepts you'll understand
Concepts covered in this tutorial

Concepts:

  • Loops: repeating code with a while loop
  • Lists: storing more than one piece of data in order
  • Conditional logic: using IF statements to let your code make decisions.

 

 

Skills covered in this tutorial
Skills covered in this tutorial

Skills:

  • How to display text and images on a micro:bit screen
  • How to detect if a button has been pressed on a micro:bit
  • How to play music on a micro:bit with a speaker
Flappy bird Micro:bit python tutorial for beginners

Flappy bird Micro:bit python tutorial for beginners

Follow these simple steps to code your own version of flappy bird on the BBC micro:bit. This tutorial is designed for someone with little or no programming experience who wants to get started writing python code. You don’t need a micro:bit to follow this tutorial, but it’s more fun if you’ve got one.

The BBC micro:bit only has a 5×5 LED screen so the graphics on our version of flappy bird are going to be predictably poor. But that means that it’s not too difficult to write (my version has 82 lines of code including comments and blank lines)

Before we start:

We’re going to write our program in python using the micro:bit simulator on create.withcode.uk.

Flappy Bird on the BBC micro:bit in python
Flappy bird micro:bit python

Why Flappy Bird?

The original Flappy Bird is a really simple idea for a game that’s fun to play. By going through this tutorial you’ll learn how to understand / use the following in python:

  • How to display text on the micro:bit screen
  • How to use variables to keep track of score
  • How to display images on the micro:bit screen
  • How to scroll images on the micro:bit screen
  • How to detect if the user presses a button
  • How to keep part of your code looping

 

Why Python?

Python is a text-based programming language that’s designed to let you write as little code as possible that gets as much done as possible. There are other languages you can code a micro:bit with which are perhaps easier for beginners but once you’ve mastered the basics in python it’s much easier to create whatever you like – there’s no faffing around dragging hundreds of blocks together and using your keyboard to write code ends up being much faster than using your mouse / tapping your screen. The skills you learn in this tutorial will also help you with other python programming projects – not just those for the micro:bit.

Why the micro:bit simulator?

Testing your code on an actual BBC micro:bit is much more fun than running in in a simulator. You can use the BBC micro:bit site or the offline python editor mu to write code and send it to your micro:bit. Sometimes though, a simulator can be really useful:

  • It lets you test the code without having to download the .hex file and wait for it to copy to the device
  • It’s easier to find and fix errors
  • It lets you run the code one line at a time
Code your own bedroom door sign: python on a BBC micro:bit for beginners

Code your own bedroom door sign: python on a BBC micro:bit for beginners

This is a BBC micro:bit for beginners tutorial that shows you how to write some python code to turn a micro:bit into a sign you can stick to your bedroom door.

Python is a programming language that’s designed to let you write as little code as possible to make as much work as possible. Writing code on in python on a micro:bit is a great way to get your head around the essentials in python and hopefully have a lot of fun along the way too.

The micro:bit has an LED screen with just 5×5 pixels (dots). This isn’t much compared to the 2880×1800 pixels you might be used to if you’ve got a Macbook Pro but it’s big enough to display simple pictures and text one letter at a time.

Try it with code
Display code on a BBC micro:bit

We’re going to be using the create.withcode.uk microbit simulator to write and test our code. To run the code you can press Ctrl + Enter or click on the run button in the bottom right of the code window.

 

If you want to test your code on an actual micro:bit, run the code in the simulator first then click on the Download Hex button. This’ll download a my_code.hex file that will run on your micro:bit when you drag it and drop it as though you were copying a file to USB memory stick.

 

 

There are two ways of displaying text to the micro:bit screen: display.scroll() and display.show(). What’s the difference? Which do you prefer?

Let’s go through each of the lines in turn:

import microbit
microbit.display.scroll("Hello")
microbit.display.show("World")

Line 1 tells python to import the microbit module. That means that python loads some extra code which tells it how to control a microbit. Lines like this, where you’re importing a module, usually go at the top of your code.

import microbit
microbit.display.scroll("Hello")
microbit.display.show("World")

Inside the microbit module there’s another module called display. Inside display is a procedure called scroll. The round brackets after scroll tell python to call (run) that procedure.

 

string
A “string” of characters

Notice the text in the brackets is surrounded by quotation marks: we call this a string because it joins the characters together as though they were threaded on a string like a washing line.

The scroll procedure scrolls the text from right to left, moving it one pixel at a time.

 

 

import microbit
microbit.display.scroll("Hello")
microbit.display.show("World")

Line 3 is similar to line 2 but instead of scrolling the text pixel by pixel, we show one character at at time.

You might notice in some tutorials that instead of  import microbit you see  from microbit import *

Both of these tell python to load code from the microbit module, but the second means that you don’t have to write  microbit. every time you use the microbit module:

from microbit import *
display.scroll("Hello")
display.show("World")
import microbit
microbit.display.scroll("Hello")
microbit.display.show("World")

Both of the two programs above will do the same thing when they run. The top one is easier to write but some people prefer the bottom one to save confusion later in the development process.