RTC time event

I have in I2C ds1307 RTC mini board which is providing the time and date to my arduino.

What is the best way to turn a pin HIGH at 8am and turn it LOW at 10pm?

Ive tried many ways but i cant find a way to keep it consistently on or off during these time periods.

Any help would be very much appreciated.


Hi VR, it would be easier to help if you showed your code and explained what problem you were having with it.

The basic logic is to check if the hours are within your desired time:

set the pin from low to high if its AM and the time is greater than or equal to 8, or if its PM and the time less than 10

at all other times set the pin low if its high.

I hope that helps


Im actually trying to fade in an LED driver with a step every 6 minutes. Its steps up fine but sometimes the LED doesnt fully turn off or doesnt stay in the correct PWM level between steps.

Here is a sample of my fade in:

  //Whites Sunrise begins at 9am and fades in every 6 minutes for an hour ( 10 steps ) until fully on 
   if ((hour ==9 )&&( minute==1  ))analogWrite (led_white_pin, 25);
   if ((hour ==9 )&&( minute==6  ))analogWrite (led_white_pin, 50);
   if ((hour ==9 )&&( minute==12 ))analogWrite (led_white_pin, 75);
   if ((hour ==9 )&&( minute==18 ))analogWrite (led_white_pin, 100);
   if ((hour ==9 )&&( minute==24 ))analogWrite (led_white_pin, 125);
   if ((hour ==9 )&&( minute==30 ))analogWrite (led_white_pin, 150);
   if ((hour ==9 )&&( minute==36 ))analogWrite (led_white_pin, 175);
   if ((hour ==9 )&&( minute==42 ))analogWrite (led_white_pin, 200);
   if ((hour ==9 )&&( minute==48 ))analogWrite (led_white_pin, 225);
   if ((hour ==9 )&&( minute==54 ))analogWrite (led_white_pin, 255); 


Is there a more simple way to say " at 9am fade in from 0 to 255 over 1 hour then stay in that final state until being faded out at 10pm?"

you could try something like this:

if (hour ==9 )
   analogWrite (led_white_pin, map(minute, 0,59,0,255) );
   analogWrite (led_white_pin, 0);


I knew there had to be a simpler way!!! Ill give a try and let you know how it goes!!!

Thanks again MEM!


Ive just been looking at the above code and here it is in my sketch:

  //Whites Sunrise begins at 9am and fades in every 6 minutes for an hour ( 10 steps ) until fully on 
   if (hour ==9 ) analogWrite (led_white_pin, map(minute, 0,59,0,255) );

  //Whites Sunset begins at 8pm and fades out every 6 minutes for an hour ( 10 steps ) until fully off
  if (hour ==20 ) analogWrite (led_white_pin, map(minute, 0,59,255,0) );
  else analogWrite (led_white_pin, 0); 

But i want the lights to fade in from 9am and then stay on full until 8pm when they then fade out. But wont the else analogWrite (led_white_pin, 0); turn off the lights inbetween these times as the program is continually run though by the processor??

You can remove the else statements if you want the light to stay on.
Also, you may want to add a check in setup to see if the light should be fully on when the sketch starts.

Ahh now i follow. So in theory if it does’nt receive any other commands it will stay in those states until another command tells it to do something else?

What do you mean about the ‘Check’ in the setup portion of the sketch?

Yes, an analog output will maintain the last value written.

If you power up or reset Arduino at a time after your fade-up (10am) and before the start of your fade-out (8pm), the light will be off when it should be on. If you check to see if you are within this period you can correctly set the light state.