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.

What is a Light Dependent Resistor?

An 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

This 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.

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 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.


0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
daniel rengifo
daniel rengifo
November 6, 2018 3:50 PM

please the code in which software was made?

EmbeTronicx India
EmbeTronicx India
Reply to  daniel rengifo
November 7, 2018 3:07 AM

Hi Daniel,
We were using MP Lab IDE to compile this.

June 29, 2019 10:24 PM

Sir! Please explain that how do you get adc_val=(adc_val/3)-1; and also please explain the criteria of selecting adc_val value. It will be very helpfull please explain this

Would love your thoughts, please comment.x
%d bloggers like this: