Build it step by step
0 of 6 doneWire the LDR
Build the divider and print readings to find sunny and cloudy values.
Choose the threshold
Set it between a sunny and a cloudy reading.
Add the clock and display
Wire the DS3231 and LCD.
Run indoors
Shine a torch on the LDR to see Today increase.
Mount outside
Fix it facing up in a clear dome on a post or roof.
Compare days
Note the totals against what you saw of the weather.
How do you build a sunshine hours recorder?
An LDR facing the sky counts how many minutes a day the sun is bright enough to cast shadows, a classic weather station measurement. You need Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, DS3231 RTC module and 16x2 LCD with I2C backpack (PCF8574). It takes 6 steps and about 75 minutes.
- Level
- Beginner
- Time
- About 75 min
- Parts
- 5
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- SensorsBuy
- DisplaysBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 5V | LDR leg 1 | Top of the divider |
| LDR leg 2 | Nano A0 and 10 kΩ resistor to GND | Light reading |
| DS3231 SDA, SCL | Nano A4, A5 | I2C |
| LCD SDA, SCL | Nano A4, A5 | I2C, same bus |
| DS3231 and LCD VCC, GND | Nano 5V, GND | Power |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "RTClib" and "LiquidCrystal I2C". Set the DS3231 once with RTClib's example. Mount the LDR facing the sky in a clear dome or under a clear cover so rain cannot reach the electronics, away from shade, and tilt it level. An LDR is not a calibrated instrument: official sunshine recorders use a threshold of about 120 W/m², so use this one to compare days with each other rather than as an exact figure. Find THRESHOLD by printing the reading on a sunny and a cloudy afternoon.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int THRESHOLD = 750; // reading above this counts as bright sun; set it from your own sunny and cloudy readings
const byte LDR = A0;
unsigned long today = 0, yesterday = 0; // seconds of bright sun
byte day = 0;
unsigned long lastSave = 0;
void setup() {
rtc.begin();
lcd.init(); lcd.backlight();
EEPROM.get(0, today); EEPROM.get(4, yesterday); day = EEPROM.read(8);
if (today > 86400UL) today = 0;
if (yesterday > 86400UL) yesterday = 0;
if (day > 31) day = rtc.now().day();
}
void loop() {
long s = 0;
for (byte i = 0; i < 10; i++) { s += analogRead(LDR); delay(20); }
if (s / 10 > THRESHOLD) today += 10; // adds ten seconds each time it is bright
DateTime n = rtc.now();
if (n.day() != day) { yesterday = today; today = 0; day = n.day(); EEPROM.put(4, yesterday); EEPROM.update(8, day); }
if (millis() - lastSave > 3600000UL) { EEPROM.put(0, today); lastSave = millis(); } // save hourly so a power cut loses little
lcd.setCursor(0, 0); lcd.print("Today "); lcd.print(today / 3600.0, 1); lcd.print(" h ");
lcd.setCursor(0, 1); lcd.print("Yest. "); lcd.print(yesterday / 3600.0, 1); lcd.print(" h ");
delay(9800);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Torch on the LDR | Today rises by 10 s every cycle |
| Shadow over it | Today stops rising |
| Midnight | Yesterday takes today's value and today resets |
| Power cut | Today resumes within an hour of its last save |
If it does not work
Always counting
Threshold is too low. Raise it above a cloudy reading.
Never counts
Threshold is too high or the LDR faces the wrong way.
Date does not roll
Set the DS3231 time.
LCD blank
Check the I2C address and contrast.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the sunshine hours recorder?
Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, DS3231 RTC module and 16x2 LCD with I2C backpack (PCF8574).
How long does the sunshine hours recorder take to build?
About 75 minutes for a beginner maker, in 6 steps.
How do I know the sunshine hours recorder works?
Torch on the LDR: Today rises by 10 s every cycle. Shadow over it: Today stops rising. Midnight: Yesterday takes today's value and today resets.
Always counting: what should I check?
Threshold is too low. Raise it above a cloudy reading.
Never counts: what should I check?
Threshold is too high or the LDR faces the wrong way.
Date does not roll: what should I check?
Set the DS3231 time.
LCD blank: what should I check?
Check the I2C address and contrast.
