Group members: Aizhan and Darmen
Concept:
As we began planning how to present our musical instrument and the sound produced by the buzzer while using both digital and analog sensors, we decided to use a button as our digital sensor and a distance-measuring sensor as our analog sensor. The main concept is that the distance sensor detects how far an object or hand is, and based on that distance, it produces different notes and sounds through the buzzer. When the button is pressed, the system pauses for 300 milliseconds (0.3 seconds) and temporarily stops reading distance values, effectively muting the instrument. Pressing the button again reactivates the sensor, allowing the instrument to continue playing notes according to the object’s position. This simple toggle system makes it easy to control when the instrument is active, giving users time to experiment with the sound and movement.
Arduino Setup and Demonstration:
Schematic Diagram:

Setup:

Coding:
if (sensorActive) {
int distance = getDistance();
Serial.println(distance);
if (1 < distance && distance < 5) {
tone(BUZZER, NOTE_C4);
} else if (5 < distance && distance < 10) {
tone(BUZZER, NOTE_D4);
} else if (10 < distance && distance < 15) {
tone(BUZZER, NOTE_E4);
} else if (15 < distance && distance < 20) {
tone(BUZZER, NOTE_F4);
} else if (20 < distance && distance < 25) {
tone(BUZZER, NOTE_G4);
} else if (25 < distance && distance < 30) {
tone(BUZZER, NOTE_A4);
} else if (30 < distance && distance < 35) {
tone(BUZZER, NOTE_B4);
} else {
noTone(BUZZER);
}
}
}
float getDistance() {
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calculatedDistance; //variable to store the distance calculated from the echo time
//send out an ultrasonic pulse that's 10ms long
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
echoTime = pulseIn(ECHO_PIN, HIGH); //pulsein command to see how long it takes for pulse to bounce back to sensor
calculatedDistance = echoTime / 55.2; //calculate distance of object that reflected the pulse in cm
return calculatedDistance;
}
We would say the highlight of the coding would be the part where the distance-measuring sensor interacts with the buzzer to produce different musical notes based on how close or far an object is. We were excited to experiment with the distance-measuring sensor and explore how it could be programmed to produce sound through the buzzer. To get better understanding of the integrating notes we referred to Arduino tutorials.
In the code above, the sensor continuously measures the distance of an object and converts the time it takes for the sound wave to bounce back into centimeters. Depending on the measured distance, the buzzer plays different musical notes, creating a simple melody that changes as the object moves closer or farther away.
Reflection:
We enjoyed experimenting with the distance-measuring sensor as it taught us how precise sensor readings can be transformed into meaningful outputs like sound, and combining it with the button control helped us manage the instrument’s activation smoothly. For future improvements, we would like to expand the range of notes to create more complex melodies and add LED lights that change color with the pitch to make the instrument more visually engaging. We could also experiment with different sensors, such as touch or motion sensors, to add more layers of interactivity. Finally, refining the accuracy and response speed of the sensor would make the sound transitions smoother and enhance the overall experience.