How to Make an Arduino Instrument (Using an Ultrasonic Sensor)
by samsneatprojects in Circuits > Arduino
4734 Views, 5 Favorites, 0 Comments
How to Make an Arduino Instrument (Using an Ultrasonic Sensor)
This instrument is made using an HC-SR04 Ultrasonic sensor, a Force Sensitive Resistor, and a Piezo Buzzer. You press the force sensor to begin playing music, and wave your hand in front of the ultrasonic sensor at various distances to play different notes. This video describes how it works and how to build it. Enjoy!
Parts List
- 1x Arduino Uno or Mega
- 1x Breadboard (I used half size)
- 1x Force Sensitive Resistor
- 1x HC-SR04 Ultrasonic Sensor
- 1x Piezo Buzzer
- 1x 10k Ohm Resistor
- 11x jumper wires
There are links to where you can buy several of these components in the description of the video
Wiring
The wiring is easiest if you take it component by component.
Ultrasonic:
Connect ground to the arduino ground, echo to a digital pin (I used 11), trig to a digital pin (I used 10), and VCC to 5V
Piezo Buzzer:
Connect the positive lead to a digital pin (I used 12) and the negative lead to ground
Force Sensitive Resistor:
I connected one lead directly to 5V, and the other lead directly to A0. Place a resistor after the A0 wire and connect it to ground
Code
int trig = 10;
int echo = 11;
long duration;
long distance;
int force;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW); //triggers on/off and then reads data
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) * .0344; //344 m/s = speed of sound. We're converting into cm
int notes[7] = {261, 294, 329, 349, 392, 440, 494}; //Putting several notes in an array
// mid C D E F G A B
force = analogRead(A0); //defining force as FSR data
if (distance < 0 || distance > 50 || force < 100) { //if not presed and not in front
noTone(12); //dont play music
}
else if ((force > 100)) { //if pressed
int sound = map(distance, 0, 50, 0, 6); //map distance to the array of notes
tone(12, notes[sound]); //call a certain note depending on distance
}
}
Substitute Parts
You may not have all of the parts that I listed in the video. Don't worry!
You can easily substitute several of them.
- HC-SR04 Ultrasonic Sensor = Potentiometer or Photoresistor(Light Sensor)
Both of these can be mapped the same way that the ultrasonic sensor is!
- Force Sensitive Resistor = Push Button
The only reason I use the force sensitive resistor is that I think it's more ergonomic. It practically has the same function as a button in this project so you can just use that instead! You could even just remove the press activation all together and trigger the instrument by just putting your hand in front of the ultrasonic sensor