RTC Based Street Light Control Using Arduino & LDR

RTC Based Street Light Control Using Arduino & LDR (Last Updated On: December 15, 2018)

RTC Based Street Light Control Using Arduino & LDR:

In this project we will learn how to design RTC Based Street Light Control Using Arduino & LDR. The concept of this project is based on low consumption of energy. In this project street lights turns on or off on the basis of day & night timing controlled by real time clock module DS3231. The timing is set via programming to determine ON and OFF time. Similarly LDR is used to detect the quantity of light and on that basis the street Light intensity is controlled.

The main advantage of street lights is that they increase safety and prevents accidents and collisions. Generally, street lights are turned on during evening time and will continue to glow till morning. This might result is unnecessary usage of power as the lights will be glowing at full intensity all the times. But using the Auto Intensity Control of Street Lights, intensity of light can be controlled on light conditions.


Components Required:




1. Arduino UNO Board
2. DS3231 RTC Module  (Buy Online from Amazon)
3. LDR
4. 16*2 LCD Display
5. Push Button
6. 10K Resistor
7. LED - 3 to 5
8. Connecting wires
9. Breadboard

Circuit Diagram & Connections:

RTC Based Street Light Control Using Arduino & LDR

First connect the SDA and SCL pins of DS3231 RTC Module to A4 (SDA) and A5 (SCL) pins of Arduino. A 10KΩ Resistor and an LDR are connected in a voltage divider format and its output is given to A0 pin of Arduino.

The data pins of 16×2 LCD Module i.e. D4 – D7 are connected to 5, 4, 3 & 2 pins of Arduino respectively. The RS and E pins are connected to pins 12 and 11.




Working of the circuit:

The project RTC Based Street Light Control Using Arduino & LDR operates in two modes i.e. RTC Mode and LDR Mode. In RTC Mode, the street lights turn on automatically based on the ON Time set in the code and turn off based on the OFF Time. In the LDR Mode, the street lights have an intensity control based on the ambient light near the LDR.

After the code is uploaded the project runs in RTC Mode. There are two times set in the code, i.e. the ON TIME and the OFF TIME.

Arduino compares the ON TIME with the time from RTC Module and when they match, the LED is turned ON. After this, the Arduino waits for the OFF TIME and once the time from RTC Module reaches the OFF TIME, the LED is turned OFF.

But during anytime of this operation, if the push button is pressed, the Arduino enters LDR Mode. In this mode, the Arduino reads the value of the LDR on the basis of quantity of light falling on LDR, then it adjusts the intensity of the LED. In order to switch back to RTC Mode, you need to press the push button again.



Source Code/Programs:

The code for RTC Based Street Light Control Using Arduino & LDR is given below. But before that upload the library for RTC. Download the library from here: DS3231 RTC Library



#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
#define ON 0
#define OFF 1
DateTime now;

RTC_DS3231 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // (rs, e, d4, d5, d6, d7)

const int buttonPin = 7;
const int led=8;
int nob = A0;
int val = 0;
int val1 = 0;
int path=1;
int a=1;
int previousState = HIGH;
unsigned int previousPress;
volatile int buttonFlag;
int buttonDebounce = 20;

int on_hour=12;
int on_minute=45;
int on_second=00;

int off_hour=12;
int off_minute=42;
int off_second=10;

int c_hour=00;
int c_minute=00;
int c_second=00;

int onOrOffFlag = ON;

void showDate(void);
void showTime(void);
void showDay(void);

void loadHandler(int, int , int , int , int , int , int , int , int );

typedef struct userTime
{
int temp_hour;
int temp_minute;
int temp_second;
}userTime_t;
unsigned char checkLessThanOrEqual(userTime_t , userTime_t);

void setup ()
{
Serial.begin(9600);
lcd.begin(16,2);

pinMode(buttonPin, INPUT_PULLUP);
pinMode(led,OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), button_ISR, CHANGE);

if (! rtc.begin())
{
Serial.println("Couldn't find RTC Module");
while (1);
}

if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

}

