This is a fun instrument that lets you play simple tunes by waving your hands in the air!!
The quarantine always makes us crave some fun and so we thought of new things to do with the few components we were stuck with at home.
What better than some music!!!
So here we are, with a simple setup for playing tunes with your hands! Have fun!!
The basic idea is to be able to generate different frequencies of sound based on the position of your hand, which is sensed using an ultra-sonic sensor.
Connect your circuit as per the schematic given below. Paste the given code in your Arduino IDE and upload it.
Keep your sensor in an open space such that there are no obstacles in front of it for at least a meter. Place your palm or any other obstacle in front of the sensor to obtain varying frequencies.
Spatial Acoustics: The Ultrasonic Theremin
Playing a piano requires pressing physical buttons locked into explicit octaves. The Simple Hand Controlled Instrument replicates the bizarre and terrifying Russian "Theremin" from the 1920s entirely utilizing digital silicon! Instead of radio waves, this instrument utilizes explosive bursts of Ultrasonic sound. The Arduino generates an acoustic wall out of an HC-SR04 sensor. As the user's empty hand approaches or retreats in mid-air, the Arduino violently intercepts the shifting distance metrics and instantly executes complex mathematical equations, scaling the physical centimeters natively into pure audible frequency sweeps via a Piezo Buzzer!
Converting Centimeters into Frequencies (map())
The core engine relies heavily upon translating spatial metrics (e.g., 5cm to 30cm) into audible Hertz (e.g., 200Hz to 1500Hz).
- The SR04 fires its 40Khz pulse, extracting the distance variable.
- The Arduino utilizes its massive built-in
map()mathematics function! int note = map(distance, 5, 30, 200, 1500);- The moment the distance drops below 5cm or goes past 30cm, the Arduino must explicitly kill the note to prevent horrifying static anomalies (
noTone()).
int distance = getDistance(); // Returns the exact CM measurement!
if (distance >= 5 && distance <= 30) {
// Constrain limits to prevent bizarre glitches!
int rawFreq = map(distance, 5, 30, 261, 1046); // Map CM into Middle C up to High C!
// Fire the hardware Timer array to generate the explicit square wave Hz!
tone(BUZZER_PIN, rawFreq);
} else {
// Silence! Hand is outside the play area!
noTone(BUZZER_PIN);
}
// Intense delay limits the math loop so the tone isn't constantly stuttering!
delay(40);
In the given code the sounds are emitted at a fixed time interval for a fixed duration. You can play around with this by changing the value of the variables 'tim' and 'space'.
Also, play around with the way the frequency is calculated from the distance. Here it is:
tones =20+distance*5;
Overcoming Square Wave Audio Distortion
The Arduino tone() function produces an incredibly harsh, aggressive, mathematically perfect Square Wave. It sounds like a cheap 1980s microwave alarm, not a smooth violin!
- To soften the audio, you use a physical Low-Pass Filter Capacitor!
- By violently wiring a
0.1µFor1µFElectrolytic Capacitor directly across the positive and negative legs of the Piezo or 8-Ohm speaker, it physically absorbs the ultra-sharp high-frequency electric spikes, rounding off the harsh edges of the Square Wave into something resembling a pseudo-Sine Wave naturally!
Acoustical Hardware Matrices
- Arduino Uno/Nano (Executing the massive hardware frequency timers continuously).
- HC-SR04 Ultrasonic Sensor (Must face unobstructed completely upwards or outwards in empty space).
- Passive Piezo Buzzer or 8-Ohm Mini Speaker (Wait! An Active Buzzer will FAIL completely; it only makes a single predetermined annoying beep! You absolutely MUST use a "Passive" Buzzer which acts like an empty speaker capable of playing varying frequencies!).
- Low-Pass Filter Capacitor (Usually completely optional, but critical for professional synthesizer clarity).
To see the distance of the obstacle measured by the sensor you can add the following code to void setup.
Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Project by theriveroars"); // print some text in Serial Monitor to indicate setup
And the following code in void loop.
Serial.print("Distance to obstacle= ");
Serial.print(distance);
Serial.println(" cm");
Thus you can view the distance in centimetres.
Have fun with this project and include multiple speakers and sensors to create more complex and fun instruments.
Keep learning!!