Photoresistor

It is like a potentiometer, but instead of a knob, it uses light to control resistance

When light falls on photo resistor, resitance decreases

Usage

1) It is an analog device, and the state of the device is read from one of the analog input pins

2) The value of resistance R1 determines the calibration of the analog values (0-1023). That is, if we want the scale to be at midpoint for ambient lighting, measure the resistance at ambient condtions (R2) and compute R1 using the voltage divider formula (in notes). For this case, R1 = R2

Sketch

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  // under bright condition, resistance is 0,
  // we get small A0 value

  // under dark condition, resistance is high, max 5V
  // we get large A0 value

  // under ambient condition, we have chosen resistor such
  // that A0 value is at midpoint (around 500)
  int value = analogRead(A0);
  Serial.println(value);
  delay(100);
}