Skip to content

Light Sensor

Introduction

In this guide, we will learn how to interface an LDR (Light Dependent Resistor) sensor with the microcontroller. The LDR sensor is used to detect light intensity. We will use the P89V51RD2 GPIO pins to connect the LDR sensor and control an LED based on the light intensity detected by the sensor. When the light intensity is low, the LED will turn on, indicating low light conditions.

Problem Statement

The objective of this project is to interface the LDR sensor with the P89V51RD2 and control an LED based on the light intensity detected by the sensor.

Requirements

To successfully complete this project, you will need the following components:

  1. P89V51RD2 development board
  2. LDR sensor
  3. LED
  4. Resistor (220/330 ohms)
  5. Jumper wires
  6. Breadboard

Circuit Diagram

  1. Connect P1.1 to LED
  2. Connect P0.0 to LDRs Output
  3. Connect +5v of LDR Sensor and ground to VCC and ground of microcontroller respectively.
Pin diagram

Code

  • Copy and paste the following code into main.c or download it from here:
c
#include <reg51.h>

void delay(void);

sbit ldr = P0 ^ 0;
sbit led = P1 ^ 1;

void main()
{
    P0 = 0xFF; //assigning as input port
    P1 = 0x00; //assigning as output port

    while (1)
    {
        if (ldr == 0)
            led = 1;
        delay();

        if (ldr == 1)
            led = 0;
        delay();
    }
}

void delay()  //delay function
{
    int i, j;
    for (i = 0; i < 500; i++)
    {
        for (j = 0; j < 1000; j++)
        {
        }
    }
}

Output

After uploading the code to the P89V51RD2, the program will continuously monitor the light intensity detected by the LDR sensor. If the light intensity is low (indicating low light conditions), the LED will turn on. Conversely, if the light intensity is high (indicating sufficient light conditions), the LED will turn off.

Conclusion

By following this guide, you have successfully interfaced the LDR sensor with the P89V51RD2 microcontroller. You can now control an LED based on the light intensity detected by the sensor. This project can be expanded further by incorporating additional features, such as adjusting the LED brightness based on the light intensity or integrating it into a home automation system.