Concept
The concept behind this project is inspired by the theremin, one of the earliest electronic musical instruments. Unlike the traditional theremin that uses electromagnetic fields, this version employs an ultrasonic sensor to detect hand movements and converts them into musical notes from the A minor pentatonic scale.
Schematic
Code snippet
The implementation uses an Arduino with an ultrasonic sensor (HC-SR04) and a piezo buzzer. Here’s the key components of the code:
const int SCALE[] = { 147, 165, 196, 220, 262, 294, 330, 392, 440, 523, 587, 659, 784, 880, 1047, 1175, 1319, 1568, 1760, 2093, 2349 };
The scale array contains frequencies in Hertz, representing notes in the A minor pentatonic scale, spanning multiple octaves. This creates a musical range that’s both harmonious and forgiving for experimentation.
The system operates in two phases:
Calibration Phase
void calibrateSensor() { unsigned long startTime = millis(); while (millis() - startTime < CALIBRATION_TIME) { int distance = measureDistance(); maxDistance = max(maxDistance, distance); } }
Performance Phase
void loop() { int currentDistance = measureDistance(); int mappedNote = map(currentDistance, MIN_DISTANCE, maxDistance, SCALE[0], SCALE[SCALE_LENGTH - 1]); int nearestNote = findNearestNote(mappedNote); tone(PIEZO_PIN, nearestNote); delay(30); }
Demo
When powered on, the instrument takes 5 seconds to calibrate, determining the maximum distance it will respond to. Moving your hand closer to the sensor produces higher pitches, while moving away produces lower ones. The pentatonic scale ensures that all notes work harmoniously together, making it easier to create pleasing melodies.
Reflection and Future Improvements
Current Limitations:
- The response time has a slight delay due to sensor readings
- Sound quality is limited by the piezo buzzer
- Only supports single notes at a time
Potential Enhancements:
- Replace the piezo with a better quality speaker
- Add an amplifier circuit for improved sound output
- Incorporate multiple sensors for more control dimensions