You Are Here: Home » AVR ATmega Projects » Light Intensity Measurement using LDR and AVR Microcontroller

Light Intensity Measurement using LDR and AVR Microcontroller

In this project we are going to interface LDR with ATMEGA8 microcontroller, and with this we can measure LIGHT INTENSITY in the area. In ATMEGA8, we are going to use 10bit ADC (Analog to Digital Conversion) feature to measure the light intensity.

Am LDR is a transducer which changes its resistance when LIGHT falls on its surface changes.  LDR sensor is available in different sizes and shapes.

Light Intensity Measurement using LDR and AVR Microcontroller

Light Intensity Measurement using LDR and ATmega8 Microcontroller

LDRs are made from semiconductor materials to enable them to have their light sensitive properties. There are many types of materials used, but one which is popular is CADMIUM SULPHIDE (CdS). These LDRs or PHOTO REISTORS work on the principle of “Photo Conductivity”. Now what this principle says is whenever light falls on the surface of the LDR (in this case) the conductance of the element increases or in other words the resistance of the LDR decreases when the light falls on the surface of the LDR. This property of the decrease in resistance for the LDR is achieved because it is a property of semiconductor material used on the surface. LDR are used most of times to detect presence of light or for measuring the intensity of light.

There are different types of LDR as shown in above figure and each have different specifications. Typically an LDR will have 1MΩ-2MΩ at total darkness, 10-20KΩ at 10 LUX, 2-5KΩ at 100 LUX. The typical resistance to LUX graph of an LDR is shown in figure.

As shown in above figure, the resistance between the two contacts of sensor decreases with light intensity or the conductance between two contacts of sensor increases.

Now for converting this change in resistance to change in voltage, we are going to use voltage divider circuit. In this resistive network we have one constant resistance and other variable resistance. As shown in figure, R1 here is a constant resistance and R2 is FORCE sensor which acts as a resistance.

The midpoint of branch is taken to measurement. When resistance R2 changes , the Vout changes with it linearly. So with this we have a voltage which changes with weight.

Now important thing to note here is, the input taken by the controller for ADC conversion is as low as 50µAmp. This loading effect of resistance based voltage divider is important as the current drawn from Vout of voltage divider increases the error percentage increases, for now we need not worry about loading effect.

What we are going to do here is we are going take two resistors and form a divider circuit so that for a 25Volts Vin, we get a 5Volt Vout. So all we have to do is multiply the Vout value with “5” in the program in order to get the real input voltage.


Hardware: ATMEGA8, power supply (5v), AVR-ISP PROGRAMMER, JHD_162ALCD (16*2LCD), 100uF capacitor, 100nF capacitor (5 pieces), 10KΩ resistor, LDR (Light Dependent Resistor).

Sofware: Atmel studio 6.1, progisp or flash magic.

Circuit Diagram & Working Explanation

In circuit PORTD of ATMEGA8 is connected to data port LCD. In 16*2 LCD there are 16 pins over all if there is a back light, if there is no back light there will be 14 pins. One can power or leave the back light pins. Now in the 14 pins there are 8 data pins (7-14 or D0-D7), 2 power supply pins (1&2 or VSS&VDD or gnd&+5v), 3rd pin for contrast control (VEE-controls how thick the characters should be shown) and 3 control pins (RS&RW&E)

In the circuit, you can observe that I have only took two control pins. The contrast bit and READ/WRITE are not often used so they can be shorted to ground. This puts LCD in highest contrast and read mode. We just need to control ENABLE and RS pins to send characters and data accordingly.

The connections for LCD are given below:

PIN1 or VSS ——————ground

PIN2 or VDD or VCC————+5v power

PIN3 or VEE—————ground (gives maximum contrast best for a beginner)

PIN4 or RS (Register Selection) —————PB0 of uC

PIN5 or RW (Read/Write) —————–ground (puts LCD in read mode eases the communication for user)

PIN6 or E (Enable) ——————-PB1 of uC

PIN7 or D0—————————–PD0 of uC

PIN8 or D1—————————–PD1 of uC

PIN9 or D2—————————–PD2 of uC

PIN10 or D3—————————–PD3 of uC

PIN11 or D4—————————–PD4 of uC

PIN12 or D5—————————–PD5 of uC

PIN13 or D6—————————–PD6 of uC

PIN14 or D7—————————–PD7 of uC

In the circuit you can see that we have used 8bit communication (D0-D7) however this is not a compulsory, we can use 4bit communication (D4-D7) but with 4 bit communication program becomes a bit complex. So from mere observation from above table we are connecting 10 pins of LCD to controller in which 8 pins are data pins and 2 pins for control.Schematic Light Intensity Measurement using LDR and AVR Microcontroller

The voltage across R2 is not completely linear; it will be a noisy one. To filter out the noise capacitors are placed across each resistor in the divider circuit as shown in figure.

In ATMEGA8, we can give Analog input to any of FOUR channels of PORTC, it doesn’t matter which channel we choose as all are same. We are going to choose channel 0 or PIN0 of PORTC. In ATMEGA8, the ADC is of 10 bit resolution, so the controller can detect a minimum change of Vref/2^10, so if the reference voltage is 5V we get a digital output increment for every 5/2^10 = 5mV. So for every 5mV increment in the input we will have a increment of one at digital output.

Now we need to set the register of ADC based on the following terms:

1. First of all we need to enable the ADC feature in ADC.

2. Here are going to get a maximum input voltage for ADC conversion is +5V. So we can set up maximum value or reference of ADC to 5V.

3. The controller has a trigger conversion feature that means ADC conversion takes place only after an external trigger, since we don’t want that we need to set the registers for the ADC to run in continuous free running mode.

4. For any ADC, frequency of conversion (Analog value to Digital value) and accuracy of digital output are inversely proportional. So for better accuracy of digital output we have to choose lesser frequency. For normal ADC clock we are setting the presale of ADC to maximum value (2). Since we are using the internal clock of 1MHZ, the clock of ADC will be (1000000/2).

These are the only four things we need to know to getting started with ADC.

All the above four features are set by two registers,

For more detail: Light Intensity Measurement using LDR and AVR Microcontroller

Leave a Comment

You must be logged in to post a comment.

Read previous post:
Fire Alarm System using AVR Microcontroller

In this project, we are going to make a Fire Alert System using ATMEGA8 microcontroller and fire sensor. Fire sensor can...

Scroll to top