The "Ultrasonic Interactive Piano" is a touchless electronic musical instrument. It uses an HC-SR04 ultrasonic sensor to measure the distance of the player's hand. That distance is translated into specific musical notes (C4 to C5). Simultaneously, an RGB LED dynamically changes its color based on the hand's proximity, offering a rich multi-sensory (audio and visual) interaction.
Map continuous physical data (distance) into discrete digital outputs (musical notes).
Understand the concept of Pulse Width Modulation (PWM) to create smooth RGB color gradients.
Implement code optimization techniques to reduce sensor noise and avoid repetitive audio triggers.
Arduino Uno
RGB LED
Buzzer
HC-SR04 Sensor
Breadboard & Wires
Metal Path
Resistors (220)
Ultrasonic Sensor (HC-SR04)
VCC ➡️ 5V (Arduino)
TRIG ➡️ Pin 2 (Arduino)
ECHO ➡️ Pin 3 (Arduino)
GND ➡️ GND (Arduino)
Passive Buzzer:
Positive (+) ➡️ Pin 9 (Arduino)
Negative (-) ➡️ GND (Arduino)
RGB LED (Common Cathode):
Red Pin ➡️ Pin 5 (via 220Ω resistor)
Green Pin ➡️ Pin 6 (via 220Ω resistor)
Blue Pin ➡️ Pin 11 (via 220Ω resistor)
GND (Longest leg) ➡️ GND
Sensing: The Arduino triggers the Ultrasonic sensor to calculate distance in centimeters.
Range Limiting: If the hand is too far (over 40cm) or too close (under 2cm), the buzzer and LED turn off.
Mapping & Tone Play: The system maps the valid distance into 8 note indexes. Sound plays only if the hand moves to a new note zone to prevent overlapping noise.
Visual Gradient: At the same time, PWM controls the RGB pins to shift colors from green (far) to red (very close).
1. Simulation: Build the circuit on TinkerCad to verify the math logic behind distance mapping.
2. Assembly: Place the Arduino, Ultrasonic sensor, and RGB LED on the breadboard.
3. Power & Sensor: Connect the sensor's VCC and GND, and wire TRIG and ECHO to digital pins.
4. Outputs: Wire the buzzer and RGB channels via 220Ω resistors to the PWM digital pins.
5. Upload & Calibrate: Upload the sketch and use the Serial Monitor to fine-tune the distance limits.
Stage 1: Distance Sensing & Basic Beep
The project began with measuring raw distances and making the buzzer beep continuously without specific notes.
Stage 2: Notes Array & Noise Reduction
Specific frequencies (notes) were introduced, and logic was coded so that a sound plays only when the note actually changes, removing heavy overlap noise.
Stage 3: RGB Visual Integration
An RGB LED was added to output specific solid colors corresponding to each specific music zone.
Stage 4: Full Gradient Mapping (Final Version)
The final logic implemented a full color-gradient shift using map() for the RGB values, blending colors perfectly as the hand moves.
Power Conditioning: Adding a small 100nF capacitor near the buzzer to eliminate PWM power flickers in the LED.
Audio Amplification: Using an NPN transistor to drive a larger 8-ohm speaker for louder and richer piano sounds.
Scalability: Expanding the distance mapping to cover two full octaves (16 notes).