Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Arduino LDR Tutorial

Created on: 27 January 2022

Arduino LDR tutorial that uses a Light Dependent Resistor which is also known as a photoresistor or photocell to turn on an LED when dark. Connect a LDR to an Arduino Uno or Arduino MEGA 2560 analog input in this part of the Arduino tutorial for beginners. Firstly read the analog value from the LDR circuit. Watch the analog value change as light on the sensor increases or decreases. Afterwards, load sketch code that turns on an LED when the LDR is darkened. Also switch off the LED when the LDR is exposed to enough light.

Part 11 of the Arduino Tutorial for Beginners

What is a LDR, Photoresistor or Photocell?

LDR stands for Light Dependent Resistor. Additionally, it is known as a photoresistor or photocell. A LDR is a type of resistor that changes resistance as light on its surface changes. That is, as more light shines on the LDR, its resistance decreases. Conversely less light or more darkness on the LDR surface causes its resistance to increase. The LDR (Photoresistor) for Beginners in Electronics article has more basic information on these devices. In addition the article shows two common LDR symbols used in circuit diagrams. The following image shows a LDR connected in series with a 10k resistor. This Arduino LDR tutorial uses the circuit to detect light levels and switch an LED on or off.

LDR Circuit Connected to an Arduino MEGA 2560 used in this Arduino LDR Tutorial
LDR Circuit Connected to an Arduino MEGA 2560 used in this Arduino LDR Tutorial

If you have a multimeter, try the following. Plug the LDR into a breadboard so that it is easier to handle. Put the multimeter onto the resistance or ohms scale. Place a multimeter lead on each LDR pin. Afterwards, check the resistance reading on the multimeter. Cover the LDR top surface using your finger. Observe that the resistance decreases with decreased light. Expose the LDR surface to more light. For example switch on a room light, or shine a torch on the LDR. Observe the LDR resistance decrease.

Arduino LDR Tutorial Circuit

The circuit below shows how to connect a LDR / photoresistor / photocell to an Arduino. Notice that the circuit uses an Arduino Uno. Connect an Arduino MEGA 2560 in the same way. That is, connect the LDR between the A0 pin and GND. Additionally, connect the 10k resistor between A0 and 5V.

Arduino LDR Tutorial Circuit Diagram
Arduino LDR Tutorial Circuit Diagram

In the above Arduino LDR circuit diagram, 10k resistor R1 and LDR R2 form a voltage divider. That is, the voltage at the junction of R1 and R2 is the divided voltage from the 5V that is across them. As light varies on the LDR surface, so does its resistance. This causes the voltage between GND and A0 to vary as well.

Arduino Uno LDR Breadboard Circuit

The image below shows the above circuit on breadboard and connected to an Arduino Uno. Build this circuit as shown and connect it to the Arduino Uno, if this is the board that you have.

Arduino Uno LDR Breadboard Circuit
Arduino Uno LDR Breadboard Circuit

Arduino MEGA 2560 LDR Breadboard Circuit

The image below shows the same circuit above, but attached to an Arduino MEGA 2560 board. Build this circuit if you have an Arduino MEGA 2560.

Arduino MEGA 2560 LDR Breadboard Circuit
Arduino MEGA 2560 LDR Breadboard Circuit

Arduino LDR Tutorial Sketch Code

Two sketches are presented in the subsections that follow. The first sketch is a built-in example sketch. This sketch displays the raw analog value read from the LDR. The second sketch turns on an LED when the LDR top surface is darkened.

Get the LDR Circuit Analog Value

As in previous parts of this tutorial, open the Analog Read Serial sketch. Select File → Examples → 01.Basics → AnalogReadSerial from the top Arduino IDE menu bar. Afterwards, upload this sketch to the target Arduino board that has the LDR circuit attached. Finally, open the Arduino IDE serial monitor window. The serial monitor window displays a raw analog value from the LDR circuit. Take note what the value is and then cover the LDR sensor surface. As a result, the analog value increases.

As an example, a value of around 300 is read in ambient light. After covering the LDR, this value goes up to about 750. Take note of the values as the high value is used in the next sketch. For example, use a value of 700 to switch on an LED if the high value is around 750. Use a value slightly below the reading that you get if your value differs. LDRs have different resistances. This is the reason that you may have a different reading. Another reason that analog values differ is because of different levels of ambient light.

Switch on an LED when Light Levels are Low

The following sketch code reads the analog value on analog input A0 of the Arduino. Afterwards it switches the Arduino on-board LED on if the analog value is above 700. That is, when the LDR is covered and dark. A value below 700 causes the sketch to switch the LED off.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0);
  if (sensorValue > 700) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
  delay(10);
}

How the LDR / LED Sketch Works

The above code is straightforward. It is based on the AnalogReadSerial and Blink example sketches. Both example sketches are on the File → Examples → 01.Basics menu of the Arduino IDE. Code in the setup() part of the sketch sets up the on-board LED pin as an output. That is the same as the Blink example sketch does. The function analogRead() gets the analog value from pin A0 at the top of the loop() function. That is from the AnalogReadSerial example sketch. After this, sensorValue is checked to see if it is greater than 700. In other words if the LDR is in the dark. Change the value of 700 to suit the LDR that you are using. This value is the value found in the serial monitor window from the previous section.

The code switches the LED on if the analog value is greater than 700. Code that does this is from the Blink sketch again. If the analog value is less than or equal to 700, then the code switches the on-board LED off. Once again, the code that does this is from the Blink sketch. Finally, delay() uses up 10 milliseconds so that the analog value is not sampled too fast.

Run the Arduino LDR Tutorial Sketch

Type the code from the above sketch listing into a new sketch in the Arduino IDE. Alternately, copy the above code and paste it into a new Arduino IDE sketch.

Change the comparison value in the sketch if needed, as mentioned above. Afterwards, upload the sketch to the target Arduino board. Check that the on-board LED is off in normal light conditions. Finally, cover the LDR top surface to make it dark. Check that the on-board LED switches on when covered. Similarly make sure that the LED switches off when uncovered again.

Load the AnalogReadSerial sketch again if there are problems. Check the analog value in the serial monitor window again. Cover the LDR and check the analog value again. Adjust the above sketch with a comparison value below the value read with the LDR covered. Subtract around 50 to 100 from the analog value read when the LDR is covered. Upload the sketch to the Arduino again. Finally, test the above modified sketch again.

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual