Skip to content

Activity

Frère Jacques loops

Intermediate | MakeCode, Python | Pins, Sound | Composition, Iteration, Pattern recognition, Sound

Step 1: Make it

What is it?

Program your micro:bit to play a famous tune – or one of your own.

These two videos show you what you'll make and how to code it:

Introduction

Coding guide

How it works

  • Connect headphones, or amplified speaker to pins 0 and GND on your micro:bit to hear the sound.
  • Pin 0 works as an output when we play music on the micro:bit.
  • This program uses musical notes to play a famous French folk tune. ‘Frère Jacques’ repeats each bar twice. We could program micro:bit to play the same notes again, but it’s much easier to use iteration (also known as a loop). This program uses loops that play each bar twice to save having to program the same notes twice.
  • Recognising patterns like this is part of computational thinking, a way of making sure computer programs are as efficient as possible, using the smallest amount of code to perform a task – or perform piece of music!
micro:bit attached to headphones, music stave showing first 4 notes of Frère Jacques

What you need

  • micro:bit (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)
  • a pair of headphones, buzzer or amplified loudspeaker
  • two crocodile clip leads

Step 2: Code it

1from microbit import *
2import music
3
4for x in range(2):
5    music.play(["C4:4", "D4", "E4", "C4"])
6
7for x in range(2):
8    music.play(["E4:4", "F4", "G4:8"])
9

Step 3: Improve it

  • Speed it up or slow it down by changing the tempo.
  • Complete the full tune. The extra notes you’ll need are GAGFEC, CGC but you’ll need to play the first 4 notes for half a beat, not 1 beat. Remember to use loops to make your code more efficient.
  • Program other tunes using sheet music or make up your own.
  • You can find out more about how micro:bit music works in Python here.