Using the point-slope formula between the on-time and off-time of a passive buzzer, we can control the high frequency and low-frequency tone sound as a function of light. As a light source is drawn near to the photoresistor, the buzzer will increase in frequency. Conversely, as the light source is moved away from the photoresistor sensor, the buzzer sound will correspond to a low-frequency tone. Removal of the light source fully separated from the photoresistor permits an ambient light reading until the buzzer is fully settled to a maximally lower frequency of sound.
By experimenting with the point-slope formula in the code, you are able to change the tonality. However, to get a precise range and audible quality of the buzzer, it is best to calculate the on-time from a sample source reading at the sensor and an off-time (delay time) to complete each frequency cycle. The more cycles of on and off time, the higher the frequency.
5K Resistor in series with the photoresistor sensor. The Arduino UNO pin connected between the 5K resistor and the light sensor is reading a change in voltage present at this connection. The voltage divider changes value as the resistance of the light sensor increases or decreases. As the sensor’s resistance value decreases with increasing light levels, it conducts more and drops less voltage. The analogRead code instruction detects the change of voltage at this node and in turn produces change in tonality via the digitalWrite command at the output pin connected to the buzzer.
This is a passive buzzer that is driven by a voltage and light level separated by a delay between each digitalWrite event. Causing the buzzer to produce a tone of lower or greater frequency as new voltages are presented to it. Each new voltage level from the voltage divider produces its sound frequency by continuously turning the buzzer on and off again as it recognizes new levels as light intensity changes.
Program & Functional Operation
This video demonstrates a change in buzzer tone as the light source distance from the light sensor is changed. The closer to the light sensor is exposed to a higher intensity of illumination, the higher the frequency in sound. The lower the light intensity, the lower the sound pitch or frequency.
Light & Delay Value Selection
To come up with a delay value for the integer variable declared in the code, it is necessary to the light sensor’s voltage value into memory via pin A0 on the Arduino. This means it is necessary to perform a calculation on the changes of input voltage levels as they change as light illumination intensity changes. The formula derivation is as follows:
Code Setup:
Copy & paste this code to your Arduino Sketch as desired to edit or experiment with your program.
int lightPin=A0;
int buzzPin=8;
int lightVal;
int delayT;
void setup()
{
pinMode(A0,INPUT);
pinMode(buzzPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
lightVal=analogRead(lightPin);
//This sets the tone for the buzzer
delayT=(9./550.)lightVal-(9.200./550.)+1.;
Serial.println(delayT);
digitalWrite(buzzPin,HIGH);
delay(delayT);
digitalWrite(buzzPin,LOW);
delay(delayT);
}