#define LDR A0 //Light dependent resistor (LDR) connected to analog pin A0 #define LED 5 //LED connected to digital pin 5 int LDR_value; //variable to store result value read by LDR
void setup() { Serial.begin(9600); //intialise the serial port for communication pinMode(LED, OUTPUT); // set LED as output pinMode(LDR, INPUT); //set light dependent resistor as input
}
void loop() {
LDR_value=analogRead(LDR); //read the data from analog pin A2 and store in LDR_value
if (LDR_value<=500) //if LDR_value value is less than or equal to 300 that means its dark outside so LED should be turn on
{ digitalWrite(LED, HIGH);//turn on the LED
Serial.println(“its dark turn on the LED”);// display “its dark turn on the LED” on serial monitor and set the cursor to the new line Serial.println(“LDR value:”); //display “LDR value:” on serial monitor and set the cursor to the new line Serial.println(LDR_value); // print the value of sensor
} else //if LDR_value value is greater than 300 that means its bright outside so LED should be turn off { digitalWrite(LED, LOW); //turn off the LED
Serial.println(“its bright turn off the LED”); // display “its bright turn off the LED” on serial monitor and set the cursor to the new line Serial.println(“LDR value:”); //display “LDR value:” on serial monitor and set the cursor to the new line Serial.println(LDR_value); // print the value of sensor } delay(200); //delay
}
The logical flow of the program is very simple:
Get the brightness value from the sensor.
Display brightness value on the screen.
If the brightness value is less than 500, that means brightness is low, then power ON the LED.
If the brightness is greater than 500, that means its day time, then power OFF the LED.
Login
Accessing this course requires a login. Please enter your credentials below!