Musical Keyboard with Arduino

Hello Guys! Today i am made a musical keyboard using an Arduino.

Read the following if you want to learn how to build it on your own.

Things you will need

  • Arduino Uno
  • Piezo buzzer
  • Various resistors with varying values.
  • 10 push button ( you can use as many as you want)
  • a Breadboard
  • Male to male jumper wires

Building the Circuit

Wire up your breadboard with power and ground. Connect one end of the piezo to ground. Connect the other end to pin 9 on your Arduino.

Place your switches on the breadboard as shown in the circuit. The arrangement of resistors and switches feeding into an analog input is called a resistor ladder. Connect the first one directly to power. Connect the rest to power through a resistor. Make sure they are all of varying resistance if you want piezo to produce different frequencies. Now connect all the switches’ output together in one junction. Connect this junction to ground with 10k-ohm resistor and also connect it to Analog In 0. Each of these act as a voltage divider.

I found the pic of a smaller circuit. I hope it explains it you how to make the circuit.

Time to Code

// Musical Keyboard with Arduino
// Description: Plays various notes.
// Author: Shabad Lamba
// Date: 05/01/2016
int notes[] = {240,270,300,320,360,400,450,480}; //notes array contation frequency on the notes
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int keyVal = analogRead(A0); // read the analof input from the push buttons
Serial.println(keyVal); // prints the value on the serial monitor
if(keyVal == 1023){ // check if the value is 1023
tone(9,notes[0]); // send output to the piezo to play the note number 0
}
//similary the if statements below check for the input value and plays the tone accordingly
else if(keyVal >= 960 && keyVal <= 980){
tone(9,notes[1]);
}
else if(keyVal >= 900 && keyVal <= 934){
tone(9,notes[2]);
}
else if(keyVal >= 80 && keyVal <= 100){
tone(9,notes[3]);
}
else if(keyVal >= 460 && keyVal <= 470){
tone(9,notes[4]);
}
else if(keyVal >= 880 && keyVal <= 900){
tone(9,notes[5]);
}
else if(keyVal >= 981 && keyVal <= 987){
tone(9,notes[6]);
}
else if(keyVal >= 935 && keyVal <= 940){
tone(9,notes[7]);
}
else{
noTone(9);
}
}

Thank you and have fun with it.

PS: If you have any query you can comment below.

Show your support

Clapping shows how much you appreciated Shabad Lamba’s story.