Theremin are those unique instruments use to make those alien show theme songs or sound effect. You may have also heard it in Star Trek, Big Bang Theory, or even a haunted house. They produced a unique sound from the electromagnetic effects between wires.

Here we will duplicate a similar sound digitally using a buzzer controlled by Pulse Width Modulations and an Light Dependent Resistor (LDR) for the input of reading values as the hand moves over it.
Step 1: BoM

* ESP32
* Light Dependent Resistor (LDR)
* Buzzer
* Jumper Wires
* Breadboard
Step 2: Soldering

We will solder a voltage divider onto the LDR to make the wiring simpler.
* Take a 10kΩ resistor and solder it to one of the pins of the LDR.
* Then take two different coloured wires and solder it to each pin of the LDR.
That’s it! Now you have a voltage divider!
Step 3: Wiring

Follow the following table when wiring the LDR and Buzzer to the ESP32:
I/O | Pin # | ESP32 Pin # |
---|---|---|
Buzzer* | 1 | D4 |
Buzzer* | 2 | GND |
LDR | Resistor | D5 |
LDR | Grey | 3.3V |
LDR | Red | GND |
* Order is arbitrary
Step 4: Code

int photopin = 5; // Pin where the photo resistor is connected to
int photValue; // 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 buzzMAX = 2500; // Maximum frequency for the buzzer
long photoMAX = 1023; // Maximum value for the photoresistor
void setup() { pinMode(buzzerPin, OUTPUT); // set a pin for buzzer output
}
void loop() { // read the values of the potentiometer photValue = analogRead(photopin); // Values 0-1023
// normalize the readings of a photoresistor to thatof the buzzer and photoresistor buzzerFreq = (photValue * buzzMAX) / photoMAX;
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);
}
}
Step 5: Enjoy!

It’s easy to use but takes a lifetime to master and to play good music. Move your hands over the LDR to change the tone.
Enjoy your pocket sized theremin!