Arduino Based Piano

Hey guys, today we will make an electronic Arduino based piano (E-Piano). It is a simple piano that plays the basic musical notes (Do Re Me Fa So La Si). I have added a custom Super Mario Bros Melody to the E-Piano which was done by PrinceTronics; a button is assigned to played the custom melody. You can add your own custom made melodies if you want.
Let's get started and see how can we make our e-piano.
This project was made in Fablab Dhahran.
Parts Required
You will need the following parts to make the electronic circuit:
- Arduino UNO
- Speaker 1W
- 8 LEDs (Any color of your choice, I have used blue LEDs)
- 8 Limit Switches
- 8 10k Ohm Resistor
- Breadboard
- 9V Battery Connector
- 9V Battery
- [Optional] Switch
You will need a 3D printer to make the external design of the E-Piano.
Electronic Circuit

The circuit is simple to make, before making it, just connect one button, the speaker, and one LED to the Arduino to see if they work. Use a breadboard to connect all the mentioned parts together.
- Connect the GND of the Arduino to the (-) pin of the Breadboard
- Now connect a 10K Ohm resistor to a pin of the button
- Connect the opposing pin of the button to a signal pin in the Arduino (Digital Pin# 4)
- [Optional] Connect the cathode of an LED to the GND of the breadboard and the anode to digital pin #13.
- Repeat the steps 7 more times to come up with a simple circuit which has buttons that are connected to the Arduino, based on these button we will select frequencies from the "pitches.h" library.
- [Optional] Cut the negative wire from the battery to the GND of the Arduino and connect a switch to it; so you can switch your E-piano on or off.
- Finally, connect the speaker's positive lead to the PWM pin #3 and the negative lead to GND.
After you have connected your first button only, use the following Arduino Button (Example) Code to test if your circuit is working fine or not.
The figure demonstrates circuit schematic and how each button is connected to the Arduino. Your finally circuit should look similar to it. You can also have LEDs added to each button.
/* Button
Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2.
The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board attached to pin 13.
created 2005 by DojoDave modified 30 Aug 2011 by Tom Igoe
This example code is in the public domain.
<a href="http://www.arduino.cc/en/Tutorial/Button"> http://www.arduino.cc/en/Tutorial/Button </a> */
const int buttonPin = 4; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin
// variables will change: int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }
void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
Programming
After you have your circuit connected, it is time to upload the code to the Arduino, the code includes the "pitches.h" Library, So make sure you keep the library within the same folder as the E-Piano code folder.
The code mainly uses the ClickButton() method to determine which button has been pressed to play a certain frequency. The frequencies of Do Re Mi Fa Sol La Si are assigned in the melody1[] array. They are not 100 percent accurate so you can keep calibrating them until you get a better result. You can post your melody1 array in the comments section below if you want to share a better calibrated musical notes.
The tone method is used to send the PWM to the speaker, it passes two parameters, the pin of the speaker and the pitch. You can use the noTone(Speaker Pin #) to end any notes playing.
Upload the following code to the Arduino to use your E-Piano. Make sure you download the "pitches.h" library and keep it in the same folder as your code.
/* Developed in Fablab DHAHRAN - Mado<br> * www.fablabdhahran.org * * Special thanks to PrinceTronics for their SuperMarioBros Theme melody * http://www.princetronics.com/supermariothemesong/ */
#include "pitches.h" #define melodyPin 3
int Do = 4; int Re = 5; int Mi = 6; int Fa = 7; int Sol = 8; int La = 9; int Si = 10; int Marioo = 11;
int LED_SYNC = 13;
const int Delay = 120;
int Speaker = 3;
int melody1[] = {NOTE_C4, NOTE_D4,NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
int melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0 }; //Mario main them tempo int tempo[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, }; //Underworld melody int underworld_melody[] = { NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_AS3, NOTE_AS4, 0, 0, NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_AS3, NOTE_AS4, 0, 0, NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, NOTE_DS3, NOTE_DS4, 0, 0, NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, NOTE_DS3, NOTE_DS4, 0, 0, NOTE_DS4, NOTE_CS4, NOTE_D4, NOTE_CS4, NOTE_DS4, NOTE_DS4, NOTE_GS3, NOTE_G3, NOTE_CS4, NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4, NOTE_GS4, NOTE_DS4, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_GS3, 0, 0, 0 }; //Underwolrd tempo int underworld_tempo[] = { 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 6, 18, 18, 18, 6, 6, 6, 6, 6, 6, 18, 18, 18, 18, 18, 18, 10, 10, 10, 10, 10, 10, 3, 3, 3 };
void setup(void) { Serial.begin(9600); pinMode(LED_SYNC, OUTPUT); pinMode(melodyPin, OUTPUT);//buzzer pinMode(Do, INPUT); pinMode(Re, INPUT); pinMode(Mi, INPUT); pinMode(Fa, INPUT); pinMode(Sol,INPUT); pinMode(La, INPUT); pinMode(Si, INPUT); pinMode(Marioo,INPUT); }
void loop() { ClickButton(); }
int song = 0; void sing(int s) { song = s; if (song == 2) { Serial.println(" 'Underworld Theme'"); int size = sizeof(underworld_melody) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / underworld_tempo[thisNote]; buzz(melodyPin, underworld_melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: buzz(melodyPin, 0, noteDuration); } } else { Serial.println(" 'Mario Theme'"); int size = sizeof(melody) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / tempo[thisNote]; buzz(melodyPin, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: buzz(melodyPin, 0, noteDuration); } } } void buzz(int targetPin, long frequency, long length) { digitalWrite(13, HIGH); long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions //// 1 second's worth of microseconds, divided by the frequency, then split in half since //// there are two phases to each cycle long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing //// multiply frequency, which is really cycles per second, by the number of seconds to //// get the total number of cycles to produce for (long i = 0; i < numCycles; i++) { // for the calculated length of time... digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram delayMicroseconds(delayValue); // wait for the calculated delay value digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram delayMicroseconds(delayValue); // wait again or the calculated delay value } digitalWrite(13, LOW); }
void ClickButton(){ if(digitalRead(Do) == 1){ Serial.println("DO"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[0]); delay(Delay); }else if(digitalRead(Re) == 1){ Serial.println("RE"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[1]); delay(Delay); }else if(digitalRead(Mi) == 1){ Serial.println("MI"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[2]); delay(Delay); }else if(digitalRead(Fa) == 1){ Serial.println("FA"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[3]); delay(Delay); }else if(digitalRead(Sol) == 1){ Serial.println("SOL"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[4]); delay(Delay); }else if(digitalRead(La) == 1){ Serial.println("La"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[5]); delay(Delay); }else if(digitalRead(Si) == 1){ Serial.println("Si"); digitalWrite(LED_SYNC, HIGH); tone(Speaker, melody1[6]); delay(Delay); }else if(digitalRead(Marioo) == 1){ Serial.println("Marioo"); digitalWrite(LED_SYNC, HIGH); sing(1); sing(2); delay(Delay); }else{ digitalWrite(LED_SYNC, LOW); noTone(Speaker); } }
Downloads
3D Design

You can come up with your own design, but you can use our design from Fablab Dhahran if you have a 3D printer or if you visit any nearby fablabs. The ".stl" file is attached, after you print the Piano you can mount your circuit inside the box and let the buttons appear from the front side of it.
The stl files are divided into 3 prints, Piano.stl has the base of the E-Piano, Piano_Lid.stl has the top cover of the E-Piano, and Piano_Keys.stl has the button covers (if you use limit switches this would be a perfect choice)
The top part of the E-Piano is engraved to allow the speaker we used to be mounted on top as well. If your speaker has a different shape, modify the ".stl" file before printing it.
Enjoy the nostalgic melodies with your E-Piano :D