Introduction: Musical Instrument Using Arduino + Ultrasonic Distance Sensor

How to make a musical instrument using an Arduino and a HC-SR04 ultrasonic distance sensor.

The video embedded shows a demonstration. If that link doesn't work, here's an alternative link to the same video:

Arduino Musical Instrument - YouTube

NOTE: I used an obsolete Iteaduino Lite instead of an Arduino Uno, but an Uno will work better. If you have any issues, let me know in the comments and I will do my best to be tech support!

Step 1: Parts Needed

Arduino UNO R3 with USB Cable

http://ebay.to/2pPOQ6A

Arduino Jumper Cables - Male to Female Sockets

http://ebay.to/2qxODEA

Wire Stripping Tool

http://ebay.to/2qxRBss

HC-SR04 Ultrasonic Distance Sensor

http://ebay.to/2pY71mH

Small Speaker -

http://ebay.to/2rw5t4A

We used one salvaged from broken toy, but the speaker from the link should work too.

It is fun to take things apart to figure out how they work!

Small Speaker Option 2-

http://ebay.to/2pYbrtU

(Soldering Required for that one.)

Computer with Arduino IDE app installed - Get the IDE from www.arduino.cc (Donation not required.)

http://bit.ly/1R2xniM

CH340G Driver (so the Arduino can talk to the computer. This is not needed for official Arduino boards.)

http://bit.ly/1r4PF7l

NOTE: The links are for the fastest shipping speed to the United States.

If you want to get these parts cheaper from china but with roughly 3 to 5 weeks shipping, search for them on Ebay without the "U.S. ONLY" filter.

Step 2: Install the Driver

Once you have downloaded the file, unzip the install file and start the install process. Once this is done, restart your computer.

If you are using a Mac and running Yosemite, for the drivers to work for you, you will have to do this before you restart your mac:

1. Open terminal

2. Write the following (just copy from here):

sudo nvram boot-args="kext-dev-mode=1"
Do not mess up here. It may ruin your Mac if you write the wrong code.

3. Once you have pressed enter, terminal will prompt you for your user password. Insert this and press enter. 4. Once done, restart your computer.

Step 3: Get the Code

Open up the Arduino IDE you downloaded and installed earlier.

Either download the attached file "piano.ino" and open it with the Arduino IDE app, or just paste the below indentical code there:

//musical instrument for HC-SR04 (4 pin) Ultrasonic Sensor
void setup() {
  pinMode (2,OUTPUT);//attach pin 2 to vcc
  pinMode (5,OUTPUT);//attach pin 5 to GND
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
digitalWrite(2, HIGH);
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(3, OUTPUT);// attach pin 3 to Trig
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  digitalWrite(3, HIGH);
  delayMicroseconds(5);
  digitalWrite(3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (4, INPUT);//attach pin 4 to Echo
  duration = pulseIn(4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
  
  int toneToPlay = 0;
  
  if(cm <= 160 && cm > 140)
  {
     toneToPlay = 523; 
  }
  if(cm <= 140 && cm > 120)
  {
     toneToPlay = 493; 
  }
  if(cm <= 120 && cm > 100)
  {
     toneToPlay = 440; 
  }
  if(cm <= 100 && cm > 80)
  {
     toneToPlay = 392; 
  }
  if(cm <= 80 && cm > 60)
  {
     toneToPlay = 349; 
  }
  if(cm <= 60 && cm > 40)
  {
     toneToPlay = 329; 
  }
  if(cm <= 40 && cm > 20)
  {
     toneToPlay = 294; 
  }
  if(cm <= 20)
  {
     toneToPlay = 261; 
  }
  
  if (toneToPlay == 0)
  {
    noTone(11);
  }
  else
  {
    tone(11, toneToPlay, 200);
  }
  
  //if you want the sound to just change based on distance, 
  //but not necessarily have frequencies that correspond 
  //to notes on a piano you can do this instead
  //tone(11, in*10, 200); 
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Step 4: Upload the Code to the Arduino Board

Plug the Arduino board into your computer using the USB cable.

In the Arduino IDE, click the little check mark button called "verify" to make sure the code compiles fine.

Under Tools>Board>, select "Arduino/Genuino Uno".

Under Tools>Port, select the port your Arduino Uno board is using. It may take some trial and error to figure out which one it is. If you click upload and it gives you an error, you probably selected the wrong port.

Click the rightwards facing arrow button called "upload" to upload the code to your arduino board.

It will say "Done Uploading" at the bottom when it is done.

Step 5: Assemble the Circuit

While the Arduino is unplugged from any power source:

1. Connect the pin labelled "VCC" on the sensor to the D2 on the arduino board.

2. Connect the pin labeled "TRIG" on the sensor to the D3 on the arduino board.

3. Connect the pin labeled "ECHO" on the sensor to the D4 on the arduino board.

4. Connect the pin labeled "GND" on the sensor to D5 on the arduino board.

5. Use the wire stripping tool to cut and strip the ends of the speaker wires to remove the insulation.

6. Cut two jumper cables in half and strip the ends with the metal pins sticking out.

7. Twist one of the stripped jumper wires to one of the stripped speaker wires.

8. Do the same for the other jumper wire piece and the other speaker wire.

9. Make sure the exposed wire does not touch the other exposed wire. You can wrap the twisted sections in tape for just in case.

10. Plug one of these wires into the GND socket on the arduino.

11. Plug the other wire into the D11 socket on the arduino.

Step 6: Test It Out!

Plug the arduino into a USB power source. Your computer, a USB wall charger, or a USB power bank.

As you move your hand or feet further away from the front of the sensor, the speaker will play tones that increase in pitch and timing.

The Ultrasonic Sensor sends out a high-frequency sound pulse and then times how long it takes for the echo of the sound to reflect back. The sensor has 2 openings on its front. One opening transmits ultrasonic waves, (like a tiny speaker), the other receives them, (like a tiny microphone).

The speed of sound is approximately 341 meters (1100 feet) per second in air. The ultrasonic sensor uses this information along with the time difference between sending and receiving the sound pulse to determine the distance to an object. It uses the following mathematical equation:

Distance = Time x Speed of Sound divided by 2

Time = the time between when an ultrasonic wave is transmitted and when it is received You divide this number by 2 because the sound wave has to travel to the object and back.

Step 7: That's All for Now!

We hope you like this instructable!

If you have made one, or played a song with it, record it and send us the link to the video in the comments!

Comments

About This Instructable

183views

7favorites

License:

Bio: We are two brothers who like to make things.
More by Maker Saga:3 Crazy Simple Autonomous Drone IdeasMakeshift Wave Powered Cooling MatBroken Toy Drone Hardware Hack
Add instructable to: