Arduino Serial Piano

34

1

Here is a simple Arduino piano that can be made from simple wiring components. This piano uses the notes C, D, E, F, G, A, B, C# by emitting different frequencies through the piezo speaker. This piano also has a serial monitor function that will display the notes being played at the time. Note that not all code is mine (simply a modified code from thelonelyprogrammer).

Supplies:

This project will require

  • 8 push-buttons (Gikfun 25 button pack on Amazon, CAD$11), these are used to activate notes.
  • 17 quick-connect wires
  • Arduino Uno R3 or Arduino Genuino (offbrand works too)
  • Piezo Speaker

Step 1: Assembly

The Assembly of this is simple, as viewed in the Fritzing diagram shown.

  1. Begin with placing the push-buttons (I used Gikfun push-buttons) on the breadboard at an equal distance from each other.
  2. Make connections from the grounds of each push-button to the negative row at the top of the breadboard.
  3. Connect a wire to the ground to the right end of the negative row and then to the ground on the microprocessor.
  4. Connect the corresponding wires at each pin on the processor to the correct button (use diagram for reference)
  5. Connect Pin 11 to Piezo and then Piezo to ground.

Assembly complete!

Step 2: Code (File Also Attached)

//Arduino Piano
/* note frequency c 262 Hz d 294 Hz e 330 Hz f 349 Hz g 392 Hz a 440 Hz b 494 Hz C 523 Hz */ // these are defining what the notes used in the code are #define T_C 262 #define T_D 294 #define T_E 330 #define T_F 349 #define T_G 392 #define T_A 440 #define T_B 493 #define T_Cs 523
// this part links the defined notes to a pin
const int C = 10;
const int D = 9;
const int E = 8;
const int F = 7;
const int G = 6;
const int A = 5;
const int B = 4;
const int Cs = 3;
const int Buzz = 11;
const int LED = 13;
void setup()
{
  Serial.begin(9600);
  // this lets the notes be able to be displayed on screen when serial monitor is activated
    pinMode(C, INPUT);
    pinMode(D, INPUT);
    pinMode(E, INPUT);
    pinMode(F, INPUT);
    pinMode(G, INPUT);
    pinMode(A, INPUT);
    pinMode(B, INPUT);
    pinMode(Cs, INPUT);
  
  pinMode(LED, OUTPUT);
 //this is a basic function for activation
  pinMode(C, INPUT);
  digitalWrite(C,HIGH);
  
  pinMode(D, INPUT);
  digitalWrite(D,HIGH);
  
  pinMode(E, INPUT);
  digitalWrite(E,HIGH);
  
  pinMode(F, INPUT);
  digitalWrite(F,HIGH);
  
  pinMode(G, INPUT);
  digitalWrite(G,HIGH);
  
  pinMode(A, INPUT);
  digitalWrite(A,HIGH);
  
  pinMode(B, INPUT);
  digitalWrite(B,HIGH);
  pinMode(Cs, INPUT);
  digitalWrite(Cs,HIGH);
  
   digitalWrite(LED,LOW);
}
// in the loop, the buzz tone is enabled and while it is put in, it prints the note in serial monitor
void loop()
{
  while(digitalRead(C) == LOW)
  {
    tone(Buzz,T_C);
    digitalWrite(LED,HIGH);
    Serial.println("C");
  }
  while(digitalRead(D) == LOW)
  {
    tone(Buzz,T_D);
    digitalWrite(LED,HIGH);
    Serial.println("D");
  }
  while(digitalRead(E) == LOW)
  {
    tone(Buzz,T_E);
    digitalWrite(LED,HIGH);
    Serial.println("E");
  }
  while(digitalRead(F) == LOW)
  {
    tone(Buzz,T_F);
    digitalWrite(LED,HIGH);
    Serial.println("F");
  }
  while(digitalRead(G) == LOW)
  {
    tone(Buzz,T_G);
    digitalWrite(LED,HIGH);
    Serial.println("G");
  }
  while(digitalRead(A) == LOW)
  {
    tone(Buzz,T_A);
    digitalWrite(LED,HIGH);
    Serial.println("A");
  }
  while(digitalRead(B) == LOW)
  {
    tone(Buzz,T_B);
    digitalWrite(LED,HIGH);
    Serial.println("B");
  }
  
   while(digitalRead(Cs) == LOW)
  {
    tone(Buzz,T_Cs);
    digitalWrite(LED,HIGH);
    Serial.println("C#");
  }
  noTone(Buzz);
  digitalWrite(LED,LOW);
}

Step 3: Difficulties

Above is a video of the code working.

The only difficulties that you should come across here is perhaps wiring and if the code is being modified there could be an issue uploading again. It's a simple code for a simple, fun little DIY.

Have Fun!

Share

    Recommendations

    • DIY Summer Camp Contest

      DIY Summer Camp Contest
    • Sensors Contest

      Sensors Contest
    • Classroom Science Contest

      Classroom Science Contest

    Discussions

    1
    None
    audreyobscura

    24 days ago

    Awesome beginner’s project! How fun!