In this invention, you’ll use a light sensor to control the tone of the buzzer. If you move your hand closer to the sensor then the pitch will get lower, but it you take it away, it will get higher.
Parts list
What you’ll need
Step 1
Let’s build this
This is the positive leg of the buzzer. It will have a ‘+’ over top and will also be longer than the other leg.
The light sensor and the resistor both need to be plugged into pin A0.
Connections
Buzzer
The +LEG is the longer leg.
+ LEG11
– LEGGND
Light Sensor
Both legs are the same.
LEGA0
LEG5V
Resistor
Both legs are the same.
LEGA0
LEGGND
Step 2
Code some chaos
Don’t forget to select your port, like usual
Copy and paste the sample code
light_synth.ino
int LDR = A0;
int buzzer = 11;
int maxLight = 200;
int minLight = 0;
int light = 0;
void setup() {
while(millis() < 5000) { light = analogRead(LDR); if(light > maxLight) maxLight = light;
if(light < minLight) minLight = light;
}
}
void loop() {
light = analogRead(LDR);
int pitch = map(light,minLight,maxLight,50,4000);
tone(buzzer, pitch, 10);
delay(5);
}