RTC Based Automatic Street Light Using Arduino & LDR
In this project, we will learn how to design RTC Based Automatic Street Light Using Arduino & LDR. The concept of this project is based on a low consumption of energy. In this project, street lights turn 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 LDR Arduino works well here.
The main advantage of street lights is that they increase safety and prevent accidents and collisions. Generally, street lights are turned on during evening time and will continue to glow till morning. This might result in unnecessary usage of power as the lights will be glowing at full intensity all the time. But using the Auto Intensity Control of Street Lights, the intensity of light can be controlled on light conditions.
Components Required:
1. Arduino UNO Board
2. DS3231 RTC Module
3. LDR
4. 16*2 LCD Display
5. Push Button
6. 10K Resistor
7. LED – 3 to 5
8. Connecting wires
9. Breadboard
Circuit: RTC Based Automatic Street Light Using Arduino & LDR
First, connect the SDA and SCL pins of the 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 the 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 Automatic Street Light 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 any time 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 the 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 Automatic Street Light Using Arduino & LDR is given below. But before that upload the library for RTC. Download the library from here: DS3231 RTC Library
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
#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); } } } |














7 Comments
That’s really useful. It helped me to choose a suitable theme for my final year project.
But how can I control time just for one lamp that switches on in the darkness? Should I use real time clock or is there any simple way of doing this?
Use rtc. Or there is another way by using millis function in code.
i am getting error on line no 8 . error is RTC_DS3231 does not name a type
Does it possible that light should be on for 12-16 hrs by automaticaly sensing sun hrs and if sun set remaining hrs used from artifical light source.
Hi
Nice Project.
how would i fit an I2C LCD2004 screen on it?
Hi
Good Project.
How can i use it on an I2C 2004LCD screen?
Good project.
Is this code working. Please tell me.