For this circuit, the LDR quantifies light intensity. In very dim or no light, the red light turns on. In normal room fluorescent light, the yellow light turns on. And a green light signifies bright light.
Schematic:
In normal room light:
In dark light (covering light sensor):
In bright light:
Code:
int red = 13; // assignment of variables
int yellow = 12;
int green = 11;
void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT); // assignment of pins
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0); // read and print values for light intensity
Serial.println(sensorValue);
delay(1);
if (sensorValue < 550) {
digitalWrite(red,HIGH);
} // red light turns on in the dark
else {
digitalWrite(red, LOW);
}
if ((sensorValue > 550) && (sensorValue < 900)) {
digitalWrite(yellow, HIGH);
}
else {
digitalWrite(yellow, LOW);
} // yellow light turns on in dim-normal light
if (sensorValue > 900) {
digitalWrite(green, HIGH);
}
else {
digitalWrite(green, LOW);
} // green light turns on in bright light
}