2.Light-Controlled LED Using an LDR and Arduino
Light-Controlled LED Using an LDR and Arduino
Components Needed:
- Arduino board
- LDR (photoresistor)
- 10kΩ resistor
- LED
- 220Ω resistor
- Breadboard and jumper wires
Circuit Connections:
LDR Circuit:
- Connect one leg of the LDR to 5V on the Arduino.
- Connect the other leg of the LDR to an analog input pin (e.g., A0) and a 10kΩ resistor.
- Connect the other end of the 10kΩ resistor to GND.
LED Circuit:
- Connect the anode (longer leg) of the LED to a digital pin (e.g., 13).
- Connect a 220Ω resistor in series with the cathode of the LED, and connect the other end of the resistor to GND.
Example Code:
C++ Code
int ldrPin = A0; // Analog pin for LDR
int ledPin = 13; // Digital pin for LED
int threshold = 500; // Threshold value for light level
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial communication
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read LDR value
Serial.println(ldrValue); // Print LDR value to Serial Monitor
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn on LED when it is dark
} else {
digitalWrite(ledPin, LOW); // Turn off LED when it is bright
}
delay(500); // Wait for half a second
}
Explanation of Code:
- Threshold: The threshold value is used to decide when the LED should turn on or off. You can adjust this value based on your ambient light conditions.
- analogRead(ldrPin) reads the current light level, and if the value is below the threshold, it turns the LED on; otherwise, it turns it off.
- Serial Monitor: You can view the light readings on the Serial Monitor to help determine a suitable threshold value for your environment.
Applications:
- This project can be used to create an automatic night light that turns on when it gets dark.
- It can also be used in security applications where lights need to be triggered based on the ambient light level.
Comments
Post a Comment