Blog navigation

Latest posts

2441 Views
Picture of Arduino Piano With Push Button Switches
Picture of Arduino Piano With Push Button Switches

Created by : Haotian Ye

Overview:

This is a piano board with eight push button switches that allows you to play one octave (Do Re Mi Fa So La Si Do) and with this one octave you can try to play some songs you like. For this project there are some important knowledge you need to know before you start.

First, we need to know the frequencies of basic notes of a piano.

The frequencies are listed below:

Do – 261Hz

Re – 294Hz

Mi – 329Hz

Fa – 349Hz

So – 392Hz

La – 440Hz

Si – 493Hz

Do – 523Hz

Second, I will show you how to build the circuit by using the parts that can be bought from Lee’s Electronic Store. Finally,I will present and explain the code that need to be uploaded onto the Arduino board.

Parts You Will Need:

Arduino Uno R3 (Product ID: 10997)

USB A to B Cable M/M (Product ID: 29861)

10K Resistor * 8 (Product ID: 91516)

Different Colors of Tack Switch * 8 (Product ID: 3124, 31242, 31243, 31245, 31246)

Mini Speaker (Product ID: 41680)

Breadboard (Product ID: 106861)

Jumper Wires (Product ID: 21801)

Step 1: Step 1: Building the Circuit

Picture of Step 1: Building the Circuit
Picture of Step 1: Building the Circuit
Picture of Step 1: Building the Circuit
Picture of Step 1: Building the Circuit

First, insert all the push button switches and mini speaker on breadboard one by one and match them on one row. Then connect the pins of each push button switches to the ground. Second connect 10k resistors between positive power and the other pins of each push button switches. And, connect this column to pins 2-9 on Arduino Board. Also, connect the ground to the ground pin and positive power to 3.3v pin on Arduino. Finally, connect the mini speaker to the pin 10 on Arduino.

Step 2: Step 2: Code and Explanation

The code below is what I wrote

const int black = 2;
const int white = 3;
const int red = 4;
const int green = 5;
const int blue = 6;
const int black2 = 7;
const int green2 = 8;
const int red2 = 9;
const int speaker = 10;// Link all push button switches and speaker with arduino's signal pins
int frequency[ ] = {262, 294, 330, 349, 392, 440, 493, 523};// array contain all frequencies of one octave
void setup() {
// put your setup code here, to run once:
pinMode (black, INPUT);
pinMode (white, INPUT);
pinMode (red, INPUT);
pinMode (green, INPUT);
pinMode (blue, INPUT);
pinMode (black2, INPUT);
pinMode (green2, INPUT);
pinMode (red2, INPUT);
pinMode (speaker, OUTPUT);
tone (speaker, 2000);
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(black) == LOW)// when you press the "DO" push button switch
{tone(speaker,frequency[0],50);
delay (50);
noTone (speaker);}
else if (digitalRead(white) == LOW)// when you press the "RE" push button switch
{tone(speaker,frequency[1],50);
delay (50);
noTone (speaker);}
else if (digitalRead(red) == LOW)// when you press the "MI" push button switch
{tone(speaker,frequency[2],50);
delay (50);
noTone (speaker);}
else if (digitalRead(green) == LOW)// when you press the "FA" push button switch
{tone(speaker,frequency[3],50);
delay (50);
noTone (speaker);}
else if (digitalRead(blue) == LOW)// when you press the "SO" push button switch
{tone(speaker,frequency[4],50);
delay (50);
noTone (speaker);}
else if (digitalRead(black2) == LOW)// when you press the "LA" push button switch
{tone(speaker,frequency[5],50);
delay (50);
noTone (speaker);}
else if (digitalRead(green2) == LOW)// when you press the "SI" push button switch
{tone(speaker,frequency[6],50);
delay (50);
noTone (speaker);}
else if (digitalRead(red2) == LOW)// when you press the "DO" push button switch
{tone(speaker,frequency[7],50);
delay (50);
noTone (speaker);}
else// when you press nothing
noTone (speaker);
}

First, we have to declare all the push button switches and speaker as the pin from 2 to 10 on Arduino. Each switch represent for one note. Then, use one array to put all the frequencies inside. Next, If and else statement is what I use to let Arduino know which push button switch I press.

Finally, connect your Arduino board to your computer or laptop with usb A to B cable. Before you upload your code, you still need to download Arduino software and do some default setting. Select Tools -andgt; Board -andgt; Arduino/Genuino Uno; Then we need to select the communication port connected to the Arduino board. Select Tools -andgt; Port, then whichever port name is labeled "(Arduino/Genuino Uno)." Then, you can upload the code to Arduino board.

Step 3: Video Demonstration