Built-in project · Arduino Uno R3

Light theremin on an Arduino Uno

A phototransistor reads the light and a piezo plays it as a pitch, higher for more light: the Arduino Starter Kit theremin. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

Build it as the Starter Projects Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $48, free on Mokxi.

Light theremin · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading light with a phototransistor
  • Turning current into voltage with a resistor
  • Mapping a reading to a frequency
  • Making sound with tone
  • Why you only call tone when the pitch changes
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A phototransistor turns light into current, and a 10k resistor turns that current into a voltage on A0. More light gives a higher reading. The sketch maps the reading to a pitch between LOW_PITCH, 120 Hz, and HIGH_PITCH, 1500 Hz, and plays it on a piezo on pin 8.

Wave a hand over the sensor and the pitch changes. A phototransistor saturates in a bright room and reads near the top. The useful range is from dark to a dim room.

The sketch prints the reading and the pitch four times a second.

Parts list

5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 10k Ω pin 1Arduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Piezo speaker pin 1Arduino Uno R3 pin 8
Phototransistor pin CArduino Uno R3 pin 5V
Phototransistor pin EArduino Uno R3 pin A0
Resistor, 10k Ω pin 2Arduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Light theremin: a phototransistor reads the light and a piezo plays it as a
// pitch, higher for more light. Move your hand over the sensor and it sings.
//
//   phototransistor   C -> 5 V   E -> A0, and 10 k from A0 to GND
//   piezo             one leg -> pin 8, the other -> GND
//
// A phototransistor is a transistor whose base current is light: the
// collector current follows the light, and the 10 k resistor turns it into a
// voltage the analog pin can read. It is not a resistor like an LDR, so in a
// bright room it saturates and reads near the top, and the useful range is
// from dark to a dim room. Drag the light slider down to "cover" it.
//
// tone() plays the note from a timer, so the loop keeps reading while the note
// sounds. It is only called again when the pitch moves: every call restarts
// the timer, and calling it every 10 ms with the same note would cut a low
// note's cycles short and raise its pitch.

const int SENSOR_PIN = A0;
const int PIEZO_PIN = 8;
const int LOW_PITCH = 120;    // hertz, in the dark
const int HIGH_PITCH = 1500;  // hertz, at full scale

unsigned long printedAt = 0;
int playing = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Light theremin: move your hand over the sensor");
}

void loop() {
  int light = analogRead(SENSOR_PIN);
  int pitch = map(light, 0, 1023, LOW_PITCH, HIGH_PITCH);
  if (pitch != playing) {
    tone(PIEZO_PIN, pitch);
    playing = pitch;
  }

  if (millis() - printedAt >= 250) {
    printedAt = millis();
    Serial.print("light ");
    Serial.print(light);
    Serial.print(" -> ");
    Serial.print(pitch);
    Serial.println(" Hz");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens serial at 9600 baud and prints a message. The analog pin and the piezo pin need no pin setup, since analogRead and tone handle them.

void setup() {
  Serial.begin(9600);
  Serial.println("Light theremin: move your hand over the sensor");
}

loop sketch.ino

Reads SENSOR_PIN, then uses map to turn 0 to 1023 into a pitch from LOW_PITCH to HIGH_PITCH. It calls tone only when the pitch changed, since each call restarts the timer. Every 250 ms it prints the light and pitch, and it waits 10 ms.

void loop() {
  int light = analogRead(SENSOR_PIN);
  int pitch = map(light, 0, 1023, LOW_PITCH, HIGH_PITCH);
  if (pitch != playing) {
    tone(PIEZO_PIN, pitch);
    playing = pitch;
  }

  if (millis() - printedAt >= 250) {
    printedAt = millis();
    Serial.print("light ");
    Serial.print(light);
    Serial.print(" -> ");
    Serial.print(pitch);
    Serial.println(" Hz");
  }
  delay(10);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The pitch sits at the top and never changes This project
    The room is too bright and the sensor has saturated. Dim the light, or cover the sensor.
  • No sound This project
    The piezo needs pin 8 on one leg and GND on the other.
  • The reading does not change with light. Phototransistor
    A phototransistor has polarity. Put a resistor (about 10 k ohms) in series, and read the voltage at the joint. If it still does nothing, flip the part around.
  • The reading is stuck at the top or bottom. Phototransistor
    A bigger resistor makes the sensor more sensitive but saturates quickly in bright light. A smaller one needs more light. Try 1 k to 100 k ohms.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
    A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
  • Sound is very quiet or the pin seems overloaded. Piezo speaker
    Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.