Cnipbotics
Home
CoursesPricingContact
All Projects
Intermediate

Arduino Theremin

Build a contactless musical instrument using an HC-SR04 ultrasonic sensor that detects hand distance and converts it into musical notes played through a passive buzzer

30 Minutes
Arduino Uno R3, HC-SR04 Ultrasonic Sensor, Passive Buzzer, Breadboard, Jumper Wires
Arduino Theremin

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() and constrain() 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

ComponentQtyNotes
Arduino Uno R31_
HC-SR04 Ultrasonic Sensor15 V, range 2–400 cm
Passive Buzzer1Must be passive (not active)
Breadboard1_
Jumper Wires7+_

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:

  1. Emitting a 40 kHz ultrasonic pulse from the Trig pin
  2. Waiting for the echo to return to the Echo pin
  3. 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

Step 1
Connect HC-SR04

| HC-SR04 Pin | Arduino Pin | |-------------|-------------| | VCC | 5 V | | GND | GND | | Trig | Pin 9 | | Echo | Pin 10 |

Step 2
Connect Buzzer
  • 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  = 131;  // Hz — C3 (low C)
const int MAX_FREQ  = 1047; // Hz — C6 (high C)
 
long measureDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
 
  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30 ms timeout
  if (duration == 0) return -1; // no echo
  return duration / 58L;
}
 
void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZ_PIN, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  long dist = measureDistance();
 
  if (dist < 0 || dist > MAX_DIST + 5) {
    noTone(BUZZ_PIN); // silence if out of range
  } else {
    dist = constrain(dist, MIN_DIST, MAX_DIST);
    int freq = map(dist, MIN_DIST, MAX_DIST, MAX_FREQ, MIN_FREQ);
    tone(BUZZ_PIN, freq);
    Serial.print("Dist: ");
    Serial.print(dist);
    Serial.print(" cm → Freq: ");
    Serial.print(freq);
    Serial.println(" Hz");
  }
 
  delay(50);
}

Playing Your Theremin

ACTIVITY

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.

ACTIVITY

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.

Explore more projects

View All Projects
Stay updated

Subscribe to
our newsletter

Get the latest curriculum updates, project ideas, and school program announcements delivered to your inbox.

Ready to get started?

Bring structured robotics to your school

Schedule a DemoView Curriculum
200+
Schools nationwide
4.8★
Teacher satisfaction
4
Class levels covered
100%
NEP 2020 aligned
CnipboticsCnipbotics

India's structured robotics curriculum for CBSE schools — Class 7 to Class 10.

CurriculumClass 7 — DiscoveryClass 8 — ExplorationClass 9 — EngineeringClass 10 — InnovationCBSE Chapter Mapping
ProgramsBrowse CoursesPricing & KitsRequest a DemoAbout Us
CompanyAbout UsContactPricing
Get in touch

Questions about curriculum, pricing, or ATL lab setup?

Contact Us

School partnership

info@cnipbotics.com

© 2026 Cnipbotics. All rights reserved.

TermsPrivacyCookiesAccessibility