Here comes the Sun
How My Chicken Coop Controller Calculates Sunrise and Sunset
After I published “A Better Automatic Chicken Coop Door,” it occurred to me that I could do a better job explaining the math behind how sunrise and sunset can be calculated from a location and date.
Remember, the coop controller runs completely offline. There’s no network connection, no cloud service, and no outside API calls. The firmware computes the solar events locally using standard NOAA-style astronomical equations.
Below is the simplified sequence of calculations used by the firmware, explained in plain English with the actual equations shown and every number plugged in so someone with high school math (trigonometry and a calculator) can follow along.
Example Setup and Constants
For this example, we’ll use the following:
Location: Little Rock, Arkansas (USA)
Latitude: 34.75° N
Longitude: 92.29° W
Date: March 6, 2026
To compute sunrise and sunset, the firmware only needs three key pieces of information:
Lat = Latitude (how far north you are) = 34.75°
SolarDec = solar declination (how far north or south the sun is that day)
HA = hour angle (how many degrees Earth must spin before sun reaches your horizon)
1. Determine the Day of the Year
Everything starts with one simple number: what day of the year is it?
For March 6, 2026:
DoY = 65(January has 31 days + February has 28 days in 2026 + 6 days = 65.)
2. Estimate the Sun’s Position (Solar Declination)
The controller calculates how far north or south the sun is using this formula:
SolarDec ≈ −23.44° × sin((360° / 365.25) × (DoY − 81))Plugging in DoY = 65:
SolarDec ≈ −23.44 × sin((360 / 365.25) × (65 − 81))
SolarDec ≈ −23.44 × sin(0.9856 × (−16))
SolarDec ≈ −23.44 × sin(−15.77°)sin(−15.77°) ≈ −0.272
SolarDec ≈ −23.44 × (−0.272) ≈ +6.37°Rounded: SolarDec ≈ +6.4°
This means the sun is slightly north of the equator (spring is coming and days are getting longer).
3. Calculate the Hour Angle
This is the most important step. The controller uses this equation:
cos(HA) = [sin(h0) − sin(Lat) × sin(SolarDec)] / [cos(Lat) × cos(SolarDec)]Where:
h0 = −0.833° (small correction for atmospheric refraction and the apparent size of the sun)
Lat = 34.75°
SolarDec = +6.37°
Plugging in the values (using a calculator for sin and cos):
Numerator:
sin(−0.833°) − sin(Lat) × sin(SolarDec)
≈ −0.0145 − (0.570 × 0.111)
≈ −0.0145 − 0.0633
≈ −0.0778
Denominator:
cos(Lat) × cos(SolarDec)
≈ 0.821 × 0.994
≈ 0.816
cos(HA) ≈ −0.0778 / 0.816 ≈ −0.095Now take the inverse cosine:
HA = arccos(−0.095) ≈ 95.5°4. Convert Hour Angle to Time
Earth spins 15° every hour, so we convert the angle to hours:
Hours from noon = HA / 15 ≈ 95.5 / 15 ≈ 6.37 hoursSunrise ≈ 12:00 − 6.37 hours ≈ 5:38 AM
Sunset ≈ 12:00 + 6.37 hours ≈ 6:22 PM5. Apply Astronomical Corrections
The quick estimate is close, but the controller now runs the full NOAA equations to fix two real-world effects.
a. Equation of Time (how much the sun appears ‘fast’ or ‘slow’ because Earth’s orbit is not perfectly circular)
The controller calculates a fractional year angle γ:
γ = (2 × π / 365) × (DoY − 1)For DoY = 65:
γ ≈ (6.2832 / 365) × 64 ≈ 1.101 radiansIt then plugs γ into the full NOAA formula (a combination of sin and cos terms). For March 6, 2026 this gives:
eqtime ≈ −11.23 minutes(The sun is running about 11 minutes “slow” today.)
b. Longitude correction (because Little Rock is not exactly on the center of the Central Time zone)
Little Rock longitude = 92.29° West
Central Time zone meridian = 90° West
Correction = 4 minutes per degree × (92.29 − 90) = +9.16 minutes
The controller now uses the refined hour angle HA together with these corrections in the main NOAA sunrise formula (in minutes past midnight, Universal Time):
sunrise_UT (minutes) = 720 − 4 × (longitude + HA) − eqtimePlugging in the values (longitude = −92.29°, HA = 95.5°, eqtime = −11.23):
4 × (−92.29 + 95.5) = 4 × 3.21 = 12.84
sunrise_UT = 720 − 12.84 − (−11.23) = 720 − 12.84 + 11.23 = 718.39 minutes718.39 minutes ≈ 11:58 UT
The calculation above uses a simplified hour angle so it’s easier to see how the longitude correction and Equation of Time affect the result.
In the actual firmware (solar.cpp), the controller runs the full NOAA solar algorithm. This produces slightly more accurate values for both solar declination and the hour angle. Because of that, the final Universal Time ends up a few minutes different from our simplified example, yielding the precise sunrise and sunset times the coop controller actually uses.
The same calculation is performed for sunset as well, with the hour angle taken on the opposite side of solar noon.
6. Convert Universal Time to Local Time
Finally, the controller simply subtracts the Central Standard Time offset:
Local time = UT − 6 hoursSo the precise Universal Time sunrise (after all refinements) converts to the local clock time shown below.
Civil dawn and civil dusk are calculated using the same NOAA solar algorithm used for sunrise and sunset. The only difference is the zenith angle used in the hour-angle calculation: official sunrise and sunset use 90.833°, which represents the sun at the horizon with refraction and solar-disk correction, while civil dawn and dusk use 96.0°, corresponding to the sun being 6° below the horizon.
Everything else in the calculation remains the same, solar declination, equation of time, and longitude correction. So the result is simply an earlier morning event and a later evening event.
Final Results for March 6, 2026 (Little Rock)
After running all the math, here are the accurate times the controller uses:
Civil Dawn : 06:07 (6:07 AM)
Sunrise : 06:32 (6:32 AM)
Sunset : 18:09 (6:09 PM)
Civil Dusk : 18:34 (6:34 PM)These match published solar tables within about one minute.
So that’s the math behind the coop door. In the original article I glossed over most of this, and afterward it felt like I’d skipped the best part. In reality it only takes a little trigonometry, a date, and the coop’s latitude to predict when the sun will appear over the horizon.
In the end, the chickens don’t know anything about solar declination or hour angles. They’re basically dinosaur pigs. They eat everything, including each other (if they could), and they’re perfectly happy as long as the door opens right on schedule every morning.
If you care about how things are made and why they’re built the way they are, you’ll probably like the rest of what I write about. Sometimes that means solar math and sometimes it’s just me pontificating about how I screwed up a design.
Hitting like and sharing helps real people find the work. The algorithm can go pound sand.



