LDR Sensor Interfacing with PIC16F877A

In this tutorial we are going to see LDR Sensor Interfacing with PIC16F877A.


Before start this tutorial we should know below topics. If you know already, please go further.

Components Required

  • PIC16F877A Development Board
  • LDR Sensor
  • LED
  • LCD Module


The dominant of street lights, outside lights, a number of indoor home appliances, and so on are typically operated and maintained manually on many occasions. This is not only risky, however additionally leads to wastage of power with the negligence of personnel or uncommon circumstances in controlling these electrical appliances ON and OFF. Hence, we can utilize the light sensor circuit for automatic switch OFF the loads based on daylight’s intensity by employing a light sensor. This article discusses in brief about what is a light dependent resistor, how to interface with PIC16F877A.

LDRPhotoresistor-300x300 LDR Sensor Interfacing with PIC16F877A

What is a Light Dependent Resistor?

Light-Dependent-Resistor LDR Sensor Interfacing with PIC16F877AAn LDR or Light Dependent Resistor is also known as photo resistor, photocell, photoconductor. It is a one type of resistor whose resistance varies depending on the amount of light falling on its surface. When the light falls on the resistor, then the resistance changes. These resistors are often used in many circuits where it is required to sense the presence of light. Resistance will be decrease when the light increases. That means, In day time the output resistance will be less compared than night time.

Working Principle of  LDR

LDR-Working-Principle LDR Sensor Interfacing with PIC16F877AThis resistor works on the principle of photo conductivity. It is nothing but, when the light falls on its surface, then the material conductivity reduces and also the electrons in the valence band of the device are excited to the conduction band. These photons in the incident light must have energy greater than the band gap of the semiconductor material.This makes the electrons to jump from the valence band to conduction.

These devices depend on the light, when light falls on the LDR then the resistance decreases, and increases in the dark.When a LDR is kept in the dark place, its resistance is high and, when the LDR is kept in the light its resistance will decrease.

The figure below shows the curve between Resistance Vs Light Intensity curve for a particular light dependent resistor.

how_a-light_depedant_resistor_works_resistance_v_light_intensity LDR Sensor Interfacing with PIC16F877A

Applications of LDR

Lighting switch

The most obvious application for an LDR is to automatically turn on a light at a certain light level. An example of this could be a street light or a garden light.

Camera shutter control

LDRs can be used to control the shutter speed on a camera. The LDR would be used to measure the light intensity which then adjusts the camera shutter speed to the appropriate level.

LDR Sensor Interfacing with PIC16F877A

LDR Sensor is a Analog Sensor. So we need to use ADC for interfacing. Please go through ADC tutorial.

Circuit Diagram


  • RS :  RC0
  • RW : RC1
  • EN : RC2
  • Data Lines : RB0 – RB7


  • RA0 (AN0) : LDR Sensor’s one end


  • RD0 – LED

ldr-interfacing-with-pic16f877a LDR Sensor Interfacing with PIC16F877A

Source Code

This source code will control the LED based on the Light Intensity. If Light Intensity is high, it turns OFF the LED and If Light Intensity is Low it turns ON the LED.


ldr-interfacing-with-pic16f877a-output LDR Sensor Interfacing with PIC16F877A

%d bloggers like this: