Projects / Electric diya with sunrise and sunset timer

Electric diya with sunrise and sunset timer

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANOIRLZ448.8 mA

    Wire the MOSFET and filaments

    Each with its own resistor.

  2. NANO

    Test the flicker

    Run it with the button.

  3. NANODS323120:10

    Add the RTC

    Set the time.

  4. NANO

    Enter your sunrise table

    12 months.

  5. NANO

    Check the schedule

    Set the clock near sunset.

  6. NANO

    Add the manual button

    Toggle on and off.

  7. NANO

    Place the lamp

    Stable and clear of cloth.

In short

How do you build an electric diya with sunrise and sunset timer?

Warm flicker LEDs imitate a diya and light at sunrise and sunset each day using an RTC and a sunrise table. You need Arduino Nano, DS3231 RTC module, LED filament 3 V (flexible) × 2, MOSFET IRLZ44N, Resistor 10 kΩ, 5 V 2 A USB adapter and Resistor 220 Ω × 2. It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Nano D9MOSFET gatePWM brightness (10 k from gate to GND)
MOSFET sourceGNDReturn
5 V adapter plusEach filament through its own 220 ohm resistorFilaments in parallel
Both filament endsMOSFET drainSwitched side
RTC SDA and SCLNano A4 and A5I2C
RTC VCC and GNDNano 5V and GNDPower
ButtonNano D2 to GNDManual on and off
5 V adapter minusNano GNDCommon ground

Code

A real diya is lovely but should never be left burning unattended; this one uses two warm filament LEDs and flickers by changing the brightness in small random steps, so it looks alive and is safe to leave on a shelf. The RTC keeps the time, and a table of sunrise and sunset by month (the values here are rough and for northern India; replace them with your own city's) lights it for half an hour at each. The filaments are rated for 3 V, so each has its own 220 ohm resistor from the 5 V supply; they glow gently, which is right for a lamp, and nothing gets hot. Press the button to switch it on by hand for a puja or festival, and again to return to the schedule. The RTC needs its coin cell; if the time is wrong after a power cut, check it. Keep the lamp on a stable surface away from curtains, even though it stays cool. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

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).

// Electric diya: warm flicker on two filament LEDs, lit for 30 minutes at sunrise and sunset by month.
// Library: RTClib (Adafruit), using a DS3231 module. Board: Arduino Nano.
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const byte LED_PIN = 9, BUTTON = 2;
// Approximate local sunrise and sunset by month, in minutes after midnight. These are rough values for northern India:
// replace them with the times for your own city from a sunrise table.
const int SUNRISE[12] = {430, 415, 385, 355, 335, 325, 335, 355, 370, 385, 410, 430};
const int SUNSET[12]  = {1065, 1095, 1110, 1130, 1150, 1165, 1165, 1145, 1110, 1075, 1050, 1050};
const int LIGHT_MIN = 30;
bool manual = false;
void setup() {
  pinMode(LED_PIN, OUTPUT); pinMode(BUTTON, INPUT_PULLUP);
  randomSeed(analogRead(A0));
  rtc.begin();
  if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));    // sets the time from the computer when first used
}
bool shouldBeOn() {
  DateTime n = rtc.now();
  int m = n.hour() * 60 + n.minute();
  int month = n.month() - 1;
  bool morning = m >= SUNRISE[month] && m < SUNRISE[month] + LIGHT_MIN;
  bool evening = m >= SUNSET[month] && m < SUNSET[month] + LIGHT_MIN;
  return morning || evening;
}
void loop() {
  static unsigned long lastBtn = 0;
  if (!digitalRead(BUTTON) && millis() - lastBtn > 300) { manual = !manual; lastBtn = millis(); }
  if (manual || shouldBeOn()) {
    static int level = 150;
    level = constrain(level + random(-40, 41), 60, 220);          // random walk gives a flame-like flicker
    analogWrite(LED_PIN, level);
    delay(random(30, 90));
  } else {
    analogWrite(LED_PIN, 0);
    delay(200);
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
ButtonFlame-like flicker
Clock set to sunsetLights for 30 minutes
Power off and onTime kept
Outside timesLamp stays off

If it does not work

Does not light

Check the MOSFET orientation and the common ground.

Flicker too harsh

Reduce the random step.

Wrong times

Check the RTC date and the table.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the electric diya with sunrise and sunset timer?

Arduino Nano, DS3231 RTC module, LED filament 3 V (flexible) × 2, MOSFET IRLZ44N, Resistor 10 kΩ, 5 V 2 A USB adapter and Resistor 220 Ω × 2.

How long does the electric diya with sunrise and sunset timer take to build?

About 60 minutes for a beginner maker, in 7 steps.

How do I know the electric diya with sunrise and sunset timer works?

Button: Flame-like flicker. Clock set to sunset: Lights for 30 minutes. Power off and on: Time kept.

Does not light: what should I check?

Check the MOSFET orientation and the common ground.

Flicker too harsh: what should I check?

Reduce the random step.

Wrong times: what should I check?

Check the RTC date and the table.