Introduction
Energy conservation is a critical issue in modern society, and automatic street lighting systems play an important role in reducing unnecessary energy consumption. An Automatic Street Light Controller is designed to turn street lights on and off automatically based on the ambient light levels. This project will show you how to build such a system using a PIC microcontroller and a Light Dependent Resistor (LDR) to detect the light levels.
System Overview
The Automatic Street Light Controller operates by sensing the ambient light level using an LDR. When the light level falls below a certain threshold, indicating it is evening or nighttime, the street lights will turn on automatically. Conversely, when the light level rises in the morning, the street lights will turn off. The PIC microcontroller serves as the brain of this system, processing the input from the LDR and controlling the relay that switches the street lights on or off.
Components Required
PIC Microcontroller (e.g., PIC16F877A) - The main controller for processing the LDR data and controlling the street lights.
Light Dependent Resistor (LDR) - Senses ambient light levels.
Relay Module - Controls the street lights by switching them on or off.
Transistor (e.g., BC547) - Acts as a switch to control the relay.
Resistors and Capacitors - For interfacing and setting up the circuit.
16x2 LCD Display (optional) - Displays system status and light levels.
Power Supply - To power the system.
Breadboard and Connecting Wires - For assembling the circuit.
Street Light (or any load) - The light that will be controlled.
Circuit Diagram
Below is a basic circuit diagram showing how the components are connected to the PIC microcontroller:
[Insert a circuit diagram here showing connections:
- LDR connected to an analog input pin (e.g., AN0) of the PIC microcontroller.
- Relay module connected to the digital output pin via a transistor.
- Optional LCD connected to PORTD of the PIC microcontroller for displaying system status.
- Power supply connections to the PIC, LDR, relay, and street light.]
Working Principle
The LDR is connected to one of the analog input pins of the PIC microcontroller. The resistance of the LDR changes with the amount of light falling on it, which causes a corresponding change in voltage at the analog input pin. The PIC microcontroller reads this analog voltage using its Analog-to-Digital Converter (ADC) module, compares it with a predefined threshold, and then decides whether to turn the street lights on or off. The relay module, controlled by the PIC microcontroller through a transistor, is used to switch the street light on or off.
Step-by-Step Implementation
Step 1: Setting Up the Development Environment
Before starting with the hardware, ensure you have the necessary software tools installed:
MPLAB X IDE - Integrated Development Environment for writing and debugging code for PIC microcontrollers.
XC8 Compiler - A compiler for converting C code into a hex file that the PIC microcontroller can understand.
Step 2: Writing the Code
Begin by initializing the PIC microcontroller, setting up the ADC module to read the input from the LDR, and configuring the output pin to control the relay.
Code Outline:
#include <xc.h>
#include "lcd.h" // Include LCD library if using an LCD
#define _XTAL_FREQ 20000000 // Define the crystal frequency (20MHz)
#define LIGHT_THRESHOLD 512 // ADC value threshold for light detection
// Configuration bits for PIC16F877A
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-up Timer Disabled
#pragma config BOREN = ON // Brown-out Reset Enabled
#pragma config LVP = OFF // Low Voltage Programming Disabled
#pragma config CPD = OFF // Data EEPROM Memory Code Protection Disabled
#pragma config WRT = OFF // Flash Program Memory Write Enable
#pragma config CP = OFF // Flash Program Memory Code Protection Disabled
void ADC_Init(void) {
ADCON0 = 0x41; // ADCON0: ADC enabled, Channel 0 selected (AN0)
ADCON1 = 0x80; // ADCON1: Right justified result, Vref+ = VDD, Vref- = VSS
}
unsigned int ADC_Read(unsigned char channel) {
ADCON0 &= 0xC5; // Clearing channel selection bits
ADCON0 |= channel << 3; // Selecting channel
__delay_ms(2); // Acquisition time
GO_nDONE = 1; // Start ADC conversion
while (GO_nDONE); // Wait for conversion to complete
return ((ADRESH << 8) + ADRESL); // Return result
}
void main(void) {
unsigned int light_level;
TRISB0 = 0; // Set RB0 as output for relay control
ADC_Init(); // Initialize ADC
while(1) {
light_level = ADC_Read(0); // Read light level from LDR (AN0)
if (light_level < LIGHT_THRESHOLD) {
RB0 = 1; // Turn on street light (activate relay)
} else {
RB0 = 0; // Turn off street light (deactivate relay)
}
__delay_ms(1000); // Delay for stability
}
}
Step 3: Interfacing the LDR
Connect the LDR in a voltage divider configuration with a fixed resistor.
The junction of the LDR and the resistor is connected to an analog input pin (e.g., AN0) of the PIC microcontroller.
Ensure the other ends of the LDR and resistor are connected to VCC and GND, respectively.
Step 4: Interfacing the Relay Module
Connect the relay module’s input to an output pin of the PIC microcontroller (e.g., RB0) through a transistor like BC547. The transistor acts as a switch to control the relay.
The relay’s NO (Normally Open) and COM (Common) terminals are connected to the street light circuit.
Step 5: Optional LCD Interface
If you want to display the light level and system status, connect a 16x2 LCD to the PORTD of the PIC microcontroller.
Initialize and update the LCD display in the code to show the current light level and whether the street light is on or off.
Step 6: Compiling and Uploading the Code
Write and compile the code in MPLAB X IDE using the XC8 compiler to generate a hex file.
Upload the hex file to the PIC microcontroller using a programmer (e.g., PICkit 3).
Step 7: Testing and Adjusting the System
Power up the system and simulate different light conditions by covering the LDR.
The street light (or connected load) should turn on when the ambient light falls below the threshold and turn off when the light level is sufficient.
Adjust the LIGHT_THRESHOLD value in the code if necessary to fine-tune the system’s sensitivity.
Applications
Street Lighting: Automate street lights to turn on at dusk and off at dawn.
Garden Lighting: Control garden lights based on natural light levels.
Security Lighting: Use the system to control security lights around homes or offices.
Conclusion
Building an Automatic Street Light Controller using a PIC microcontroller is an excellent project to understand the basics of ADC, LDR interfacing, and relay control. This project can be expanded with additional features such as time-based control, remote monitoring, or even integration with a smart home system. The skills learned here are widely applicable in the fields of home automation, energy conservation, and embedded systems design.
Want us to guide you through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects
Comentários