Arduino Theremin
Learning Objectives
- Use an HC-SR04 ultrasonic sensor to measure distance without contact
- Map distance values to audio frequencies using
tone() - Understand the relationship between frequency, wavelength, and sound
- Apply
map()andconstrain()for range scaling
Overview
A theremin is a musical instrument played without physical contact. Your Arduino Theremin uses an HC-SR04 ultrasonic distance sensor to detect how far your hand is from the sensor, then converts that distance into a musical note played through a buzzer. Wave your hand up and down to play tunes!
Good to Know
The original theremin (invented by Léon Theremin in 1920) used radio-frequency oscillators that shifted frequency based on the player's hand capacitance. Your Arduino version achieves the same contactless effect using ultrasound ranging instead.
Components Required
| Component | Qty | Notes |
|---|---|---|
| Arduino Uno R3 | 1 | _ |
| HC-SR04 Ultrasonic Sensor | 1 | 5 V, range 2–400 cm |
| Passive Buzzer | 1 | Must be passive (not active) |
Good to Know
Use a passive buzzer, not an active one. An active buzzer produces only a fixed frequency when powered. A passive buzzer acts like a tiny speaker — it needs an oscillating signal from the Arduino's tone() function to produce different pitches.
How Ultrasound Ranging Works
The HC-SR04 works by:
- Emitting a 40 kHz ultrasonic pulse from the Trig pin
- Waiting for the echo to return to the Echo pin
- Measuring the pulse duration — longer duration = greater distance
Formula: Distance (cm) = Duration (µs) / 58
This is derived from: Distance = (Speed of Sound × Time) / 2
where speed of sound ≈ 343 m/s at 20 °C.
Circuit Wiring
| HC-SR04 Pin | Arduino Pin | |-------------|-------------| | VCC | 5 V | | GND | GND | | Trig | Pin 9 | | Echo | Pin 10 |
- Buzzer + → Pin 8
- Buzzer − → GND
Arduino Sketch
// Arduino Theremin
// HC-SR04 distance → buzzer frequency
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int BUZZ_PIN = 8;
const int MIN_DIST = 3; // cm — closest hand position
const int MAX_DIST = 40; // cm — furthest hand position
const int MIN_FREQ =
Playing Your Theremin
Musical Scale Practice
Hold your hand at different distances and try to find the positions for the notes of the C major scale. Hint: open the Serial Monitor to see the frequency in real time. The frequencies for C major are:
| Note | Frequency (Hz) | |------|---------------| | C4 (Middle C) | 262 | | D4 | 294 | | E4 | 330 | | F4 | 349 | | G4 | 392 | | A4 | 440 | | B4 | 494 | | C5 | 523 |
Mark the hand positions with tape on a ruler to create a "fret board" for your theremin.
Understanding the Code
pulseIn()
pulseIn(pin, HIGH, timeout) measures how long the pin stays HIGH. If no echo returns within the timeout (30,000 µs = 30 ms), it returns 0.
constrain()
constrain(value, min, max) clamps a value within bounds, preventing out-of-range inputs from producing nonsensical frequencies.
map()
map(dist, MIN_DIST, MAX_DIST, MAX_FREQ, MIN_FREQ) — note the reversed frequency arguments. Closer distance (small dist) → higher frequency, which feels intuitive when playing.
Pentatonic Scale Mode
Modify the sketch to only output the 5 notes of the pentatonic scale (C, D, E, G, A) by adding an array of allowed frequencies and snapping the continuous distance reading to the nearest note. This prevents dissonant sounds between notes.
