Photoresistor or Light Dependent Resistor (LDR) is used wherever there is a need to sense the absence or presence of light. In this tutorial, you will learn what is photoresistor or Light Dependent Resistor (LDR), LDR interfacing with Arduino, and how to write Arduino code for LDR sensor and LED. We will also introduce the Elegoo 37 sensor kit and its photoresistor module. Finally, intensity control of an LED using LDR and Arduino board code is presented.

What is photoresistor ?

The photoresistor or LDR (Light Dependent Resistor) is a light-sensitive semiconductor element that changes its resistance value according to the intensity of the light: the higher the light intensity on the sensitive surface of the LDR, the smaller its resistance value becomes.

A photoresistor has two connecting wires and can be treated like a variable resistor. The resistance value can be measured with an ohmmeter. The resistor has a very high resistance value in darkness and a very low resistance value in brightness.

LDR circuit

As we already mentioned, the resistance value indicates how much light falls on the photocell. However, the microcontroller cannot measure resistance directly, but it can measure the voltage applied to a resistor. Since the voltage is proportional to the resistor value according to ohm’s law, the easiest photoresistor circuit is to build a voltage divider between a fixed resistor and the LDR. The result is a voltage that changes according to the light intensity. The voltage drop across the resistor is connected to the analog input of the Arduino as shown below.

If the photoresistor is subjected to high light intensity, its resistance becomes small in relation to the fixed resistor. There is less voltage at the smaller resistor than at the larger one. In the following circuit, this means that we will measure a voltage value close to 5V at Arduino pin A0 at sun light for instance. In complete darkness, almost the entire voltage is applied to the photoresistor, and we measure voltage close to 0V.

LDR connection with Arduino

Photoresistor Arduino code

Below is an LDR Arduino code that measures the analog value of the LDR and prints it on the serial monitor. The purpose is to determine the analog value on which the room is dark or bright. Simply cover and uncover the LDR with your finger and monitor the value on the serial monitor.

Try to find out at which value the room is considered dark! The reason to do it this way because you might not be able to find the datasheet of your specific LDR. And to choose an appropriate value for your application without having to determine the illumination in lux.

#define LDR_PIN  A0 // LDR pin
unsigned int photoresistor = 0; // LDR_PIN value

void setup() { 
  Serial.begin(9600);
}

void loop() {
  // Read analog value of the LDR
  photoresistor = analogRead(LDR_PIN);
  Serial.println(photoresistor); // Print on serial monitor 
  delay(1000); //Delay 1 second
}

Arduino LDR LED code

The Figure below shows the LDR connection with Arduino. The LDR circuit is connected to the Arduino analog pin A0. And an LED is connected to digital pin 13. The LED here is a representation of a lamp. You can replace the LED with a relay with transistor circuit, and the code remains the same.

Photoresistor circuit Arduino

Below is an Arduino code for LDR sensor and LED. This Arduino LDR LED code measures the analog value of the LDR and compares it with a threshold value (that you have already determined). If the LDR value is below the threshold, the LED turns on.

#define LED_PWM_PIN  13 // LED pin
#define LDR_PIN  A0 // LDR pin
unsigned int photoresistor = 0; // LDR_PIN value
const unsigned int thrLDR = 300; // Threshold for LDR

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

void loop() {
  // Read analog value of the LDR_PIN 
  photoresistor = analogRead(LDR_PIN);
  // is LDR analog value greater than Threshold ?
  if (photoresistor < thrLDR) {
    digitalWrite(LED_PWM_PIN, HIGH); //Turn LED on
  }
  else
  {
    digitalWrite(LED_PWM_PIN, LOW); //Turn LED off
  }

  delay(1000); //Delay 1 second
}

Elegoo 37 sensor kit

The Elegoo 37 sensor kit contains 37 sensors and other electronic components that are combined together in a high-quality and nice plastic box. The benefit of buying such a kit is that the sensor circuits are already built for you so that you can concentrate on the code and your application.

Elegoo photoresistor

The Elegoo photoresistor module is a breakout board for the LDR circuit as shown below. Its LDR Resistance in darkness can go higher than 20 Megohms and as low as 80 Ohms in sunlight. The fixed resistor is 10 kOhm.

Elegoo photoresistor

The pin marked with – should be connected to GND, the middle pin should be connected to VCC, and the pin marked with S should be connected to Arduino analog pin.

Intensity control of LED using LDR and Arduino

Arduino LDR LED

In the previous code, the Arduino make a digital decision to turn the LED on or off if the room is dark or not. In this Arduino LDR example, we will use Arduino analog read and PWM output to change the intensity of the LED with respect to changes of the LDR brightness using Arduino map. Finally, it print out the LDR and LED PWM values. If the result is not satisfying, tune the values: 1023 and 255 for your application.

#define LED_PWM_PIN  9 // LED PWM pin
#define LDR_PIN  A0    // LDR analog pin
unsigned int ValLDR = 0, ledPWM = 0; // LDR and LED values

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

void loop() {
  // Read analog value of the LDR_PIN 
  ValLDR = analogRead(LDR_PIN);

  // Map LDR to PWM range
  ledPWM = map(ValLDR, 0, 1023, 0, 255);
  // Set PWM to the LED pin
  analogWrite(LED_PWM_PIN, ledPWM);
  
  Serial.print("LDR value: ");
  Serial.print(ValLDR);
  Serial.print("       "); 
  Serial.print("LED PWM vlaue: ");
  Serial.println(ledPWM);

  delay(1000); //Delay 1 second
}