In this guide, we will learn how to interface an LDR (Light Dependent Resistor) sensor with the LPC2148 development board. By connecting the LDR sensor to the LPC2148, we can measure light intensity and use it for various applications like automatic lighting systems, security systems, and more.
The objective of this guide is to interface the LDR sensor with the LPC2148 development board to measure light intensity accurately and utilize the data for different purposes. When the light will fall on LDR sensor all the LEDs will be turned off and when there will be dark all the LEDs will be turned on.
To successfully complete this project, you will need the following components:
LPC2148 Development board
LDR Sensor
16x2 LCD
2 USB cables, for power and programming.
This experiment involves connecting a new external device this time, which is the LDR sensor. Let's understand the connections for the LDR sensor module which is similar to IR sensor that we saw in previous experiment:
The LDR sensor module has three pins: DO, GND, and VCC.
The DO pin will be connected to pin 24 of Port 1, as specified in our experiment (you can choose any available pin on the board).
Connect the power pins of the LDR sensor to the microcontroller's power pins.
And the in-built LEDs D1, D2, D3 and D4 connected to 16th, 17th, 18th and 19th pin of port 1.
Open the Keil IDE and create a new project with name ldr_sensor by clicking on project in the menu bar. (Refer the steps from Getting started page to create new project - do not skip any step from the process)
The code for the LDR sensor will work based on the same logic as the IR sensor. You can even run the same code after connecting the LDR sensor to the board and check the result.
Copy the code given below into the main.c file and save the main.c file. You can also copy and paste the code from here into the file.
#include <lpc214x.h> //Includes LPC2148 register definitions
#define LED1_ON() IO1SET=(1<<16) //Macro Functions to turn ON LED
#define LED2_ON() IO1SET=(1<<17)
#define LED3_ON() IO1SET=(1<<18)
#define LED4_ON() IO1SET=(1<<19)
#define LED1_OFF() IO1CLR=(1<<16) //Macro Functions to turn OFF LED
#define LED2_OFF() IO1CLR=(1<<17)
#define LED3_OFF() IO1CLR=(1<<18)
#define LED4_OFF() IO1CLR=(1<<19)
#define LDR (IO1PIN & (1<<24)) //Macro Functions to read LDR status LPC2148 dev. board
void Delay(unsigned char j)
{
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}
int main(void)
{
PINSEL0 = 0x00000000; // Enable GPIO on all pins
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
IO1DIR = (0<<24);
IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P1.16, P1.17, P1.18, P1.19 as Output to control LEDs
while(1)
{
if(LDR == 0) {
LED1_OFF();
LED2_OFF();
LED3_OFF();
LED4_OFF();
}
Delay(100);
LED1_ON();
LED2_ON();
LED3_ON();
LED4_ON();
}
}
After saving the file click on build icon or press F7. You should be able to see the build output with 0 errors and warnings.
Now the hex file has been generated i.e. your program is compiled for uploading on the board. Open Flash Magic and do the following changes:
a. Change the Device to LPC2148
b. Select the Serial Port
c. Change the baud rate to 9600
d. Finally in the firmware section select the generated hex file.
e. In the erase section select entire device (refer the image below).
To upload the code do the following:
i. Connect a DB9 cable between the selected COMx port on your PC and the UART0 port on LPC2148 development board.
ii. Plug in an AC/DC 9V/1A supply to the development board and slide ON/OFF switch to ON position.
iii. Enter in to boot load mode by keeping the BOOT switch pressed and then press RESET switch.
iv. You should see LED D4 glowing which means board is now in programming mode.
Click on Start in the Flash Magic tool. This will upload the code to the board.
Once you have uploaded the code into board then exit the programming mode by pressing reset button once.
Upon successful execution of the program, if the light intensity surpasses a threshold all LEDs connected to port 1 of the LPC2148 will turn off. If the light intensity falls below the threshold, the LEDs will turn on.
By following this guide, you have successfully interfaced an LDR sensor with the LPC2148 development board. You can now measure light intensity and utilize the data for your desired applications. Feel free to modify the code to suit your specific requirements and explore further possibilities with the LPC2148 and LDR sensor.