void loop ()
{
if(path)
{
if(a==1)
{
lcd.setCursor(0,0);
lcd.print(" RTC ");
lcd.setCursor(0,1);
lcd.print(" MODE ON ");
delay(2000);
a=0;
}
now = rtc.now();
showTime();
c_hour=now.hour();
c_minute=now.minute();
c_second=now.second();
loadHandler( on_hour, on_minute, on_second, off_hour, off_minute, off_second, c_hour, c_minute, c_second);
delay(1000);
}
else
{
if(a==0)
{
lcd.setCursor(0,0);
lcd.print(" LDR ");
lcd.setCursor(0,1);
lcd.print(" MODE ON ");
delay(2000);
a=1;
}
val = analogRead(nob);
if(val>300 && val<450)
{
lcd.setCursor(0,0);
lcd.print(" 30% ");
lcd.setCursor(0,1);
lcd.print(" Brightness ");
analogWrite(led, 400);
}
else if(val>450 && val<550)
{
lcd.setCursor(0,0);
lcd.print(" 60% ");
lcd.setCursor(0,1);
lcd.print(" Brightness ");
analogWrite(led, 600);
}
else if(val>550 && val<600)
{
lcd.setCursor(0,0);
lcd.print(" 100% ");;
lcd.setCursor(0,1);
lcd.print(" Brightness ");
analogWrite(led, 1023);
}
else if(val<300)
{
lcd.setCursor(0,0);
lcd.print(" 0% ");
lcd.setCursor(0,1);
lcd.print(" Brightness ");
analogWrite(led, 0);
}
}
}

void showTime()
{
lcd.setCursor(0,0);
lcd.print(" Time:");
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
lcd.print(" ");
}

void button_ISR()
{
buttonFlag = 1;
if((millis() - previousPress) > buttonDebounce && buttonFlag)
{
previousPress = millis();
if(digitalRead(buttonPin) == LOW && previousState == HIGH)
{
path =! path;
previousState = LOW;
}

else if(digitalRead(buttonPin) == HIGH && previousState == LOW)
{
previousState = HIGH;
}
buttonFlag = 0;
}
}

unsigned char checkLessThanOrEqual(userTime_t a, userTime_t b)
{
if(a.temp_hour < b.temp_hour)
return true;
else
{
if ((a.temp_hour == b.temp_hour) && (a.temp_minute < b.temp_minute))
{
return true;
}
else
{
if(a.temp_hour > b.temp_hour)
return false;
else
{
if((a.temp_minute == b.temp_minute) && (a.temp_second < b.temp_second))
{
return true;
}
else
{
if(a.temp_minute > b.temp_minute)
return false;
else
{
if(a.temp_second == b.temp_second)
{
return true;
}
else
{
return false;
}
}
}
}
}
}
}

void loadHandler(int onTimeHr, int onTimeMin, int onTimeSec, int offTimeHr, int offTimeMin, int offTimeSec, int rtcTimeHr, int rtcTimeMin, int rtcTimeSec)
{

userTime_t in1 = {onTimeHr, onTimeMin, onTimeSec}, in2 = {offTimeHr, offTimeMin, offTimeSec}, rtc_hr = {rtcTimeHr, rtcTimeMin, rtcTimeSec}, a = {}, b = {};

if(checkLessThanOrEqual(in1, in2))
{
onOrOffFlag = ON;
memcpy(&a, &in1, sizeof(userTime_t));
memcpy(&b, &in2, sizeof(userTime_t));
}
else
{
onOrOffFlag = OFF;
memcpy(&a, &in2, sizeof(userTime_t));
memcpy(&b, &in1, sizeof(userTime_t));

}

if((checkLessThanOrEqual(a, rtc_hr)) && (checkLessThanOrEqual(rtc_hr, b)))
{
if(onOrOffFlag == ON)
{
// Switch on the load
digitalWrite(led,HIGH);
lcd.setCursor(0,1);
lcd.print("OffTime:");
lcd.print(off_hour);
lcd.print(':');
lcd.print(off_minute);
lcd.print(':');
lcd.print(off_second);


}
else
{
// Switch off the load
digitalWrite(led,LOW);
lcd.setCursor(0,1);
lcd.print(" OnTime:");
lcd.print(on_hour);
lcd.print(':');
lcd.print(on_minute);
lcd.print(':');
lcd.print(on_second);

}
}
else
{
if(onOrOffFlag == ON)
{
// Switch off the load
digitalWrite(led,LOW);
lcd.setCursor(0,1);
lcd.print(" OnTime:");
lcd.print(on_hour);
lcd.print(':');
lcd.print(on_minute);
lcd.print(':');
lcd.print(on_second);
}
else
{
// Switch on the load
digitalWrite(led,HIGH);
lcd.setCursor(0,1);
lcd.print("OffTime:");
lcd.print(off_hour);
lcd.print(':');
lcd.print(off_minute);
lcd.print(':');
lcd.print(off_second);
}
}
}

Recommended For You

Ask A Question .......

Close