4.Light-Controlled Multi-LED System Using an LDR and Arduino
- Get link
- X
- Other Apps
Light-Controlled Multi-LED System Using an LDR and Arduino
In this project, we'll use an LDR (Light Dependent Resistor) to control multiple LEDs based on the ambient light level. Depending on the light intensity, different LEDs will turn on or off to indicate the current light condition.
Components Needed:
- Arduino board
- LDR (photoresistor)
- 10kΩ resistor
- Multiple LEDs (e.g., 3 different colors)
- 220Ω resistors (one for each LED)
- 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 Circuits:
- Connect the anode (longer leg) of each LED to a different digital pin (e.g., 11, 12, 13).
- Connect a 220Ω resistor in series with the cathode of each LED, and connect the other end of the resistor to GND.
Example Code:
C++ Code
int ldrPin = A0; // Analog pin for LDR
int led1Pin = 11; // Digital pin for LED1
int led2Pin = 12; // Digital pin for LED2
int led3Pin = 13; // Digital pin for LED3
void setup() {
pinMode(led1Pin, OUTPUT); // Set LED1 pin as output
pinMode(led2Pin, OUTPUT); // Set LED2 pin as output
pinMode(led3Pin, OUTPUT); // Set LED3 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
// Control LEDs based on light level
if (ldrValue < 300) {
digitalWrite(led1Pin, HIGH); // Turn on LED1 when it is very dark
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
} else if (ldrValue >= 300 && ldrValue < 700) {
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH); // Turn on LED2 when it is moderately bright
digitalWrite(led3Pin, LOW);
} else {
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH); // Turn on LED3 when it is bright
}
delay(500); // Wait for half a second
}
Explanation of Code:
- LED Control: The code reads the LDR value and turns on different LEDs based on the light intensity:
- When it’s very dark, LED1 turns on.
- When the light level is moderate, LED2 turns on.
- When it’s bright, LED3 turns on.
- Threshold Values: Adjust the threshold values (
300
and700
in this example) to control the sensitivity for your specific environment. - Serial Monitor: You can use the Serial Monitor to observe the LDR values and determine the correct thresholds.
Applications:
- This project can be used for visual feedback on light levels in a room.
- It can also be used in projects where a multi-stage response is needed based on ambient light, such as dimming or brightening lights in steps.
- Get link
- X
- Other Apps
Comments
Post a Comment