Eight Key Piano
In this lab we wire up eight momentary press buttons so that when each one is pressed it will play a different note.
To create this project, you will need the following parts:
- A Raspberry Pi Pico
- A standard size breadboard or two 1/2 breadboards
- 8 momentary press buttons
- A speaker or a Piezo buzzer
- An optional sound amplifier such as the LM386 DC 5V-12V Mini Micro Audio Amplifier which can be purchased on e-bay for under $2 USD. If you are working in a quiet room you may not need the amplifier.
Each "key" is a momentary press button that is wired up between a GPIO pin and the +3.3 volt rail on the breadboard connected to 3V3(OUT) pin. We do not need to use any resistors to pull the signals low since we configure the pins to be inputs with the PULL_DOWN resistor like this:
The Play Tone Functions
We will use two Python functions, one for playing a tone of a given frequency and one for turning off the sound.
1 2 3 4 5 6 7 8
We will also use the
.value() method on each pin to detect if it is HIGH (1) like this:
We will be playing "notes" generating square waves with various frequencies from our lowest note of A3 at 220 Hz up to A4 at 440 Hz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
- Rewrite the code above using lists for the pin numbers and the notes.
- Try different notes with other scales.
- Add another button to change the "octave" of the notes.
- Add a display to show the notes as they are being played.
- Print the time each note is being pressed as well as the length of the pauses between the notes.
- Write the notes to a recording file.
- Add a menu system so you can do things like start a new song recording, save a recording and playback a recording.
- Eight keys are not enough for many songs. Use two full-size breadboards to expand the number of keys on your piano.
- Look into getting a MIDI keyboard such as the 32-key $40 MIDIPLUS AKM320 USB MIDI Keyboard Controller
- Read this blog about running MIDI on the Pico: MIDI, MicroPython and the Raspberry Pi Pico