Professional Documents
Culture Documents
Raspberry Pi Piano Project
Raspberry Pi Piano Project
L6-Group 9
Syed Muhammad Asad Hasan Naqavi, Shahjahan Sangrasi, Huzaifah
Tariq Ahmed.
Aim:
To put the practicality of Python to use via interfacing it with Raspberry
Pi and hence creating a piano which plays all the seven notes.
Components:
1. Breadboard
2. Raspberry Pi
3. Jumper Wires
4. Pushbuttons
5. Passive Buzzer
Implementation:
The group members decided that the code should first be written
beforehand and then implemented upon. The code can be seen below
with detailed comments:
From gpiozero import TonalBuzzer, Button
From gpiozero.tones import Tone
From time import sleep
#importing different libraries for use in the project
b=TonalBuzzer(21) #defining GPIO pin number 21 as a buzzer
#defining GPIO pin numbers in the brackets for the respective
buttons
button1=Button(2)
button2=Button(3)
button3=Button(4)
button4=Button(17)
button5=Button(27)
button6=Button(22)
button7=Button(10)
#now defining the notes or harmonics of the piano
def turner():
b.stop()
def note1(): #A note
b.play(Tone(440))
def note2(): #B note
b.play(Tone(494))
def note3(): #C note
b.play(Tone(523))
def note4(): #D note
b.play(Tone(587))
def note5(): #E note
b.play(Tone(659))
def note6(): #F note
b.play(Tone(698))
def note7(): #G note
b.play(Tone(784))
#assigning the notes to each button which will then play the
corresponding note when pressed
button1.when_pressed=note1
button1.when_released=turner
button2.when_pressed=note2
button2.when_released=turner
button3.when_pressed=note3
button3.when_released=turner
button4.when_pressed=note4
button4.when_released=turner
button5.when_pressed=note5
button5.when_released=turner
button6.when_pressed=note6
button6.when_released=turner
button7.when_pressed=note7
button7.when_released=turner
Bugs:
Our group faced trouble in outputting the sound directly from the in-
built function of the python library for different frequencies such as the
minor and major frequencies of each harmonic. However, it was not
applicable since there was non-availability of the piezo-electric buzzer
which resonates electromagnetically and produces a variety of pleasant
sounds. This was overcome by using python’s TonalBuzzer library
which accorded for a fixed amount of frequencies-which were then used
to play each note, albeit flat.
Learning Outcomes:
This was an extremely fun-filled project which, as stated in our aim
above, enabled us to bridge the gap between theory and practical
implications of Python. It also gave us a deep insight in to a variety of
such projects that have been made using the same inspiration as us as
well as giving us new ideas which we hope to implement as well.