The light sensor is also called a light-dependent resistor (LDR) or photocell. It is connected to Arduino pin A0. analogRead measures the voltage at the pin. You will get a value between 0 and 1023 (where 0 equals ground and 1023 equals +5 volts).

Changing the type of lamps and ambient light will give a bit of variation in your readings. In many cases a covered sensor equals about 500 and an exposed sensor is around 800. So under those circumstances an ON / OFF threshold-value of about 700 would be fine.

 

 

Light sensor


 

 

Code

void setup() {
  Serial.begin(9600); // Set up serial communication with the computer
}
 
void loop() {
  int lightSensorValue = analogRead(A0);     // Read the input on analog pin 0
  if(lightSensorValue > 700) {
      Serial.println("Light switch is ON");  // Sensor is receiving light
  } else {
      Serial.println("Light switch is OFF"); // Sensor is covered
  }
  delay(1);                          // Don't do analogRead too often
}

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.