In this project, we will utilize a Light Dependent Resistor (LDR) to detect varying light levels and a piezo sounder to provide audible feedback based on the detected light intensity. The LDR's resistance decreases as it receives more light, allowing us to determine the ambient light conditions.
What We Will Learn in This Section- Implementing light sensing with an LDR on Arduino
- Using a piezo sounder for audible feedback
- Understanding the relationship between LDR resistance and light intensity
Learning to interface sensors like the LDR with Arduino expands your ability to create projects that respond to environmental conditions, enhancing interactivity and functionality.
Components ListCircuit Diagram (With Connection)Before starting, ensure your Arduino is powered off by disconnecting it from the USB cable. Connect the components as per the diagram. Verify all connections to avoid issues when powering up your Arduino.
Codeint piezoPin = 8; // Piezo on Pin 8
int ldrPin = 0; // LDR on Analog Pin 0
int ldrValue = 0; // Value read from the LDR
void setup() {
// nothing to do here
}
void loop() {
ldrValue = analogRead(ldrPin); // read the value from the LDR
tone(piezoPin, 1000); // play a 1000Hz tone on the piezo
delay(25); // wait a bit
noTone(piezoPin); // stop the tone
delay(ldrValue); // wait for the amount of milliseconds in ldrValue
}
Code ExplanationVariable Declaration:
piezoPin
is connected to the piezo sounder.ldrPin
is connected to the LDR on analog pin 0.ldrValue
stores the value read from the LDR.- Variable Declaration:
piezoPin
is connected to the piezo sounder.ldrPin
is connected to the LDR on analog pin 0.ldrValue
stores the value read from the LDR.
Setup Function:
- No setup actions are needed for this project.
- Setup Function:
No setup actions are needed for this project.
Loop Function:
analogRead(ldrPin);
: Reads the current light level as a value.tone(piezoPin, 1000);
: Plays a 1000Hz tone on the piezo sounder.delay(25);
: Waits briefly.noTone(piezoPin);
: Stops the tone.delay(ldrValue);
: Waits for a duration based on the light level read from the LDR.- Loop Function:
analogRead(ldrPin);
: Reads the current light level as a value.tone(piezoPin, 1000);
: Plays a 1000Hz tone on the piezo sounder.delay(25);
: Waits briefly.noTone(piezoPin);
: Stops the tone.delay(ldrValue);
: Waits for a duration based on the light level read from the LDR.
After uploading the code to your Arduino, it will emit short beeps with longer intervals in low light and shorter intervals in bright light, creating a feedback effect similar to a Geiger counter. For practical use, consider extending the LDR's reach with soldered wires to explore different light conditions while keeping your setup stationary.
Further Learning: This book will help you gain more knowledge about Arduino: Beginning Arduino
Comments