Distance Sensor Arduino Piano
Hi my name is Joshua Kim and this is my final project. For this project, I have made a Arduino Distisensoring Piano. This piano uses push buttons with each push button connecting to a different arduino pin, and through that arduino's code, controls a different buzz noise on the buzzer, making it a working piano. I added a distisensor to change the pitch of the piano the further your hand is from the piano. Pretty straight foward. Now that you know how it works, lets get to the supplies.
Supplies
For the supplies, you will need:
- 2x breadboards (or 1 long one)
- jumper wires
- arduino uno
- push buttons
- buzzer
- distisensor
Wiring Main Piano (push Buttons and Wires)
For wiring, the tinkercad and schematic of this project can help with connecting everything together.
For each push button, the left side should be connected to the ground, while the right side should be connected to the arduino pins
The buttons should be connected from D10 all the way to D4
Each of this pins control each button, which controls a specific pitch and tone so that each button has it's own unique sound
Buzzer's power should be connected to D11, while it's ground lien should be on the ground next to it.
Wiring on Distance Sensor
Here you can see the wiring for the second part, which is the distance sensor next to the Piano itself.
The distance sensor should have its power line directly on the 5v of the arduino, with trig on D3, and echo on D2 with ground on the main ground line on the arduino.
Very simple itself.
Image of Arduino Pin Connections
You can simply see here to double check any errors or misplacements.
Overall Coding Step by Step
//Arduino Piano
#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
void none(){
#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
//normal key notes
}
void one(){
#define T_C 562
#define T_D 594
#define T_E 630
#define T_F 649
#define T_G 692
#define T_A 740
#define T_B 793
//key notes if something before 5 inches
}
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 Buzz = 11;
const int LED = 13;
int trig = 3;
int echo =2;
void setup()
{
Serial.begin(9600);
for (int i =7; i<14; i++)
pinMode( i,OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
pinMode(LED, OUTPUT);
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);
digitalWrite(LED,LOW);
}
void loop()
{
long duration, inches;
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
inches = duration /74 /2;
Serial.println(inches);
delay(500);
if (inches < 5)
{
one();
}
else
{
none();
//piano keys are same
}
while(digitalRead(C) == LOW)
{
tone(Buzz,T_C);
digitalWrite(LED,HIGH);
}
while(digitalRead(D) == LOW)
{
tone(Buzz,T_D);
digitalWrite(LED,HIGH);
}
while(digitalRead(E) == LOW)
{
tone(Buzz,T_E);
digitalWrite(LED,HIGH);
}
while(digitalRead(F) == LOW)
{
tone(Buzz,T_F);
digitalWrite(LED,HIGH);
}
while(digitalRead(G) == LOW)
{
tone(Buzz,T_G);
digitalWrite(LED,HIGH);
}
while(digitalRead(A) == LOW)
{
tone(Buzz,T_A);
digitalWrite(LED,HIGH);
}
while(digitalRead(B) == LOW)
{
tone(Buzz,T_B);
digitalWrite(LED,HIGH);
}
noTone(Buzz);
digitalWrite(LED,LOW);
}
Explanaition of Code
Constants and Definitions
#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
This section defines the frequencies for each musical note (C, D, E, F, G, A, and B) in Hz. These frequencies will be used to generate the corresponding tones on the buzzer.
Function Definitions
void none(){
#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
//normal key notes
}
void one(){
#define T_C 562
#define T_D 594
#define T_E 630
#define T_F 649
#define T_G 692
#define T_A 740
#define T_B 793
//key notes if something before 5 inches
}
These two functions, none() and one(), redefine the frequencies for each musical note. The none() function sets the frequencies to the normal values, while the one() function sets them to higher values. These functions will be called based on the distance reading from the distance sensor.
Pin Definitions and Setup
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 Buzz = 11;
const int LED = 13;
int trig = 3;
int echo =2;
This section defines the pin numbers for the push buttons (C, D, E, F, G, A, and B), the buzzer (Buzz), the LED (LED), and the distance sensor (trig and echo).
void setup()
{
Serial.begin(9600);
for (int i =7; i<14; i++)
pinMode( i,OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
pinMode(LED, OUTPUT);
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);
digitalWrite(LED,LOW);
}
The setup() function initializes the serial communication, sets the pin modes for the push buttons, buzzer, LED, and distance sensor, and sets the initial state of the LED to LOW.
Main Loop
void loop()
{
long duration, inches;
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
inches = duration /74 /2;
Serial.println(inches);
delay(500);
This section of the loop() function reads the distance from the distance sensor using the pulseIn() function and calculates the distance in inches. The distance is then printed to the serial monitor.
if (inches < 5)
{
one();
}
else
{
none();
//piano keys are same
}
Based on the distance reading, this section calls either the one() or none() function to set the frequencies for the musical notes.
while(digitalRead(C) == LOW)
{
tone(Buzz,T_C);
digitalWrite(LED,HIGH);
}
while(digitalRead(D) == LOW)
{
tone(Buzz,T_D);
digitalWrite(LED,HIGH);
}
while(digitalRead(E) == LOW)
{
tone(Buzz,T_E);
digitalWrite(LED,HIGH);
}
while(digitalRead(F) == LOW)
{
tone(Buzz,T_F);
digitalWrite(LED,HIGH);
}
while(digitalRead(G) == LOW)
{
tone(Buzz,T_G);
digitalWrite(LED,HIGH);
}
while(digitalRead(A) == LOW)
{
tone(Buzz,T_A
Demonstration
Well, we've made it to the end! Here is a quick demonstration on the 2 modes the piano can switch from depending on where the distisensor is tracking in inches. Enjoy your project!