An LDR is a component that has a (variable) resistance that changes with the light intensity that falls upon it. This allows them to be used in light sensing circuits.
Variation in resistance with changing light intensity. LDR is connected with pin “A0” of Arduino pin.
As per shown in Figure LDR is connected in such a way that it forms a voltage divider with a 1k resistor(why?). With a change in LDR resistance value, the voltage at A0 will change and thus indicates the variation in the intensity of light w.r.t. to voltage.
Interface LDR with Arduino. Bring the torch or any light near to the sensor. Check the change in serial monitor. Now cover the LDR with hand and observe the change in readings.
//LDR with Arduino
int LDR_pin = A0;
int LDR_value; //To store the current LDR value
void setup()
{
pinMode(LDR_pin,INPUT); //Initialize the analog pin A0 as an Input
Serial.begin(9600); //Initialize Serial to check the LDR value
}
void loop() {
LDR_value = analogRead(LDR_pin); //Sample the LDR value at Analog pin A0
Serial.println(LDR_value); //Write the LDR value to the Serial Monitor
delay(1000); //Delay for 1 second before sampling again
}
Serial.begin()
function is used to initialize the serial monitor with a baud rate of 9600 bps. You can set other baud rates as well. To know more about baud rates check: Serial CommunicationSerial.println()
function.