Reliable dawn to dusk with no extra hardware?

I have three independent ‘dawn to dusk’ circuits, the newest about 15 years old. All use the basic LDR voltage divider design with Schmitt, logic, relay, etc. Triggering mains lights around the house and garden. But over three decades or so I must have replaced the LDRs dozens of times. Sooner or later they all deteriorate to the point when their resistance range falls significantly.

So I’m considering replacing all three with a single UNO circuit. Its output High or Low would go to the existing logic and relay driving circuitry.

I’d prefer to achieve this without adding an RTC or other hardware and without using specialist libraries. (Those I’ve studied briefly look dauntingly complicated.) Accuracy is of no importance. But I would like to be able to easily adjust its daily On and Off times, seasonally for instance. At least by removing it from its eventual dedicated 328 socket to recompile the sketch on the PC. Ideally it would be great do so in-situ, say by using a couple of buttons to add or subtract an hour from On or Off times, but maybe that would be pushing it!

A quicker solution for me, as an experienced hobbyist, would almost certainly be to stay with legacy electronics and redesign a single circuit using a much more reliable sensor. Perhaps a photodiode? Or even buy a small digital timer and design an add-on to rectify the audio. But I’m on the low slopes of the Arduino learning curve and this is an opportunity to advance a bit.

All advice and practical recommendations would be much appreciated please. And particularly any that point me to existing code to get me started. I have written a few simple millis()-based sketches but still regard myself as a novice.


I’m guessing that what you want to do is replace the LDRs with a system based on time. I reckon an RTC is essential - Arduinos do not keep to clock time over periods of 24 hours or more.

If you are taking on the challenge of programming an Arduino then I don’t reckon that an RTC would make the programming more complicated.

Using buttons to adjust the time should be straightforward.

I think it would help your planning for the project if you take the trouble to work through some of the online Arduino tutorials so you have a good grasp of the basics. That obviously takes time and may push you towards your alternative of finding more long-lasting sensors for your existing system - especially if you would have no other use for Arduino skills.


Is this an project that will be using a MCU or not, the OP's description is vague on that matter.

I'd have a photodiode used to measure the ambient light and compare the ambient light detected to a setting of a potentiometer. I'd have a set point to trigger that I can change. Now, what time it is or not, with a ambient light measure, is irrelevant. Energizing a few relays to turn on other lights. No MCU required, no RTC required.

I interpreted the OP’s inquiry as already having “dumber” circuitry and looking to improve it, however.

There are two distinct problems. If the lights are to be coming on and off at specific times, or specific times relative to sunrise and sunset, an RTC would work nicely, of course.

If you do use an RTC, do so fearlessly, they are quite easy to handle and inexpensive to boot.

But I’ve always wondered about a program or circuit that would have a strong circadian rhythm machine that it would smartly use the long periods of darkness and light to (more or less) synchronize a rough clock idea of where we are in the day.

So lighting at dusk would be based on the last several days of when sun set probably was.

I never tried anything, for me the RTC was the solution if less fun.


Thanks for those fast replies.

I’ll buy an RTC as Robin2 recommended. At least that will make it easier to understand the RTC sketches I’ve been browsing and at best it will get me one reliable solution.

Interesting idea, alto777. I had actually been wondering how difficult it would be for the 328 UNO to store a set of say fortnightly UK ‘lighting up’ times as the basis for my ‘no hardware’ sketch.

Not clear why you think I was vague Idahowalker? I did say I was using a UNO and I did say it would be on a dedicated 328.

The easiest way I know is running on an ESP8266.
It needs no hardware and no RTC, but a network connection.
It is not impacted by storms or other transient events.
The dawn/dusk times (and a LOT more) come from

I have a project that tracks the sun. I calculate the sunrise from a collection of several days of sunrises, do a bit of math to calculate when the sunlight will be strong enough for the solar cells, un-park the solar array, aim solar array, track the sun, measuring the ambient light along the way. When ambient light is too low, park solar cells, turn off motors, and wait for ambient light to start the process all over..

