LDR Interfacing using PIC-Microcontroller

LDR interfacing

The LDR interfacing using pic microcontroller circuit is simple but it requires more coding part. This circuit consists of pic18F4550 , LCD, LDR and Basic electronic component.

Microcontroller is an intelligent electronic device which can perform complex tasks and take decisions. It is used in variety of applications. However to make it more useful certain external circuitry is added to increase its applications. Microcontrollers do not have any light sensor which can enable it to measure light or perform applications related to light. Therefore certain external components are interfaced with microcontroller through which light can be measured and sensed. So measure light via microcontroller, it is to be converted into other quantities like voltage, resistance etc which can be read by microcontroller.

  Components Used : –

  • LDR
  • Power Supply
  • Microcontroller (PIC 18F4550)
  • Liquid Crystal Display
  • Battery


LDR is used for calculating the light intensity of the environment. The light dependent resistor is connected to ADC1 (PC1) pin of the micro controller. The analog light value is converted to digital value using ADC.

Microcontroller (PIC 18F4550)

PIC is a family of Harvard architecture microcontrollers made by microchip technology, derived from the PIC1640 originally developed by general instrument’s microelectronics divison. The name PIC initially referred to “Programmable interface controller”, but shortly thereafter was renamed as “Programmable Intelligent Computer ”. PIC is popular due to their low cost , wide availability ,large user base, extensive collection of application notes ,availability of low cost or free development tools and serial programming (and re-programming with flash memory) capability.


  • The above circuit implements LDR interfacing using PIC microcontroller.
  • The circuit can be used in automation of street light control thereby reducing manual control and human errors.
  • It can be used in Solar tracker circuit.
  • When integrated with IR sensors, the same circuit can be used to control the street lights depending on the traffic intensity.


In order to measure light using microcontroller, the following program is written in C language, comipled in  MIKRO should be embedded in microcontroller.


float in_voltage, light_lux;           // Initialize variable

// Lcd module connections start

sbit LCD_RS at LATC3_bit;

sbit LCD_EN at LATC2_bit;

sbit LCD_D4 at LATC4_bit;

sbit LCD_D5 at LATC4_bit;

sbit LCD_D6 at LATC6_bit;

sbit LCD_D7 at LATC7_bit;


sbit LCD_RS_Direction at TRISC3_bit;

sbit LCD_EN_Direction at TRISC2bit;

sbit LCD_D4_Direction at TRISC4_bit;

sbit LCD_D5_Direction at TRISC5_bit;

sbit LCD_D6_Direction at TRISC6_bit;

sbit LCD_D7_Direction at TRISC7_bit;      // Lcd module connections ends

// copy above statments into your code for every project involving LCD

char a[5];             // declare a char arrays

void main(){

TRISA=0XFF;       // make PORTA input port

TRISC=0;              // make PORTC output port

ADC_Init();         // Initialize ADC module with default settings

Lcd_Init();           // Initialize Lcd

Lcd_Cmd(_LCD_CLEAR);               // Clear display

Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off

Lcd_Out(1,1,”Welcome to”);      // display the welcome logo on the LCD screen at position ROW=1 COLUMN=1


delay_ms(5000);              // keep displaying logo for 5s


Lcd_Cmd(_LCD_CLEAR);               // Clear display


in_voltage= ADC_Read(0);          // Read analog value from channel 0


in_voltage=(in_voltage*4.88)/1000;        //Converting analogue value to digital. For a 10 bit //conversion process please follow how to interface relay with microcontroller

if (in_voltage>2.43)         //checking voltage of LDR.  Checking which equation to apply

{light_lux = (-35.514*in_voltage)+ 154.27;            // Equation a is used

floattostr(light_lux,a);}  // converting calculated value to characters string for display


else if (in_voltage<= 2.43 && in_voltage > 1.26) //checking voltage of LDR.  Checking which equation to apply

{light_lux = (-149.5*in_voltage)+ 404.2; // Equation b is used

floattostr(light_lux,a);}  // converting calculated value to characters string for display


else if (in_voltage<= 1.26 )           //checking voltage of LDR.  Checking which equation to apply

{light_lux = (-532*in_voltage)+ 896;        // Equation c is used

floattostr(light_lux,a);}  // converting calculated value to characters string for display


else if ( in_voltage<0.79 || in_voltage > 4.21)     //Checking if lighr is out of range

{  a[14]=”out of range” ;  }            //Display out of range


lcd_cmd(_lcd_clear);Lcd_Cmd(_LCD_CURSOR_OFF);      //Clear display//Cursor off



lcd_out(1,1,a);  // display the temperature centigrade value stored character text on the LCD screen at position ROW=1 COLUMN=2

lcd_out(1,13,”lux”);        // display the character ‘C’ on the LCD screen at position ROW=1 COLUMN=13

delay_ms(2000);              // wait 2s before taking second value


Leave a Reply

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