Controlling LED with PIC Microcontroller using LDR
Using a Light Dependent Resistor (LDR) with a PIC Microcontroller |
In this tutorial, we will explore how to use a Light Dependent Resistor (LDR) with a PIC microcontroller. The LDR is a type of sensor that changes its resistance in response to light. This makes it an ideal component for detecting light levels and triggering actions accordingly. |
Components Required |
- PIC16F8778 PIC microcontroller
- 20 MHz crystal oscillator
- 22 pF capacitors
- 5 mm LED
- LDR sensor
- Breadboard
- Jumper wires
|
Circuit Diagram and Connections |
The circuit diagram for this project is as follows:
- Place the PIC microcontroller on the breadboard.
- Connect pin 11 of the PIC to the positive terminal of the breadboard.
- Connect pin 12 of the PIC to the negative terminal of the breadboard.
- Connect pin 31 of the PIC to the negative terminal of the breadboard.
- Connect pin 32 of the PIC to the positive terminal of the breadboard.
- Connect the crystal oscillator between pins 13 and 14 of the PIC.
- Connect a 22 pF capacitor between the negative terminal of the breadboard and the crystal oscillator.
- Connect a 10 kOhm resistor between pin 1 of the PIC and the positive terminal of the breadboard.
- Connect one terminal of the LDR sensor to the positive terminal of the breadboard.
- Connect a 10 kOhm resistor between the negative terminal of the breadboard and the other terminal of the LDR sensor.
- Connect the LDR terminal to analog pin A0 of the PIC microcontroller.
- Connect the negative terminal of the LED to the negative terminal of the breadboard.
- Connect the positive terminal of the LED and pin 1 of port B of the PIC through a 220 ohms resistor.
|
Programming the PIC Microcontroller |
To program the PIC microcontroller, we will use two software: Micro C Pro and PickKit 2.
- Open Micro C Pro and create a new project.
- Enter the project name, device name, and frequency of the crystal oscillator.
- Create a new project and enter the code:
|
// Define LED pin at RB1
#define LED_PIN TRISBbits.TRISB1
// Define LDR threshold
#define LDR_THRESHOLD 512
// Configure analog to digital converter settings
void configADC() {
ADCON = 0x80; // Set ADC on and configure settings
}
// Initialize ports and variables
void init() {
LED_PIN = 0; // Set LED pin as output
TRISBbits.TRISB1 = 0; // Set direction of pin RB1 as output
configADC(); // Configure ADC settings
}
// Main program loop
while(1) {
// Read analog value from channel 1 using ADC
signed int LDR_value = ADC_Read(1);
// Check if light level is low (below threshold)
if(LDR_value < LDR_THRESHOLD) {
LED_PIN = 1; // Turn on LED
} else {
LED_PIN = 0; // Turn off LED
}
// Add a delay of 500 milliseconds before the loop ends
__delay_ms(500);
}
|
Uploading the Program to the PIC Microcontroller |
To upload the program to the PIC microcontroller, follow these steps:
- Open PickKit 2 software.
- Erase the chip.
- Import the hex file created in Micro C Pro.
- Upload the program to the PIC microcontroller.
|
Testing the Circuit |
Once the program is uploaded, test the circuit by shining light on the LDR sensor. The LED should turn on when the light level is below the threshold and turn off otherwise.
|
Conclusion |
In this tutorial, we have successfully used a Light Dependent Resistor (LDR) with a PIC microcontroller to detect light levels and trigger actions accordingly. This project demonstrates the basic principles of using sensors with microcontrollers.
|
LDR Circuit |
The LDR (Light Dependent Resistor) circuit is an electronic circuit that utilizes a light-dependent resistor as its primary component. The LDR's resistance changes in response to variations in the ambient light intensity, making it an ideal sensor for detecting and measuring light levels. |
Background |
The concept of LDRs dates back to the early 20th century when scientists discovered that certain materials, such as selenium and cadmium sulfide, exhibited a decrease in electrical resistance when exposed to light. This property made them suitable for use in various applications, including photography, where they were employed to control exposure levels. |
Working Principle |
In an LDR circuit, the light-dependent resistor is connected in series with a fixed resistor and a voltage source. When light falls on the LDR, its resistance decreases, allowing more current to flow through the circuit. This change in current is then measured using various techniques, such as voltage measurement or digital signal processing. |
Applications |
LDR circuits have numerous applications across various fields, including: |
|
Photography: LDRs are used in cameras to control exposure levels and adjust the aperture accordingly. |
|
Security Systems: LDR circuits can detect changes in ambient light, making them suitable for use in intruder detection systems. |
|
Automotive Systems: LDRs are used in some vehicles to control the brightness of dashboard displays and headlights based on ambient light levels. |
Controlling LED with PIC Microcontroller using LDR |
In this article, we will explore how to control an LED using a PIC microcontroller and a Light Dependent Resistor (LDR). The LDR is used as a sensor to detect the light intensity and based on that, the LED is turned ON or OFF. |
Hardware Components |
- PIC Microcontroller (e.g., PIC16F877A)
- LDR (Light Dependent Resistor)
- LED (Light Emitting Diode)
- Resistors (1kΩ, 10kΩ)
- Breadboard and Jumper Wires
|
Circuit Diagram |
 |
