Build a walking piano with the RaspberrySTEM Creator Kit


Each lesson takes the user through progressively more involved programming and hardware projects. Project builds are illustrated and discussed very clearly. Code for a project is usually presented complete and described as needed. For more in-depth knowledge, hyperlinked "Concepts" pages go beyond the project at hand.

Hardware concepts presented include providing power, breadboarding, working with LEDs and buttons, using GPIO pins as input or output, adding a circuit board, and reading a schematic. Programming concepts cover lists, strings, numbers, and variables; assignments and math; conditional statements and for and while loops; and functions and modules.


Three of the lessons included in the RDE show you how to build a piano, starting with four buttons assigned four notes of equal length, then modifying the program to play notes that respond to the length of time you press a button. The final lesson shows you how to create you own piano "key" and wire it to the RPi2.

The Fritzing diagram in Figure 7 shows a button on the breadboard attached to GPIO15. The button outside the breadboard represents the self-made "button" presented in the final piano lesson; it is wired in parallel with the breadboard button, so pressing either button plays the note assigned to GPIO15. The black wire to the blue rail is ground (GND). Both buttons have one side wired to GND. The blue and yellow wires attach the other sides of the buttons to the correct pin. The code to play a C is shown in Listing 1; rstem.button and rstem.sound are RaspberrySTEM modules that provide the additional code needed to handle the built-in speaker and button functions.

Listing 1

Playing a Note

01 from rstem.button import Button
02 from rstem.sound import Note
03 import time
05 button = Button(15)
06 note = Note('C')
08 while True:
09     if button.is_pressed():
11     time.sleep(0.01)
Figure 7: Wiring diagram of a single piano key with two input buttons. (Fritzing)

I wanted to expand on Project 14: Build a Walkable Piano and create a walking keyboard of eight notes. The first step was to map my project (Figure 8). Each key must be wired to its own GPIO pin. Next, I had to make eight piano keys.

Figure 8: Layout and pin assignments for the eight-key walking piano.

After gathering the materials (Figure 9), I set about creating the keys, which are essentially home-made buttons. One key is made with two pieces of cardboard, each covered with a wide strip of foil and taped together like a book (Figure 10). Bumper strips are added along each side of one foil-wrapped piece to keep the two sides from touching when at rest. When wired to the RPi2 as shown in the Fritzing diagram, the button plays a note when pressed. Detailed instructions for building a button are described in the Project Guide.

Figure 9: The materials needed to make a walkable piano key: recycled cardboard, foil, wire, box-cutter or scissors, a wire cutter/stripper, and tape.
Figure 10: Building a button. (A) Wrapping a piece of cardboard with foil. (B) Adding the "bumper" strips. (C) Taping the foiled parts together like a book. (D) Adding a wire to each side of the key.

After building all my keys, I wired each one to the GPIO connector board (Figure 11) as diagramed in my original plan and according to the Fritzing diagram. For the wires attached to the key, I exposed a good 1 inch for taping to the foil. On the other ends, I stripped just enough insulation to insert the wires into the breadboard.

Figure 11: It might look like a mess, but each key is wired to ground and its own GPIO pin, as shown in Figure 7. Some of the keys are wired in parallel with a pushbutton from a previous project.

The code in Listing 2 defines every key (Button) and its corresponding GPIO pin in a list. The list in line 6 assigns a Note to each button; it must have the same number of elements as the buttons list and appear in the order in which they should be assigned to a button. The while block looks for a pressed button and plays the appropriate note.

Listing 2

Walkable Piano

01 from rstem.button import Button
02 from rstem.sound import Note
03 import time
05 buttons = [Button(15), Button(18), Button(23),
              Button(24), Button(25), Button(7),
              Button(16), Button(21)]
06 notes = [Note('C'), Note('C#'), Note('D'),
            Note('Eb'), Note('E'), Note('F'),
            Note('F#'), Note('G')]
08 while True:
09    for button, note in zip(buttons, notes):
10       if button.is_pressed():
11          if not note.is_playing():
13       else:
14          note.stop()
15    time.sleep(0.01)

Although the resulting build (Figure 12) is indeed walkable, it is much easier to play by hand. The program can play more than one note at a time, but you might hear some trilling interference. Walking the keyboard, therefore, requires energetic jumping from key to key for clean sound output.

Figure 12: An eight-key walkable piano; only 80 more to go. I plugged in a set of external speakers instead of using the RaspberrySTEM speaker.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Diving Deep

    The past year has seen a continued emphasis on STEM (science, technology, engineering, and mathematics) education in English-speaking countries.

  • A new way of no-solder prototyping

    The Grove system's standardized connector and multitude of devices allow quick and easy project prototyping with your favorite small-board computers.

  • Android Pi

    Your Android device can be a versatile companion for Raspberry Pi. We describe some useful apps to help you make this happen.

  • Blynk for Raspberry Pi

    Use your Android device to build graphical interfaces for interacting with Raspberry Pi.

  • The Pi Wire

    As with everything in the technology industry, things move quickly in the Raspberry Pi ecosystem – so much so that it is sometimes hard to keep up! This regular column rounds up the best Raspberry Pi and open source hardware news to keep you up to date on the latest developments, projects, products, and events.