19 votes

Can you set a clock using a light sensor to detect sunrise and sunset?

While pondering an off-grid microcontroller project, I got to wondering: A light sensor can obviously detect day vs night. So it could be used as a very cheap way to set a device's clock - but how accurately? To within an hour? A few minutes? How would you do it?

Questions that arose from this include:

  • Should it detect dawn/dusk (light <-> dark transition), or noon/midnight (brighest/darkest time) ?
  • How do dawn/dusk times relate to clock time? Does it depend on lat/long?
  • If using dawn/dusk, what light level threshold to use?
  • The same threshold for dawn & dusk, or different ones?
  • Better to detect a darker threshold (start of dawn, end of dusk) or a lighter one?
  • Some days will be lighter/darker than others, so how to manage averaging of times?
  • How accurate could it be made?

My naïve first stab at this would be: Pick a light threshold. Record the dawn/dusk times according to that threshold. Average them, call that "noon", and gradually tweak the clock time over several days to bring it into line with the sensed/calculated "noon" - but a searching for graphs of sunrise/sunset times quickly showed that the midpoint of sunrise & sunset is not noon.

Googling threw up lots of results for sensor lights combining a clock and a photocell, but I couldn't find anything about using the photocell to set the clock. So does anyone know if this has been tried before? Is it a non-starter for some reason?

Edit:

Perhaps it's worth sharing the project I had in mind, which is a rain alarm so I can rush out and get the washing in from the line when it starts to rain. I was thinking how annoying it would be if I left it switched on and it rained in the middle of the night and the alarm woke me up. So I decided should automatically avoid triggering during the sleeping hours of night (say 10pm to 8am). My first thought was a photocell so it wouldn't trigger when it's dark. Then I remembered that it gets light at 3am at the moment, which wouldn't work. So it needs a clock. How to set the clock:

  • Manually - Needs a user interface with buttons and a display. Seems overkill just for a clock.
  • Serial port - Clunky to plug a laptop in just to set the clock.
  • WiFi - Needs a username and password or WPS, and an ESP32 or similar - again seems overkill just to get the time.
  • GPS - also overkill and expensive.

