LDR Sensor Interfacing with PIC16F877A

This article is a continuation of the series of tutorials on the PIC16F877A Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have interfaced the Bluetooth module with PIC16F877A. In this tutorial, we are going to see LDR Sensor Interfacing with PIC16F877A.


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

Components Required

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


The dominance 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 a photoresistor, photocell, photoconductor. It is 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 decreased when the light increases. That means, In the daytime, the output resistance will be less compared to nighttime.

Working Principle of  LDR

This resistor works on the principle of photoconductivity. 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 bandgap of the semiconductor material. This makes the electrons 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 an LDR is kept in a 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.

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

#define delay for(i=0;i<=1000;i++)
#define rs RC0
#define rw RC1
#define e RC2
#define LED RD0


unsigned int adc();

void lcd_int();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);

int i;

void main()
    unsigned int val;
    TRISB=TRISC=0;          //Port B and Port C is Output (LCD)
    TRISD = 0;              //Port D is output LED
    TRISA0=1;               //RA0 is input (ADC)
    while(1) {

        val = adc();
        show("LIGHT INT : ");
        if(val>150) {
            show("LOW ");
            LED = 1;
        } else {
            LED = 0;

void lcd_int()

void cmd(unsigned char a)

void dat(unsigned char b)

void show(unsigned char *s)
    while(*s) {

unsigned int adc()
    unsigned int adcval;
    ADCON1=0xc0;                    //right justified
    ADCON0=0x85;                    //adc on, fosc/64
    while(GO_nDONE);                //wait until conversion is finished
    adcval=((ADRESH<<8)|(ADRESL));    //store the result
    return adcval;  


LDR Sensor Interfacing with PIC16F877A

In our next tutorial, we will see how to interface the Sound detecting sensor with PIC16F877A.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Notify of

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

Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents