# Calculate Sunrise and Sunset

### Why Calculate Sunrise and Sunset in a PLC?

In the olden days, you used a photo resistor to detect when the sun has risen or set. The problem with this are obvious. You are adding an extra component that will degrade over time. Not to mention the fact that cloudy days will affect what time the sensor is triggered.

Obviously, the sun does not rise or set at the same time each day. We must use an algorithm to determine these times.

### Different Methods to Calculate Sunrise and Sunset

If you have a microprocessor that supports complex math equations, you could use a method such as this one: https://edwilliams.org/sunrise_sunset_algorithm.htm

As can be seen, the math might be a little hard to follow for most people. However, once you have it working, and the processor clock is set, simply provide an output that indicates when “Night Mode” is active.

In ControlLogix, the sample library has a very good add-on instruction that you can use in your ControlLogix project. Simply set your clock, and enter your Longitude and Latitude values.

Older processors, however, do not support complex math formulas easily. With this in mind, we must use more “rudimentary” methods of finding the sunrise and sunset times.

### SLC-500 / PLC-5 Approach

Because the older processors do not easily do complex math, my approach was to use a 12-sided figure 8. To summarize, I set up data tables containing Sunrise and Sunset values for the first day of each month. Although, we could enter values for each day of the year, I wanted this approach to be easy for maintenance due to the earth’s wobble, or if I need to modify the logic for another location. My goal here is to roughly calculate the difference between times for this month and the next month. Then figure out how far we are through the month. At this point we can calculate an estimated sunrise and sunset for the current date. By all means, this is not a perfect method, but it should get us within a few minutes each day.

### Create the Data Tables

I have set up six data tables. N50 is for Sunrise Hours, N51 for Sunrise Minutes. Then I created two more tables: N52 for Sunset Hours, and N53 for Sunset Minutes. I also created N54 as an integer workspace, and F55 as a floating point workspace. Each of these data tables had 15 elements to give me some extra room. For each data table, enter the corresponding value for each month: For example. The Sunrise Hour for January 1 will be placed into N50:0. Since I’m using indirect addressing for some of the calculations, I also enter this value into N50:13. (This is used when I calculate December (12) + 1 Month. Here is an example of the N50 data file:

It’s very important to note: I’m entering all values in STANDARD TIME only. Although that is easy to do, to keep it simple, I’m not accounting for DST in this post. Be sure to fill out all 13 values for each of the first four data tables, and don’t forget to repeat January’s value on element #13. Search for the values for your own location own location at sunrise-sunset.org.

### Write the Logic

In the first rung of logic, I’m simply moving the current month and the next month into the N54 data file as shown.

Secondly, we’ll need to know how far we are through the month. Again, this method is not perfect, but will get us pretty close. For simplicity, we’ll assume there are 30 days in a month.

Thirdly, for comparison, we need to convert the sunrise times for this month and next month to decimal.

Likewise, we will convert the sunset times to decimal.

Fourthly, we need to convert the current time to decimal for easy comparison.

Fifth, we need to know the differences between this month and next month for both Sunrise, and Sunset.

Finally, we will calculate the sunrise and sunset times for today in floating point.

At last, if we are between sunset and sunrise, set a bit to indicate that we are in night mode!

Summary

To emphasize, this method uses standard time only, and is not perfect, but should get you within a few minutes per day. All values here must be entered in Standard Time in your data tables, so be sure to account for that when you enter them from a website. If you wish to change your processor clock to Daylight Saving time, simply subtract one hour from the current time calculated, and use that value for your comparisons. Remember, all of these posts are examples only. Feel free to write comments, and suggestions or corrections.

Feel free to visit the SLC-500 category page, or the PLC-5 category page!

— Ricky Bryce