While I was free during my holiday, I decided to make a simple Arduino Piano playing musical notes. Playing musical notes is quite simple with the Arduino built in function Tone(). Overall this project’s difficulty should be easy. Another reason I wanted to make this mini project, is to make a piano with 12 keys, but using lesser amount of pins.
I came across with 74HC165 Parallel In, Serial Out Shift register which able to expand the input ports for microcontroller. This is a perfect time to include and learn how to use with it. By using 74HC165 Shift register in this project, I can read 12 inputs by using only 4 pins, cool. In controlling 74HC165, I used the function shiftIn() which also includes in Arduino to read from the shift register. I will try to cover a tutorial on how to use 74HC165 Shift Register when I have more free time.
To generate the tones for each keys, you would need to determine the frequency of each note. A good reference to obtain all the required frequencies, http://cs.nyu.edu/courses/fall03/V22.0201-003/notes.htm. Then you can just sound a tone by the following code, where pin is the pin number for speaker and frequency is frequency to be generated.
Of course, when no keys is pressed, you want the speaker to play nothing. So you can stop playing by noTone() function.
How it operates? Each of the inputs at shift register are connected to a 10k ohm pull up resistor. The program will monitors the inputs from shift register. It will read the inputs as a byte from each shift register. If there is any LOW detected, the program will determine which key is being pressed. Each key is associated with different tone, and will be played from the Arduino pin through a speaker.
Alright, some images of this project before showing a few videos of this project.
Testing with the keys for 4 different octaves
I never actually learn on how to play a piano, so I can only play a few simple songs as demo. Enjoy.
Marry Had A Little Lamb
Twinkle Twinkle Little Star
Basically, this is just a simple mini project to kill some time, at the same time learning some new stuff.