Skip to content

Activity

Jukebox

Beginner | MakeCode, Python | Buttons, Pins, Sound, Speaker | Hardware & software, Input/output, Sound

Step 1: Make it

What is it?

Make a music machine that plays different tunes at the push of a button.

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

Introduction

Coding guide

How it works

  • A jukebox is a machine that plays different songs when you put a coin in and choose which tune you want to hear by pressing buttons.
  • This micro:bit jukebox uses the A and B button inputs to play different built-in tunes.
  • If you have a micro:bit V2 with built-in speaker, you'll hear the sound straight away. You can also attach headphones to any micro:bit using crocodile clip leads:
headphone plug tip connected to micro:bit pin 0, long part of headphone plug connected to GND on micro:bit
  • Connect micro:bit pin 0 to the tip of your headphone plug, and GND to the longer part of the headphone plug.
  • When you make sound with the micro:bit, pin 0 becomes an output.

What you need

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

Step 2: Code it

1from microbit import *
2import music
3
4while True:
5    if button_a.was_pressed():
6        music.play(music.ODE)
7    if button_b.was_pressed():
8        music.play(music.BLUES)
9

Step 3: Improve it

  • Change the tunes, for example try PRELUDE or ENTERTAINER. You can find a list of all the built-in tunes you can use in Python here.
  • Show different pictures, letters, numbers or words depending on which tune you picked.
  • Make it play a different tune if you shake it, press buttons A and B at the same time, or press the micro:bit's touch logo.