Since I need to do some serious work on the Arduino platform in the near future, I decided to have a look at all the material I had lying around to make sure I have everything I need for my next project. However, I discovered a particular object that I didn’t know what it was.
I quickly took a picture of it and shared on Twitter (which is actually much better than Google for this kind of stuff) and, right away, several people replied pointing out that the mysterious object was in fact a photoresistor, or a light-dependent resistor.
Basically, this small sensor outputs its sensitivity to light and in this case, it produces a value between 0 (in very dark places) and 1023 (close to the sun) that can be read by an Arduino board through one of the analog input pins.
Since I also had a small buzzer that I could connect to the Arduino, the next step became obvious: to build a cheap-ass theremin. Before lunch, preferably… And it was indeed quite easy to build and program. Here’s the schematic for the whole thing:
Basically, you need: a buzzer, a photoresistor, an Arduino board (pictured here is the Ethernet shield since the Arduino I used was the Ethernet variant), a couple of resistors, a mini-breadboard and some wires for the connections. In case you’re wondering how to do this kind of schematics, I used Fritzing.
As for the code, it’s also quite simple: you just need to get the photoresistor sensor value from the chosen analog input pin (in this case, A0) – a value between 0 and 1023 (awesome tutorial here) – and convert it to a frequency, ranging from 0 to 2500Hz (I chose this particular frequency because it sounded loud enough but I’m not sure how high it can go) and send it to the digital output pin (in this case, 4). The buzzing part is a bit trickier but this tutorial explains it quite well.
Here’s the complete code:
int prPin = 0; // Pin where the photo resistor is connected to
int prReading; // The analog reading from the photoresistor
int buzzerPin = 4; // Connect Buzzer to Pin 4
long buzzerFreq; // The frequency to buzz the buzzer
// You can experiment with these values:
long BUZZ_FREQ_MAX = 2500; // Maximum frequency for the buzzer
long PR_MAX = 1023; // Maximum value for the photoresistor
void setup() {
pinMode(buzzerPin, OUTPUT); // set a pin for buzzer output
}
void loop() {
prReading = analogRead(prPin); // Values 0-1023
buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;
buzz(buzzerPin, buzzerFreq, 10);
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2;
long numCycles = frequency * length/ 1000;
for (long i=0; i < numCycles; i++){
digitalWrite(targetPin,HIGH);
delayMicroseconds(delayValue);
digitalWrite(targetPin,LOW);
delayMicroseconds(delayValue);
}
}
And here’s the video with a small demo:
I know this is nothing like a real theremin, but considering the limited output sound that the buzzer can produce (and the ridiculous amount of time I spent with this – around 20 minutes), this is probably the best I can do with this stuff.


really good job.
No! this like a real thermin sound.