21 comments

  1. [4]
    mat
    Link
    Sounds like a fun project! My first approach would be to set up a sensor and just log it's output for a while. Draw some charts. Then pick a threshold to consider "dawn" and "dusk". Not sure how...

    Sounds like a fun project! My first approach would be to set up a sensor and just log it's output for a while. Draw some charts. Then pick a threshold to consider "dawn" and "dusk".

    Not sure how you'd handle deciding what clock time dawn and dusk map to seeing as they're different every day of the year.. but who says you have to tell traditional time with your clock? You can get fuzzy with it - start off having it tell you if it's day or night, then morning/afternoon, etc.

    Oooh, one other thing you might try is making an electronic sundial. Put a couple of sensors around a gnomon and that should let you detect noon, on days when the sun is bright enough to cast a shadow. Noon isn't always the brightest time, but it is the time when the sun is at it's most southerly point (in the northern hemisphere). Solar noon, at least. You've got to compensate for daylight savings if you want human time.

    I'm interested in this, please do post how you get on!

    18 votes
    1. [2]
      bobstay
      Link Parent
      I think that's a great first step. Now, will I have the time and motivation to do it? I'd rather avoid the sundial idea, because it would need multiple sensors, careful physical design, and...

      My first approach would be to set up a sensor and just log it's output for a while.

      I think that's a great first step. Now, will I have the time and motivation to do it?

      Oooh, one other thing you might try is making an electronic sundial.

      I'd rather avoid the sundial idea, because it would need multiple sensors, careful physical design, and correct compass alignment - and it only works if the sun is shining (which, as I live in the UK, it mostly isn't...).

      1 vote
      1. mat
        Link Parent
        I'm in the UK and yeah. Sun actually being out is definitely an issue. Perhaps you have to have to log stuff over multiple days and use rolling averages or something. Because even the amount of...

        I'm in the UK and yeah. Sun actually being out is definitely an issue. Perhaps you have to have to log stuff over multiple days and use rolling averages or something. Because even the amount of light in the morning is super variable - dawn today was pretty dim where I am, but dawn on sunday was incredibly bright (admittedly I was in an east-facing tent a few hundred metres from the sea so the sun was coming right at me). Maybe you use rate of light change rather than absolute brightness?

        Just two sensors ought to do the trick for detecting noon, you'll be able to pick up the difference between shadow on one and on the other. If you 'cheat' by looking at your watch and set up a gnomon and a single sensor at noon, you only need one. I do think you have to detect noon somehow if you want to set a clock, because noon is the only consistent solar event which maps to human time.

        Edit: just struck me that with two sensors, a gnomon and a couple of known angles, you can calculate an hour! (maybe one sensor and a gnomon of a particular width?)

        3 votes
    2. waltteri
      Link Parent
      Goddamn you were quicker than me :D my comment looks like a copy of yours

      Goddamn you were quicker than me :D my comment looks like a copy of yours

      3 votes
  2. [2]
    ndupont
    Link
    That looks like a fun project ! Cloud cover will make the detection of dawn/dusk quite tricky. From my experience with home automation, there is quite a luminosity range due to bad weather. I...

    That looks like a fun project !
    Cloud cover will make the detection of dawn/dusk quite tricky.
    From my experience with home automation, there is quite a luminosity range due to bad weather.
    I would rather try to find the astronomical noon by detecting the maximum luminosity of the day, and maybe limit the sensor view to due south where the sun is expected. And even then, it would be probably be good to limit the measurement to an hour around the expected noon, then build a moving average day after day around that.

    6 votes
    1. bobstay
      Link Parent
      Oh, I like that idea. Try to find the brightest point of the day (and maybe discard the day's results if clouds etc cause the graph to look different from a smooth peak)

      limit the measurement to an hour around the expected noon

      Oh, I like that idea. Try to find the brightest point of the day (and maybe discard the day's results if clouds etc cause the graph to look different from a smooth peak)

      3 votes
  3. waltteri
    Link
    Interesting idea, I have no idea how much that’s being done. There are satellites and radio stations that broadcast the current time in easily available digital format, and I’d hazard a guess...

    Interesting idea, I have no idea how much that’s being done. There are satellites and radio stations that broadcast the current time in easily available digital format, and I’d hazard a guess these are the most used ways of doing this.

    But of course, if you know your lat/lon and the current date, you could easily calculate the exact time (to the minute) of the sunrise quite easily. But due to changes in weather, it might be quite hard to actually detect the exact moment of the sunrise/sunset, as you’d likely have quite a noisy graph in front of you. How would you differentiate between sunsets and clouds, for example. You’d likely need to do a lot of processing to your data, unless you’re in the middle of a desert or something.

    One idea that comes to mind would be a kind of a sundial (a solar clock, in this case e.g. a box with a hole and a gnomon, pointed at the zenith in the sky), with simple light sensors on the plane upon which the time-telling shadow is being cast. Solar noon would be an easy thing to measure with that.

    5 votes
  4. DataWraith
    Link
    That's a fun problem. I'd probably try to frame it in terms of Monte Carlo Localization. Instead of locating the 1D position of a robot in a corridor, we can locate the 1D position around a...

    That's a fun problem.

    I'd probably try to frame it in terms of Monte Carlo Localization. Instead of locating the 1D position of a robot in a corridor, we can locate the 1D position around a circular clock.

    The only thing we need is a model of the world -- something similarly simple as "brightness peaks at noon and then goes down and then back up the next day" should suffice, though the efficiency goes up with more accurate models IIUC.

    The entire thing is probabilistic, so it should work in the long run regardless of weather, but a microcrontroller may or may not have enough RAM to do particle filtering...

    5 votes
  5. [3]
    m-p-3
    Link
    What about skipping light detection entirely, and determine the sunrise and sunset times by having the microcontroller calculate those using GPS coordinates, received from a GPS receiver hooked...

    What about skipping light detection entirely, and determine the sunrise and sunset times by having the microcontroller calculate those using GPS coordinates, received from a GPS receiver hooked up.

    You could reduce power consumption by only updating the GPS location once a day or something, and make it solar powered to make it run off the grid.

    Anyway, just a thought :)

    3 votes
    1. majromax
      Link Parent
      That would defeat the purpose of the project. If you begin using a GPS sensor, you have immediate access to GPS time, After all, GPS satellites are more or less atomic clocks in orbit that...

      That would defeat the purpose of the project. If you begin using a GPS sensor, you have immediate access to GPS time, After all, GPS satellites are more or less atomic clocks in orbit that broadcast their current time to the surface.

      1 vote
    2. bobstay
      Link Parent
      I did know about GPS time, but a photocell is an order of magnitude cheaper than a GPS receiver :) I've edited to add my intended use for this system - see edit to main post.

      I did know about GPS time, but a photocell is an order of magnitude cheaper than a GPS receiver :)

      I've edited to add my intended use for this system - see edit to main post.

  6. DanBC
    Link
    Are we allowed to cheat and use a bunch of light sensors placed around a gnomon? This converts a sundial into an electronic sundial. That works for daytime in reasonably sunny weather.

    Are we allowed to cheat and use a bunch of light sensors placed around a gnomon? This converts a sundial into an electronic sundial. That works for daytime in reasonably sunny weather.

    2 votes
  7. Chobbes
    (edited )
    Link
    You're not going to believe this, but you can do it without electronics too! https://en.wikipedia.org/wiki/Sundial In all seriousness, this seems like a fun project, and I was actually wondering...

    You're not going to believe this, but you can do it without electronics too!

    https://en.wikipedia.org/wiki/Sundial

    In all seriousness, this seems like a fun project, and I was actually wondering about it the other day in a weird coincidence.

    I'd probably think more in terms of measuring when it's dark than when it's light, if that makes sense. I think it'll be easier for you to be certain that it's nighttime than day time (although clouds and the moon can have an effect on this too, so maybe it'll still be a problem). But knowing when night starts and ends should allow you to estimate solar noon, and from there you could either calculate "human noon" or decide that we've messed with time too much and should go back to a life before timezones and DST. If the earth were a perfect sphere the threshold for dawn / dusk should be the same, but in theory elevation changes can make sunset / sunrise slightly different... My guess would be that you're just better off determining when it's super dark and not worrying about compensating for elevation because in most cases the difference is probably only a few minutes. You could probably do some nice statistics to get a more accurate time, but I suspect that would be a good start.

    If you know your latitude (you could probably even determine this + the time of the year with a few samples?) you could more accurately account for solar noon, as I think the time between solar noons isn't consistent. You're going to become a bit of an astronomer if you go down this path, which seems super cool to me!

    2 votes
  8. [2]
    raleeyu
    Link
    Disclaimer: I'm not an expert. I'm just having fun. This might be true for places far away from their respective longitudinal timezone. Certain places sandwiched between extreme elevations would...

    Disclaimer: I'm not an expert. I'm just having fun.

    but a searching for graphs of sunrise/sunset times quickly showed that the midpoint of sunrise & sunset is not noon.

    This might be true for places far away from their respective longitudinal timezone.

    Certain places sandwiched between extreme elevations would also be affected by what you mentioned(sunset/sunrise midpoint not noon).

    1 vote
    1. bobstay
      Link Parent
      I was looking at this graph - taking November as an example (to avoid the DST offset), sunrise seems to be ~4.5 hours away from noon, whereas sunset is 5 hours away, giving an error of 15 minutes...

      I was looking at this graph - taking November as an example (to avoid the DST offset), sunrise seems to be ~4.5 hours away from noon, whereas sunset is 5 hours away, giving an error of 15 minutes or so. Which is not terrible, I suppose. I wonder if that's to do with the distance of the centre of the timezone from the measurement location.

      1 vote
  9. [2]
    TumblingTurquoise
    Link
    I think that this idea can work in ideal conditions. But what about weather? Clouds, fog, precipitations can all influence the perceived brightness and set some misleading time on this clock.

    I think that this idea can work in ideal conditions. But what about weather? Clouds, fog, precipitations can all influence the perceived brightness and set some misleading time on this clock.

    1 vote
    1. bobstay
      Link Parent
      I was thinking you'd have to adjust the clock by a small amount every day, which should average out the "variations in brightness due to weather" issues.

      I was thinking you'd have to adjust the clock by a small amount every day, which should average out the "variations in brightness due to weather" issues.

      1 vote
  10. [2]
    caliper
    Link
    To be able to relate dusk and dawn to actual time, you’ll need the current date and a table of dusk down times per date. So just measuring dusk and dawn isn’t enough, I think. How far off grid are...

    To be able to relate dusk and dawn to actual time, you’ll need the current date and a table of dusk down times per date. So just measuring dusk and dawn isn’t enough, I think.

    How far off grid are you thinking? You can grab gps signals to get time, or if you’re close enough to a source, radio transmitted time. That could simply be a daily “set time” event, where you receive the signal once and set the clock accordingly.

    1 vote
    1. mordae
      Link Parent
      I believe that for a couple centuries you only need date and latitude. You can calculate expected brightness using a solar system model. It wouldn't take daylight saving time into account, nor...

      I believe that for a couple centuries you only need date and latitude. You can calculate expected brightness using a solar system model.

      It wouldn't take daylight saving time into account, nor time zones. It would have it's ideal local time zone instead.

      The precision would suck and I don't dare to estimate it, but it's doable.

      But I agree that using GPS or other time source would work better for practical purposes in the upcoming decades.

  11. Stranger
    Link
    Set up a post sticking out of the ground Track the shadow of the pole and mark where it is at it's shortest Dig a deep and narrow hole following the angle from the top of the post to the shortest...
    1. Set up a post sticking out of the ground

    2. Track the shadow of the pole and mark where it is at it's shortest

    3. Dig a deep and narrow hole following the angle from the top of the post to the shortest shadow mark

    4. Put the light sensor at the bottom of the hole

    5. The sensor will now trigger at local noon. The deeper and more narrow the hole, the more accurate it is (until it becomes so precise that it is only valid for one day of the year).

    6. Measure the length of time that the sensor is active, as well as the time that it is not.

    7. Divide by 24 and/or 60

    8. ???

    9. Profit!

  12. Pavouk106
    Link
    I'd track the light for 24 hours and then by averaging get midnight and noon. I think that midnight isn't actually the middle of the night, middle would be more like 2 o'clock, so shift the...

    I'd track the light for 24 hours and then by averaging get midnight and noon. I think that midnight isn't actually the middle of the night, middle would be more like 2 o'clock, so shift the average accordingly.

    If you do this averaging over more days, you may be able to probably get accurate time to say +-15 minutes?

    I don't think you can precisely detect either dawn or dusk or midnight or noon. You would have to average that out through thw whole 24 hour period.