Simple piezo piano using Arduino

It’s time to make some noise, ok musicians friends who have an Arduino board and some time to spend, I’ll show you how to build a very simple piano using a piezo for playing sound.

Our piano is gonna have the basec 8 notes : ‘c’  , ‘d’,  ‘e’,  ‘f’,  ‘g’, ‘a’,  ‘b’,  ‘C’ so that’s what we need to create this :

– Arduino board

– Piezo (for sound output)

– 8 buttons (one for every note)

– 8 Resistors (for the buttons)

– 10 Cables

The circuit connectivity :

As you see there are 8 buttons / notes in a row for  ‘c’  , ‘d’,  ‘e’,  ‘f’,  ‘g’, ‘a’,  ‘b’,  ‘C’ and when you press one you gonna hear the sound (within frequency creation) from the piezo.

The Arduino code is really simple, it just reads the signals from the buttons and it creates a frequency to produce sound with the piezo.

The code follows bellow :

int button_C = 2;
int button_D = 3;
int button_E = 4;
int button_F = 5;
int button_G = 6;
int button_A = 7;
int button_B = 8;
int button_Cup = 9;

int speaker = 13;

int buttonstate_C = 0;
int buttonstate_D = 0;
int buttonstate_E = 0;
int buttonstate_F = 0;
int buttonstate_G = 0;
int buttonstate_A = 0;
int buttonstate_B = 0;
int buttonstate_Cup = 0;

//NOTES         'c'  , 'd',  'e',  'f',  'g', 'a',  'b',  'C'
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; //freq
int Cur_tone = 0;

void setup()
{
  pinMode(button_C, INPUT);
  pinMode(button_D, INPUT);
  pinMode(button_E, INPUT);
  pinMode(button_F, INPUT);
  pinMode(button_G, INPUT);
  pinMode(button_A, INPUT);
  pinMode(button_B, INPUT);
  pinMode(button_Cup, INPUT);

  pinMode(speaker, OUTPUT);
}

void loop()
{
	buttonstate_C = digitalRead(button_C);
	buttonstate_D = digitalRead(button_D);
	buttonstate_E = digitalRead(button_E);
	buttonstate_F = digitalRead(button_F);
	buttonstate_G = digitalRead(button_G);
	buttonstate_A = digitalRead(button_A);
	buttonstate_B = digitalRead(button_B);
	buttonstate_Cup = digitalRead(button_Cup);

	if((buttonstate_C == HIGH) || (buttonstate_E == HIGH) || 
		(buttonstate_G == HIGH) || (buttonstate_D == HIGH) || 
		(buttonstate_F == HIGH) || (buttonstate_A == HIGH) || 
		(buttonstate_B == HIGH) || (buttonstate_Cup == HIGH) )
	{ 
		if (buttonstate_C == HIGH)
		{
			Cur_tone = tones[0];
		} 
		if (buttonstate_E == HIGH)
		{
			Cur_tone = tones[1];
		}
		if (buttonstate_G == HIGH)
		{
			Cur_tone = tones[2];
		}
		if (buttonstate_D == HIGH)
		{
			Cur_tone = tones[3];
		}
		if (buttonstate_F == HIGH)
		{
			Cur_tone = tones[4];
		}
		if (buttonstate_A == HIGH)
		{
			Cur_tone = tones[5];
		}
		if (buttonstate_B == HIGH)
		{
			Cur_tone = tones[6];
		}
		if (buttonstate_Cup == HIGH)
		{
			Cur_tone = tones[7];
		}

		digitalWrite(speaker, HIGH);
		delayMicroseconds(Cur_tone);
		digitalWrite(speaker, LOW);
		delayMicroseconds(Cur_tone);
	}
	else //in case no button is pressed , close the piezo
	{
		digitalWrite(speaker, LOW);
	}

}

As you see the frequencies for the notes are :

1915, 1700, 1519, 1432, 1275, 1136, 1014, 956

when you write this value of electricity into a piezo it produces those notes.

Advertisements

16 thoughts on “Simple piezo piano using Arduino

  1. Hi
    Thanks for posting the code here.

    Could you please explain why you use resistors?
    Which resistor should I choose when buliding this Piano?

    • Hello Matthias and thanks for the comment,
      Resistors are needed on button circuits and they called “Pull-up resistor”, they are used for device safety reasons.
      The values of the resistors I used is : 330 Ohms 5%, as you can see it from the colour code on the image also.
      Hope that helps!

  2. Yes of course,
    The calculation of the tones is made following the mathematical operation:

    timeHigh = period / 2 = 1 / (2 * toneFrequency)

    where the different tones are described as in the table:

    note frequency period timeHigh
    c 261 Hz 3830 1915
    d 294 Hz 3400 1700
    e 329 Hz 3038 1519
    f 349 Hz 2864 1432
    g 392 Hz 2550 1275
    a 440 Hz 2272 1136
    b 493 Hz 2028 1014
    C 523 Hz 1912 956

  3. I’ve been trying to get my piano to work for a while; so far, I’ve only gotten 3 keys to work despite using all new components, making sure everything is in, so on. Do you guys know whats happening?

  4. Hi, really thanks for providing this fun project, and I’m making this right now, but I met with a issue, I didn’t change the code at all and wiring like the graph you provided, but when I pressed the button, some of them play a different tone, for example when I pressed the second button which should play D, but it actually play E. And I tried to exchange them, but then The button was playing G, it is really wired and I don’t know how. Can u help me to figure it out? Thanks again!

    Jiahao

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s