Arduino light sensing timer circuit

Thread Starter

Chris Danckert

Joined Mar 10, 2010
2
I am attempting to build a after dark lighting system. I am using a arduino and a LDR to control the lights via a relay. I am seeking code that would allow the lights to remain on for 5 hours and then turn off after the circuit sensors the lack of daylight.
 

woozycactus

Joined Jan 4, 2021
104
I am attempting to build a after dark lighting system. I am using a arduino and a LDR to control the lights via a relay. I am seeking code that would allow the lights to remain on for 5 hours and then turn off after the circuit sensors the lack of daylight.
I am not a pro with arduino but im pretty sure your going to need a real time clock device to make this work which elego sells.
 

Reloadron

Joined Jan 15, 2015
7,480
I am attempting to build a after dark lighting system. I am using a arduino and a LDR to control the lights via a relay. I am seeking code that would allow the lights to remain on for 5 hours and then turn off after the circuit sensors the lack of daylight.
Yes, you can do it with an Arduino. Click here's sample code would likely work and as djsfantasi points out you will not get split second accuracy but close enough for most basic applications. That said Westinghouse markets a bunch of timers popular for exterior lighting that will do exactly what you want done. Years ago, about 20 years ago I bought a few of these timers for outside use. One mode was on at dark and you simply set how many hours after On you wanted Off. Mine were Westinghouse Model 28011 and looking at some newer ones similar on Amazon but they seem to only offer 2, 4, and 6 hour delays till Off which does not get you the 5 hours you want. They call them "stake timers" only because you use a stake to shove them into the ground. Here is a real basic flavor which does exactly what you want and includes a 5 hour delay for off after on at dusk. Now they seem to cost about $35 USD and I am looking at 120 VAC 60 Hz flavors but sure they can be had in other voltages and switching load currents. Here is another for about $23 USD with two grounded outlets.

Again, an Arduino will do just fine and I am merely pointing out another option where you are not tying up an Arduino in a permanent application. Using an Arduino will work just fine and afford you some experience if that is what you are after.

Ron
 

woozycactus

Joined Jan 4, 2021
104
Unless you need EXACTLY five hours, while the Arduino’s clock is not as accurate as a RTC, you will NOT need a Real Time Clock.
The reason I think you should use a RTC, correct me if im wrong, but if you loose power after the 4.5 hour time starts, wouldn't it reset for another 5 hours?
 

click_here

Joined Sep 22, 2020
548
I don't know about where you are from, but where I live power outages are not too common - I'd just accept that sometimes it turns on for a second time if the power goes off and on...
 

djsfantasi

Joined Apr 11, 2010
9,155
I don't know about where you are from, but where I live power outages are not too common - I'd just accept that sometimes it turns on for a second time if the power goes off and on...
No worries. This will work for you. Just google “Arduino EEPROM writes” for advice.
 

click_here

Joined Sep 22, 2020
548
Look at it this way:

If the light comes on at 6:00pm, and power goes off at 8:00pm, and then comes back on at 10:00pm, do you really want it to go back on for another 3 hours?

If you are going to use EEPROM, only update it once an hour. There is only so many times you can write to a EEPROM memory

Another way would be to look at the BORF flag in the MCUSR register - If brown out has been detected at the start of the program, don't continue until the user has cleared the error via a push button, or 12h has passed, or ...
 

djsfantasi

Joined Apr 11, 2010
9,155
Look at it this way:

If the light comes on at 6:00pm, and power goes off at 8:00pm, and then comes back on at 10:00pm, do you really want it to go back on for another 3 hours?

If you are going to use EEPROM, only update it once an hour. There is only so many times you can write to a EEPROM memory

Another way would be to look at the BORF flag in the MCUSR register - If brown out has been detected at the start of the program, don't continue until the user has cleared the error via a push button, or 12h has passed, or ...
Wrong approach.

When the light sensor triggers, calculate the end time and write it to the EEPROM.

When power fails, read the end time from the EEPROM.

Always test for the end time in order to determine when to shut off the light.

This way, you only write to EEPROM once a day.
 

click_here

Joined Sep 22, 2020
548
Wrong approach.

When the light sensor triggers, calculate the end time and write it to the EEPROM.

When power fails, read the end time from the EEPROM.

Always test for the end time in order to determine when to shut off the light.

This way, you only write to EEPROM once a day.
Sure, if you want a RTC and brown out detect circuit that would work nicely

Another approach would be to have battery back up for the control circuit which is a lower voltage than the supply voltage and isolated via a diode
 
Top