CS170: Programming for the World Around Us - Tin Foil Piano with the micro:bit

Activity Goals

The goals of this activity are:
  1. To use loops to create repetition in a program
  2. To implement code in functions for easy reuse
  3. To use the micro:bit pin interface to read sensor data from the environment

The Activity


Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Tin Foil Piano using the micro:bit

There's nothing remarkable about (the piano). All one has to do is hit the right keys at the right time and the instrument plays itself.
Johann Sebastian Bach

Pinout for the micro:bit to use headphones and allegator clips to tin foil pads


  1. The micro:bit comes with 5 pins: P0, P1, P2, a 3-Volt connection, and an electrical ground. We can measure the current on P0, P1, and P2, related to the voltage difference between the pin and ground. With alligator clips, connect two pieces of tin foil to ground and P1.
  2. You can hook up headphones if you like, but the micro:bit v2 has speakers built in!
  3. When the P1 pin is pressed (that is, when you touch the P1 tin foil while touching the ground tin foil), play a sound.
  4. Be very careful never to connect the 3V pad to ground. Only connect the P0, P1, and P2 pads to ground!

Model 2: Changing the Sound

\(pitch = base \times (2^{\frac{1}{12}})^{step} = base \times 1.05946309436^{step}\)


  1. A note's pitch is determined by its frequency. Each successive note can be calculated as a "half step" above or below a base frequency. The formula is given above. Using a calculator, what is the frequency of the note right above Middle C (which is 262 Hz), and the note right below Middle C?
  2. What note do these two frequencies correspond to? Use this table to look up a note by its frequency.
  3. Using the online pitch detector, sing "do-re-me" into the computer. If you're not in the mood for singing, you can use this online piano player instead. What notes do you hear? What frequencies are they? Do higher frequencies correspond to higher pitched sounds or lower pitched sounds?
  4. Create a variable called frequency, and on start, set it to some note (like Middle C, or Concert A, which is 440 Hz). Play that note when P1 is pressed.
  5. Create another variable called step that increments by 1 when you press B, and decrements by 1 when you press A. Then, calculate frequency using the formula above.
  6. Modify this program to use a loop to play a crescendo of 10 notes when the A+B button is pressed, by setting step to step + 1, calling calculateFrequencies, and playing the resulting tone.
  7. Modify this program to play a second (different) note when P2 is pressed. You'll need a new variable to keep track of the frequency and step.
  8. Modify the program to allow the user to do something to change the beat (like shaking the microbit). Start with one-eighth beat, and keep adding one-eighth until the beat reaches 2; then go back to one-eighth. Hint: you can use an if statement to "wrap around" the value if it is 2, and add one-eighth to it otherwise (this is called an else statement)!


I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.

Teacher Highlights

  • Teachers can set up a MakeCode Classroom environment by activating this template.