How to connect Photo resistor(LDR) with Arduino and use it

Learn how to use a photoresistor or LDR with Arduino, which is a device that varies its value depending on the light received.

In this article we are going to make a simple light meter with our arduino, we will use an arduino with LDR photoresistor and we will show the result on the computer as well as on a group of LEDs. The objective of this tutorial is to learn how to connect sensors that vary their resistance depending on a physical magnitude. An example of this type of sensor is the LDR or photoresistor, we will  study the operation of this component and its connection to the Arduino UNO R3 board using a resistive divider in the rest of this text.

In our cover photo you can see the test circuit for the Arduino with LDR Photoresistance mounted on the breadboard.

Basic concepts

To understand how this circuit works and the program that runs on the Arduino board, we must know 3 key concepts:

  • LDR Photoresistor : Component whose resistance varies significantly with the amount of light perceived. The relationship between light intensity and resistance value is not linear. It is widely used to measure lighting in electronic devices that require aggressive pricing. Its behavior is as follows:
    • More light = less electrical resistance
    • Less light = greater electrical resistance
  • Voltage divider : Using a pair of resistors in series, it is possible to distribute the voltage supplied by the source between the terminals of these, in our case, the divider is used with the LDR to obtain a variable voltage according to the amount of light perceived.

voltage divider formula

  • Analog-Digital Conversion (ADC):  It is the process by which a physical quantity such as voltage, current, temperature, etc. is converted. in a binary number (or digital signal) in order to facilitate its handling by digital circuits such as a CPU. The Arduino carries out this process to know the amount of light perceived by the LDR and to be able to process it numerically.

We recommend studying these concepts in depth to develop the necessary skills to apply them in other different situations , since they will be very useful when connecting any other analog sensor to the arduino.

Example 1: Creating a Light meter

In this example we are going to build a circuit in such a way that when light falls on the photo resistor, the LED bulbs which are connected in the circuit glows. we connected 3 LEDs so that when high intensity light falls on LDR all the three LEDS glow accordingly when low intensity light falls on LDR only one light glows.

Required Components
  • 1 Arduino UNO R3
  • 1 Photoresistor (LDR)
  • 1 10 Kohm 1/4 W resistor
  • 3 220 or 330 ohm 1/4 W resistors
  • 3 LEDs 5mm
  • Breadboard cables
  • Breadboard

Diagram to use Arduino with LDR Photoresistance

We must assemble the circuit as shown in the following pictorial diagram. Remember that LEDs have polarity and must be respected. The longest pin is connected to the arduino board, while the shortest pin goes with a resistance to ground, the resistance can also be placed without problem between the anode of the led and the arduino (on the positive side of the led):

connection diagram of light meter

The armed circuit on the breadboard and the connections with the arduino are as follows:

light meter real circuit

Code (sketch) for Arduino with LDR Photoresistance.

The explanation for the code is as follows: First prepare the output pins where the LEDs are connected, the input pin where the resistive divider is connected and the serial port for communication. Then an infinite cycle is executed in which the voltage value is read on the LDR pin and the LEDs are actuated accordingly. We also transmit the value read by the ADC of the ATMEGA328 to the PC through the serial port of our arduino board. We have discussed the code in a way that is easy to understand.

/ *
 * Example of light meter with Arduino using an LDR photoresistor and
 * a group of LEDs to show the result of the readings. This sketch can
 * serve as the basis for other projects that require intensity measurement
 * light with photoresistance.
 * /

// Pin where the LEDs connect
int pinLed1 = 2;
int pinLed2 = 3;
int pinLed3 = 4;
// Analog input pin for the LDR
int pinLDR = 0;

// Variable where the LDR value is stored
int valueLDR = 0;  

void setup ()
  // We configure the pins where the LEDs are connected as outputs
  pinMode (pinLed1, OUTPUT);
  pinMode (pinLed2, OUTPUT);
  pinMode (pinLed3, OUTPUT);
  // configure the serial port
  Serial.begin (9600);

void loop ()
  // Turn off all LEDs whenever the cycle starts
  digitalWrite (pinLed1, LOW);
  digitalWrite (pinLed2, LOW);
  digitalWrite (pinLed3, LOW);

  // Store the read value of the ADC in a variable
  // The value read by the ADC (voltage) increases directly proportionally
  // regarding the light perceived by the LDR
  LDRvalue = analogRead (pinLDR);
  // Return the value read to our serial monitor in the Arduino IDE
  Serial.println (valueLDR);

  // Turn on the appropriate LEDs according to the ADC value
  if (LDRvalue> 256)
    digitalWrite (pinLed1, HIGH);
  if (LDRvalue> 512)
    digitalWrite (pinLed2, HIGH);
  if (LDRvalue> 768)
    digitalWrite (pinLed3, HIGH);
  // Wait a few milliseconds before updating
  delay (200);

To test our brand new light meter, we only have to place it on a light source and verify that the LEDs light up as the amount of light increases. In the terminal, we can also see how the value of the analog to digital conversion changes.

Example 2: Reading values of a photo resistor (LDR) on terminal.

To read a photoresistor, a voltage divider is created to be able to read it.

Required Components

  • 1x Arduino.
  • 1x LDR.
  • 1x breadboard.
  • 1x Resistor (10kΩ).

Circuit diagram:

As you can see we have 2 ways of connecting through circuit below circuit diagrams.

circuit diagram of ldr connected to arduino

Schematic circuit

Code or Sketch

2 codes are provided in which only the arithmetic calculations vary and are considered as integers, thus avoiding floating numbers and thus not slowing down the program execution. They can use the code that suits them best.

Use if the photoresistance is between GND and A0

const long A = 1000; // Dark resistance in KΩ.
const int B = 15; // Light resistance (10 Lux) in KΩ.
const int Rc = 10; // Calibration resistance in KΩ.
const int LDR = A0; // LDR pin.
int V;
int lighting;
<em>void setup ()</em>
<strong>Serial.begin</strong> (115200);
<em>void loop ()&nbsp;</em>// We define our sequence.
V = <strong>analogRead</strong> (LDR);
lighting = ((long) (1024-V) * A * 10) / ((long) B * Rc * V);
<strong>Serial.println</strong> (lighting);
<strong>delay</strong> (1000);

Use if the photoresistance is between Vcc and A0

const long A = 1000; // Dark resistance in KΩ.
const int B = 15; // Light resistance (10 Lux) in KΩ.
const int Rc = 10; // Calibration resistance in KΩ.
const int LDR = A0;&nbsp;// LDR pin.

int V;
int lighting;
<em>void setup ()</em>
<strong>Serial.begin</strong> (115200);
<em>void loop ()&nbsp;</em>// We define our sequence.
V =&nbsp;<strong>analogRead</strong> (LDR);
lighting = ((long) V * A * 10) / ((long) B * Rc * (1024-V));
<strong>Serial.println</strong> (lighting);
<strong>delay</strong> (1000);

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button