Arduino - Light Sensor

About Light Sensor

The light sensor used in this tutorial is a photoresistor, which is also called light-dependent resistor or photocell.

It is used not only to detect light but also to measure the brightness/illuminance level of the ambient light.

Pinout

A photoresistor has two pins. Since it is a kind of resistor, we do NOT need to distinguish these pins. They are symmetric.

Light Sensor Pinout

How It Works

The more the light the photoresistor's face is exposed, the smaller its resistance is. Therefore, by measuring the photoresistor's resistance, we can know how bright the ambient light is.

How Light Sensor Works

WARNING

The light sensor value only reflects the approximated trend of the intensity of light, it does NOT represent the exact luminous flux. Therefore, it should be used only in an application that does NOT require high accuracy.

Arduino - Light Sensor

Arduino's pin A0 to A5 can work as the analog input. The analog input pin converts the voltage (between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.

By connecting a pin of the photoresistor to an analog input pin, we can read the analog value from the pin, and then we can know the light levels relatively.

Wiring Diagram

Arduino Light Sensor Wiring Diagram

Image is developed using Fritzing

Arduino Code

The below code reads the value from photocell and determine the light level qualitatively

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor */ void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analogValue = analogRead(A0); Serial.print("Analog reading = "); Serial.print(analogValue); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (analogValue < 10) { Serial.println(" - Dark"); } else if (analogValue < 200) { Serial.println(" - Dim"); } else if (analogValue < 500) { Serial.println(" - Light"); } else if (analogValue < 800) { Serial.println(" - Bright"); } else { Serial.println(" - Very bright"); } delay(500); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Radiates light to sensor
  • See the result on Serial Monitor:
  • COM6
    Send
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Light Sensor and LED

The below code turns ON the LED when it is dark, otherwise turns OFF the LED

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor */ void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analogValue = analogRead(A0); Serial.print("Analog reading = "); Serial.print(analogValue); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (analogValue < 10) { Serial.println(" - Dark"); } else if (analogValue < 200) { Serial.println(" - Dim"); } else if (analogValue < 500) { Serial.println(" - Light"); } else if (analogValue < 800) { Serial.println(" - Bright"); } else { Serial.println(" - Very bright"); } delay(500); }

Video

incoming

Challenge Yourself

  • Automatically turn on the light when your room is dark. Hint: Refer to Arduino - Relay .

Alternative Light Sensors

The above code also works with the following light sensors:

Comments

If you have any questions or suggestions, please comment on our YouTube channel