Using a light sensor to control an LED

This tutorial explains how to use an Arduino Uno and LDR (light dependent resistor) light sensor to control an LED. The light sensor will detect low-light conditions to turn on an LED and turn off the LED when light is present.

The tutorial will cover two different types of light sensors:

  • Freetronics LDR light sensor (it has 3 pins – GND, OUT, and VCC)
  • Generic LDR light sensor (it has 2 pins)

The project requires:

  • An Arduino Uno board
  • An LED light
  • An LDR light sensor
  • Breadboard
  • Jumper wires
  • 10k Ohm resistor (if using the light sensor with two pins)

The way the project works is as follows. Use the light sensor to get light readings at a set interval (eg. once per second). The light sensor will return values representing different levels of light between 0 and 1023 (0 means very dark and 1023 means very bright). You can specify a value which, when the light is less than this value an LED light will be turned on, and when the light is greater than this value the LED light will be turned off.

This project allows you to work with taking readings from a sensor (input), view readings in the serial monitor or serial plotter, use if statements to test conditions, and use an LED to produce an output.

Watch the video below or scroll down to view the wiring schematic and sample code.

Using the Freetronics Light SENSOR (with 3 pins)

Here is the layout showing the long pin of the LED connected to Pin 13 on the Arduino and the short pin connected to GND on the Arduino. GND on the light sensor is connected to GND on the Arduino, OUT is connected to pin A2, and VCC is connected to 5V.

Light sensor

You can also share one GND pin on Arduino between the LED and the light sensor using the breadboard and jumper wires, as shown below.

Screen Shot 2021-02-10 at 10.10.01 am 2

Upload the code to the Arduino. In the Arduino IDE, click Tools > Serial Monitor and set the baud rate (in the dropdown box) to 9600 baud to view the light sensor readings received from the Arduino when it is connected via USB to the computer.

Here is a screenshot of the light sensor readings in the Serial Monitor. You can use these readings to test the light sensor and determine an appropriate value for low-light conditions to specify when the LED should turn on. If you are only logging numbers (and no text) to the serial monitor, then you can also use the Serial Plotter to plot the values on a graph.

Light sensor readings
Light sensor readings

Test the light sensor by covering it with your hand or shining a light on it to see the different light sensor readings.

FREETRONICS LIGHT SENSOR (3 pins) Source code

Here is the source code for the Freetronics light sensor (the sensor with three pins).

int led = 13;
int lightReading = 0;

void setup () {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop () {
  lightReading = analogRead(A2);
  Serial.println(lightReading, DEC);
  if (lightReading < 50) // change this value to suit light conditions. Ranges from 0 (very dark) to 1023 (very bright).
  {
    digitalWrite(led, HIGH); // turns the light on
    Serial.println("light on");
  } 
  else
  {
    digitalWrite(led, LOW); // turns the light off
    Serial.println("light off");
  }
  // Delay of 1000ms before checking light level again
  delay(1000);
  
}

USING THE generic LDR LIGHT SENSOR (WITH 2 PINS)

Here is the schematic showing the 2-pin LDR light sensor and an LED connected to the Arduino via a breadboard. One pin of the light sensor is connected to 5V on the Arduino and the other is connected to analog input pin A0 on the Arduino. GND from the Arduino is also connected via a 10k Ohm resistor on the breadboard to the light sensor, and directly to the short pin of the LED. Digital pin 13 on the Arduino is connected to the long pin of the LED.

LDR Photoresistor

Light Sensor (with 2 pins) source code

Here is the source code for the light sensor that has two pins.

int led = 13;
int lightSensor = A0;
int lightReading = 0;

void setup () {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop () {
  lightReading = analogRead(lightSensor);
  Serial.println(lightReading, DEC);
  
  if (lightReading < 50) // change this value to suit light conditions. Ranges from 0 (very dark) to 1023 (very bright).
  {
    digitalWrite(led, HIGH); // turns the light on
    Serial.println("light on");
  } 
  else
  {
    digitalWrite(led, LOW); // turns the light off
    Serial.println("light off");
  }
  
  // Delay of 1000ms before checking light level again
  delay(1000);
}

Upload the code to the Arduino. In the Arduino IDE, click Tools > Serial Monitor and set the baud rate (in the dropdown box) to 9600 baud to view the light sensor readings received from the Arduino when it is connected via USB to the computer. You can use these readings to test the light sensor and determine an appropriate value for low-light conditions to specify when the LED should turn on.

Test the light sensor by covering it with your hand or shining a light on it to see the different light sensor readings. If it’s not getting correct readings, try swapping the light sensor pins around.

Leave a Reply