The LDR is connected to the PIC microcontroller's analog input pin (AN0). The LED is connected to one of the digital output pins (RB0) through a resistor. A voltage divider circuit is formed using two resistors (1kΩ and 10kΩ) to provide a reference voltage for the ADC. |
Software Components |
- MPLAB X IDE (or any other PIC microcontroller programming software)
- PIC16F877A Library (for ADC and Digital I/O functions)
|
Code Explanation |
The code starts by configuring the PIC microcontroller's ADC module and setting the reference voltage. Then, it reads the analog value from the LDR using the ADC module.
#include <xc.h>
// Configuration bits
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF);
// Function to read analog value from LDR
unsigned int read_ldr(void) {
ADCON0bits.ADON = 1; // Turn ON ADC module
__delay_ms(10); // Wait for ADC to settle
ADCON0bits.GO_DONE = 1; // Start conversion
while (ADCON0bits.GO_DONE); // Wait for conversion to complete
return ADRES; // Return the result
}
void main(void) {
TRISBbits.RB0 = 0; // Set RB0 as digital output
PORTBbits.RB0 = 0; // Initialize LED as OFF
while (1) {
unsigned int ldr_value = read_ldr();
if (ldr_value > 512) { // If light intensity is high
PORTBbits.RB0 = 1; // Turn ON LED
} else {
PORTBbits.RB0 = 0; // Turn OFF LED
}
}
}
|
Working Principle |
The LDR's resistance changes in response to the light intensity. When the light intensity is high, the LDR's resistance decreases and vice versa. The PIC microcontroller reads this change in resistance using its ADC module and converts it into a digital value.
Based on the digital value, the PIC microcontroller controls the LED connected to one of its digital output pins. If the light intensity is high (i.e., LDR's resistance is low), the LED is turned ON; otherwise, it remains OFF.
|
Q1: What is an LDR and how does it work? |
An LDR (Light Dependent Resistor) is a type of resistor that changes its resistance value in response to changes in light intensity. It works by using a photoconductive material that reduces its resistance when exposed to light. |
Q2: What is a PIC microcontroller and why is it used? |
A PIC (Peripheral Interface Controller) microcontroller is a small computer on a single integrated circuit. It is used for controlling and interacting with external devices, such as LEDs, sensors, and motors. |
Q3: How can an LDR be used to control an LED with a PIC microcontroller? |
An LDR can be connected to the analog input of a PIC microcontroller. The microcontroller reads the voltage drop across the LDR, which changes with light intensity, and uses this value to control the brightness of an LED. |
Q4: What is the advantage of using an LDR over other sensors? |
The main advantage of using an LDR is its simplicity and low cost. It also provides a high degree of linearity between light intensity and resistance value, making it suitable for accurate brightness control. |
Q5: What PIC microcontroller can be used to control an LED with an LDR? |
Any PIC microcontroller with analog input capabilities can be used. Some popular choices include the PIC16F877A, PIC18F4550, and PIC24FJ64GA004. |
Q6: What is the circuit diagram for connecting an LDR to a PIC microcontroller? |
The circuit typically involves connecting the LDR between Vcc and GND, with the wiper of a potentiometer connected to the analog input pin of the PIC. The LED is then connected to a digital output pin. |
Q7: What is the programming language used for PIC microcontrollers? |
The most commonly used programming languages for PIC microcontrollers are C, Assembly, and MikroBUS. |
Q8: Can multiple LEDs be controlled with a single LDR using a PIC microcontroller? |
Yes, by using multiple digital output pins of the PIC microcontroller to control each LED separately. The brightness of each LED can then be controlled individually based on the reading from the LDR. |
Q9: What is the range of light intensity that an LDR can detect? |
The range of light intensity that an LDR can detect typically ranges from a few lux to several thousand lux, depending on the specific type and model of LDR used. |
Q10: Can the circuit be modified for other types of sensors instead of an LDR? |
|
Pioneers/Companies |
Description |
1. NXP Semiconductors |
Developed the first PIC microcontroller in 1993, which revolutionized the field of embedded systems. |
2. Microchip Technology |
Acquired the PIC microcontroller product line from General Instrument in 1985 and has since become a leading provider of microcontrollers. |
3. Texas Instruments (TI) |
Introduced the first microcontroller with an LDR (Light Dependent Resistor) interface, enabling control of LEDs using light intensity. |
4. Atmel Corporation |
Developed a range of microcontrollers with built-in LDR interfaces, making it easier to implement LED control applications. |
5. STMicroelectronics |
Released a series of microcontrollers with advanced LDR and LED driver capabilities, enabling sophisticated lighting control systems. |
6. Analog Devices (ADI) |
Developed specialized ICs for controlling LEDs using LDR sensors, offering high accuracy and reliability. |
7. ON Semiconductor |
Introduced a range of LED driver ICs with integrated LDR interfaces, enabling efficient and compact lighting solutions. |
8. Rohm Semiconductor |
Developed advanced microcontrollers with built-in LDR and LED driver capabilities, targeting IoT and smart lighting applications. |
9. Infineon Technologies |
Released a series of microcontrollers with integrated LDR and LED driver capabilities, focusing on automotive and industrial lighting systems. |
10. Dialog Semiconductor |
Developed specialized ICs for controlling LEDs using LDR sensors, targeting low-power and energy-efficient applications. |
Component |
Description |
Technical Details |
PIC Microcontroller |
16F877A or similar |
- 8-bit microcontroller with 14KB of program memory
- 256 bytes of EEPROM data memory
- 368 bytes of RAM
- 20 MHz clock frequency
|
LDR (Light Dependent Resistor) |
GL5528 or similar |
- Resistance range: 100 ohms to 1M ohm
- Sensitivity: 10-20% per 10 lux change in light intensity
- Power dissipation: 0.25W
|
LED (Light Emitting Diode) |
5mm or similar |
- Forward voltage: 1.8-2.2V
- Reverse voltage: 5V
- Current: 20mA
- Luminous intensity: 1000-2000 mcd
|
Resistors |
R1, R2 (1k ohm), R3 (4.7k ohm) |
- Tolerance: ±5%
- Power dissipation: 0.25W
|
Circuit Diagram |
|
|
Code Snippet (MPASM) |
|
LIST p=16F877A
INCLUDE <p16F877A.inc>
__CONFIG _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF
ORG 0x00
GOTO MAIN
MAIN
BSF STATUS, RP0 ; Select Bank1
CLRF ANSEL ; Configure PortA as digital I/O
BCF STATUS, RP0 ; Select Bank0
MOVLW 0xFF ; Initialize PORTB as output
MOVWF TRISB
LDR_LOOP
MOVLW 0x00 ; Read LDR value
MOVWF ADCON0
BSF ADCON0, GO ; Start conversion
BTFSS ADCON0, DONE ; Wait for conversion complete
MOVF ADRESH, WREG ; Get result
SUBLW 0x10 ; Subtract threshold (adjust as needed)
BTFSC STATUS, CARRY
BSF PORTB, 0 ; Turn on LED if LDR value is below threshold
ELSE
BCF PORTB, 0 ; Turn off LED otherwise
ENDIF
GOTO LDR_LOOP
END
|
|