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.

74hc165
74HC165 Parallel In, Serial Out Shift Register

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.


tone(pin, frequency)

Of course, when no keys is pressed, you want the speaker to play nothing. So you can stop playing by noTone() function.

musical notes

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.

The code for Arduino is as below:


#define load 5
#define clock 6
#define data 7
#define enable 8
#define load1 9
#define clock1 10
#define data1 11
#define enable1 12
#define speaker 4
#define pot A0

void setup()
{
pinMode(speaker, OUTPUT);
pinMode(load, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, INPUT);
pinMode(enable, OUTPUT);
digitalWrite(load, HIGH);
digitalWrite(enable, HIGH);
pinMode(load1, OUTPUT);
pinMode(clock1, OUTPUT);
pinMode(data1, INPUT);
pinMode(enable1, OUTPUT);
digitalWrite(load, HIGH);
digitalWrite(enable, HIGH);
pinMode(pot,INPUT);
Serial.begin(9600);
}

void loop()
{
unsigned int received=read_reg();
Serial.println(received,BIN);
int value=analogRead(pot);
if(value<255)
{
switch(received)
{
case 65534:
tone(speaker,131);
break;
case 65533:
tone(speaker,139);
break;
case 65531:
tone(speaker,147);
break;
case 65527:
tone(speaker,156);
break;
case 65519:
tone(speaker,165);
break;
case 65503:
tone(speaker,175);
break;
case 65471:
tone(speaker,185);
break;
case 65407:
tone(speaker,196);
break;
case 65279:
tone(speaker,208);
break;
case 65023:
tone(speaker,220);
break;
case 64511:
tone(speaker,233);
break;
case 63487:
tone(speaker,247);
break;
default:
noTone(speaker);
break;
}
}
else if(value>255&&value<512)
{
switch(received)
{
case 65534:
tone(speaker,262);
break;
case 65533:
tone(speaker,277);
break;
case 65531:
tone(speaker,294);
break;
case 65527:
tone(speaker,311);
break;
case 65519:
tone(speaker,330);
break;
case 65503:
tone(speaker,349);
break;
case 65471:
tone(speaker,370);
break;
case 65407:
tone(speaker,392);
break;
case 65279:
tone(speaker,415);
break;
case 65023:
tone(speaker,440);
break;
case 64511:
tone(speaker,466);
break;
case 63487:
tone(speaker,494);
break;
default:
noTone(speaker);
break;
}
}
else if(value>512&&value<768)
{
switch(received)
{
case 65534:
tone(speaker,523);
break;
case 65533:
tone(speaker,554);
break;
case 65531:
tone(speaker,587);
break;
case 65527:
tone(speaker,622);
break;
case 65519:
tone(speaker,659);
break;
case 65503:
tone(speaker,698);
break;
case 65471:
tone(speaker,740);
break;
case 65407:
tone(speaker,784);
break;
case 65279:
tone(speaker,831);
break;
case 65023:
tone(speaker,880);
break;
case 64511:
tone(speaker,932);
break;
case 63487:
tone(speaker,988);
break;
default:
noTone(speaker);
break;
}
}
else if(value>768)
{
switch(received)
{
case 65534:
tone(speaker,1046);
break;
case 65533:
tone(speaker,1108);
break;
case 65531:
tone(speaker,1175);
break;
case 65527:
tone(speaker,1245);
break;
case 65519:
tone(speaker,1319);
break;
case 65503:
tone(speaker,1397);
break;
case 65471:
tone(speaker,1480);
break;
case 65407:
tone(speaker,1568);
break;
case 65279:
tone(speaker,1661);
break;
case 65023:
tone(speaker,1760);
break;
case 64511:
tone(speaker,1865);
break;
case 63487:
tone(speaker,1976);
break;
default:
noTone(speaker);
break;
}
}
}

int read_reg()
{
digitalWrite(load,LOW);
delayMicroseconds(5);
digitalWrite(load,HIGH);
digitalWrite(clock,HIGH);
digitalWrite(enable,LOW);
byte upper=shiftIn(data,clock,MSBFIRST);
byte lower=shiftIn(data,clock,MSBFIRST);
digitalWrite(enable,HIGH);
int incoming=lower|(upper<<8);
return incoming;
}

Alright, some images of this project before showing a few videos of this project.

DSC08430
Overall setup on breadboard
DSC08429
Speaker
DSC08428
12 tact switches as piano keys

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.

Advertisements