Mini Piano Arduino Project
I made a mini piano on an Arduino uno using a buzzer to create sound, a 7-segment to display the note being played, and a servo motor to display the notes on the staff
Supplies
Power and Ground
Connect the power and ground accordingly as shown in the image above.
Black wire = GND
Red wire = Power
Buzzer/Piezo
Connect the piezo to pin 13 and connect the piezo to the ground. As shown in the image above
The purpose of the piezo is to play the sound of the notes.
Push Buttons
Connect the pushbuttons to pins 10-12. Connect the pushbuttons to power and ground using wires and resistors. As seen in the image above
The purpose of the pushbuttons is to play a note using the buzzer when pressed
Button1 = Pin 10
Button2 = Pin 11
Button3 = Pin 12
7 Segment
Connect the 7 segments accordingly. as seen in the image above
The purpose of the 7-segment is to display the note being played based on the push button pressed
a = Pin 7
b = Pin 8
c = PIn 4
d = Pin 3
e = Pin 2
f = Pin 6
g = Pin 5
Servo Motor
Connect the servo motor to power and ground through a capacitor, as well as connect it to pin 9 as shown in the image above.
The purpose of the servo motor is to display the note on the staff according to the pushbutton being pressed
The Code
Write the code as shown above connecting the pins according to your components.
The Code
int but1 = 10;
int but2 = 11;
int but3 = 12;
int buzzer = 13;
int a = 7;
int b = 8;
int c = 4;
int d = 3;
int e = 2;
int f = 6;
int g = 5;
#include <Servo.h>
Servo myServo;
void setup()
{
Serial.begin(9600);
myServo.attach(9);
//declare the button pins as input
pinMode(but1,INPUT);
pinMode(but2,INPUT);
pinMode(but3,INPUT);
//declare buzzer pin as output
pinMode(buzzer,OUTPUT);
//declare 7-seg as output
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
myServo.write(90);
}
void loop()
{
// read the value from buttons
int b1 = digitalRead(but1);
int b2 = digitalRead(but2);
int b3 = digitalRead(but3);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
if( b1 == 1 ){
tone(buzzer,300,100);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
for ( int angle =0; angle<90; angle++){
Serial.println(angle);
myServo.write(angle);
}
}
if( b2 == 1 ){
tone(buzzer,400,100);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if( b3 == 1 ){
tone(buzzer,500,100);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
for ( int angle=170; angle>90; angle--){
Serial.println(angle);
myServo.write(angle);
}
//put a short delay for a nice pitch
delay(10);
}
}
Picture of the Finished Project
You have now completed your mini Piano!
Here is a video of how the toy should work: Video