Tutorials

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.

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

Use if the photoresistance is between Vcc and A0

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

Back to top button