RIN67630: Thanks, I’m not yet into internet stuff yet but that’s more motivation to get there.

Meanwhile my RTC Is due shortly from UK Amazon Prime so that will keep me busy for a while.

Idahowalker: Sounds fascinating. I wish the English weather would allow me to confidently play with that approach here!

Here's one of the sketches I was playing with a few days ago. Its operation is obscure to me but it looked sort of promising, without extra hardware.

If in Setup() I serial printed something like
"Before ending Setup use Serial Monitor to enter the current time, plus a few seconds."
could additional code then keep track of time accurately enough for my dawn to dusk program? If so, for how long could I expect times to be correct within say 10 minutes?

 * HardwareCounting sketch
 * uses pin 5 on 168/328

// Sunday 29 November 2020, 18:52, shed
// Not discovered yet what TCCR1A and TCCR1B are.

const int hardwareCounterPin = 10;   // input pin fixed to internal Timer; was originally 5
const int ledPin             = 13;

const int samplePeriod = 1000;   // the sample period in milliseconds
unsigned int count;

void setup()
  Serial.println("HardwareCounter"); // My addition
  pinMode(ledPin, OUTPUT);
  // hardware counter setup (see ATmega data sheet for details)
  TCCR1A = 0;      // reset timer/counter control register A

// If I serial printed something like
// "Before ending Setup use Serial Monitor to enter the current time, plus a few seconds."
// Could some additional code then keep track of time accurately enough for my dawn to dusk program?
// If so, for how long could I expect times to be correct within say 10 minutes?

void loop()
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin, HIGH);
  // start the counting
  bitSet(TCCR1B , CS12); // Counter Clock source is external pin
  bitSet(TCCR1B , CS11); // Clock on rising edge
  // stop the counting
  TCCR1B = 0;
  count = TCNT1;

  TCNT1 = 0;  // reset the hardware counter
  if (count > 0)

Time for some research, as a starting point try searching on "rate of change for length of days" and go from there.

Sooner or later they all deteriorate to the point when their resistance range falls significantly.

I didn't know that. Is that from direct exposure to sunlight? It's probably just worth keeping extra LDRs on hand or switching to a phototransistor/photodiode. I'm pretty sure photodiodes & phototransistors are stable & reliable.

Ironically, I'm considering changing to a photosensor from a timer... I've got an older X-10 home automation system and the timer/controller is pretty smart. You tell it approximately where you are geographically and it knows when sunrise & sunset is throughout the year. *

The only thing I built myself for the home automation system is a "sunrise simulator" fade-up dimmer (and little time-delay beeper to go along with it). It's plugged-into a controlled-outlet so the X-10 master controller takes care of the time-of-day. (The X-10 by itself can't automatically fade-up... i.e. If you program it to 10% brightness it jumps to 100% before falling-back to the target brightness.)

But the X10 system is having some communication issues and I don't think they make that kind of controller anymore so I'm upgrading to Z-Wave (I have some stuff on order) and I'm going to try a light sensor. They have a multi-sensor that senses light, motion, temperature, and a couple other things. I think the Z-wave controller I ordered also knows about sunrise/sunset times if the light sensor doesn't work-out for some reason.

  • You can do that too with an RTC, since the RTC knows the date... You just have to make an adjustment table A once-a-week adjustment is probably good enough, but you could store daily values. The rate-of-change is fastest around the 1st day of spring and the 1st day of fall so that time of year you can often notice the change from one week to the next.

… for how long could I expect times to be correct within say 10 minutes?



Thanks DVDdoug.

My LDRs are all in shaded areas, out of direct sunlight and rain, so I assume the degradation is mainly due to atmospheric exposure. I also expect that industrial circuits use higher quality components.

Might change to a photodiode circuit out of curiosity as well, but from feedback here it sounds like the RTC is the approach to focus on.

alto777: Thanks for that helpful link.