Arduino Light Theremin (Starter Kit Project #6)
We will use a phototransistor as a light sensor to control a piezo and produce sound – resembling the functionality of an actual theremin.
Setup
Here’s what we need:
Arduino UNO + BreadboardJumper wires/cables1 10kΩ resistorPiezo1 phototransistor
Layout
Here’s what the instrument should look like, as per the Arduino projects book.
[image error]
[image error]
And here is what I made:
[image error]
Code
The calibration of the sensor is the most important part in this mini-project.
int sensLow = 1023;
int sensHigh = 0;
void setup() {
while (millis() < 5000) {
sensVal = analogRead(A0);
if (sensVal > sensHigh) {
sensHigh = sensVal;
}
if (sensVal < sensLow) {
sensLow = sensVal;
}
}
}
The millis function returns the amount of time for which our board has been working. Thus, we calibrate for the first five seconds by taking in the minimum and maximum possible sensor input values. These will be used to scale our input values later on.
void loop() {
sensVal = analogRead(sensPin);
int pitch = map(sensVal, sensLow, sensHigh, 50, 4000);
tone(8, pitch, 20);
delay(10);
}
Here, we take in our input value and map it to the appropriate frequency. Then, using the tone function, we output sound using our piezo (which vibrates as per the frequency provided by tone()). You can play around the frequency and mapping values just for fun.
The post Arduino Light Theremin (Starter Kit Project #6) appeared first on Mehul Jangir's blog.
Mehul Jangir's Blog
- Mehul Jangir's profile
- 6 followers

