LDR Sensor Interfacing with LPC2148

This article is a continuation of the series of tutorials on the LPC2148 Microcontroller (ARM7). 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 LPC2148 (ARM7). In this tutorial, we are going to see LDR Sensor Interfacing with LPC2148.

Prerequisites

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

Components Required

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

Introduction

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

LDR Sensor Interfacing with LPC2148

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 decrease when the light increases. That means, In the daytime, the output resistance will be less compared to nighttime.

Working Principle of  LDR

LDR Sensor Interfacing with LPC2148This 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.

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 LPC2148

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

Circuit Diagram

LCD:

  • RS:  P1.16
  • RW: P1.17
  • EN: P1.18
  • Data Lines: P1.24 – P1.31

ADC:

  • P0.4 (ADC0.6): LDR Sensor’s one end

LED:

  • P0.31 – LED

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.

[You can get the source code from GitHub]

#include<lpc214x.h>

#define LED (1U<<31)

#define bit(x) (1<<x)

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

unsigned int val;
unsigned int adc(int,int);


int main()
{
    IO1DIR=0xffffffff;
    IO0DIR=0x80000000;
    PINSEL0=0x0300;
    VPBDIV=0x02;
    lcd_init();
    while(1) {
        cmd(0x80);
        adc(0,6);
        show("LIGHT INT : ");
        if(val>800) {
            show("LOW ");
            IO0SET = LED;
        } else {
            show("HIGH");
            IO0CLR = LED;
        }
    }
}

void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x01);
    cmd(0x06);
    cmd(0x0c);
    cmd(0x80);
}

void cmd(unsigned char a)
{
    IO1CLR=0xFF070000;
    IO1SET=(a<<24);
    IO1CLR=bit(16);             //rs=0
    IO1CLR=bit(17);             //rw=0
    IO1SET=bit(18);             //en=1
    lcd_delay();
    IO1CLR=bit(18);             //en=0
}

void dat(unsigned char b)
{
    IO1CLR=0xFF070000;
    IO1SET=(b<<24);
    IO1SET=bit(16);             //rs=1
    IO1CLR=bit(17);             //rw=0
    IO1SET=bit(18);             //en=1
    lcd_delay();
    IO1CLR=bit(18);             //en=0
}

void show(unsigned char *s)
{
    while(*s) {
        dat(*s++);
    }
}

void lcd_delay()
{
    unsigned int i;
    for(i=0;i<=3000;i++);
}

unsigned int adc(int no,int ch)
{
    switch(no)                                    //select adc
    {
        case 0: AD0CR=0x00200600|(1<<ch);       //select channel
                AD0CR|=(1<<24);                 //start conversion
                while((AD0GDR& (1U<<31))==0);
                val=AD0GDR;
                break;

        case 1: AD1CR=0x00200600|(1<<ch);       //select channel
                AD1CR|=(1<<24);                 //start conversion
                while((AD1GDR&(1U<<31))==0);
                val=AD1GDR;
                break;
    }
    val=(val >> 6) & 0x03FF;                   // bit 6:15 is 10 bit AD value

    return val;
}



Output

LDR Sensor Interfacing with LPC2148

In our next tutorial, we will see how to interface the Sound detecting sensor with LPC2148 (ARM7). If you want to use FreeRTOS on LPC2148, then please refer FreeRTOS series.

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

Subscribe
Notify of
guest

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

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents