Mini Piano Arduino Project

by 710409 in Circuits > Arduino

499 Views, 2 Favorites, 0 Comments

Mini Piano Arduino Project

IMG_4226.jpg

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

Screen Shot 2023-01-25 at 11.52.53 PM.png

Connect the power and ground accordingly as shown in the image above.

Black wire = GND

Red wire = Power

Buzzer/Piezo

Screen Shot 2023-01-25 at 11.54.01 PM.png

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

Screen Shot 2023-01-25 at 11.57.23 PM.png

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

Screen Shot 2023-01-25 at 11.59.52 PM.png

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

Screen Shot 2023-01-26 at 12.00.50 AM.png

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

Screen Shot 2023-01-26 at 12.07.34 AM.png
Screen Shot 2023-01-26 at 12.07.51 AM.png
Screen Shot 2023-01-26 at 12.08.10 AM.png
Screen Shot 2023-01-26 at 12.08.27 AM.png
Screen Shot 2023-01-26 at 12.08.38 AM.png

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

IMG_4226.jpg

You have now completed your mini Piano!

Here is a video of how the toy should work: Video