Skip to content

Activity

Jukebox with volume

Intermediate | MakeCode | Accelerometer, Buttons, LED display, Sound | Hardware & software, Input/output, Sound

Step 1: Make it

What is it?

This project adds a volume control to a micro:bit jukebox. It plays different tunes if you press button A or B, and you can also adjust the volume by tilting your micro:bit left or right.

How it works

  • If you're using a real micro:bit, connect it to headphones like this:
diagram showing how to connect headphones to a micro:bit
  • The current volume is stored in a variable called volume.
  • micro:bit volume numbers can go from 0 to 255, so at the start of the program we set the volume somewhere in the middle, to 128.
  • The accelerometer detects when you tilt your micro:bit left and right.
  • If you tilt it left, the volume variable is reduced by 32. (We picked 32 rather than 1 so you can hear the change when you tilt it once.)
  • When you tilt it right, the volume variable is increased by 32.
  • We use if... then blocks to stop the volume variable going below 0 or above 255 as the micro:bit's volume can't go below 0 or above 255.
  • A forever block keeps updating the micro:bit's actual volume from changes you make to the volume variable when you tilt it. It also updates the bar graph so you can see a visual representation of the current volume setting.

What you need

  • micro:bit (or MakeCode simulator)
  • if using a real micro:bit, a pair of headphone and two crocodile clip leads

Step 2: Code it

Step 3: Improve it

  • Program your own melodies.
  • Add a mute function when you press A+B buttons together.
  • Modify the volume control so it's controlled by other gestures like shake.