rtc module ds3231 controlled led

hello there i m new to arduino programming i want my arduino nano to power an led with rtc module ds3231

for example morning 8am the led must onn and night 8pm it should off

is there any simple code for newbie like me to make this operation come alive

currently i had set time to the rtcmodule and also checked that after power loss the time is still retianing

is there any simple code for newbie like me to make this operation come alive

Yes.

currently i had set time to the rtcmodule and also checked that after power loss the time is still retianing

So, you have SOME code that reads time from the RTC and does something with it. That is a VERY good starting point. A couple of if statements, and two calls to digitalWrite() and your done.

As Paul suggests, start with the code that you have that verifies that the time is retained by the RTC after a power loss. Write a few extra lines of code to test whether the time is 0800 or 2000 and print a message if it is. Now add code to turn the LED on or off instead of, or as well as, printing a message.

Give it a try and post the code here. If it does not do what you want then describe what it does do. If it prints the messages but does nothing with the LED then please post a circuit diagram even if it is a photograph of a hand drawn circuit.

really dont know how to do it ????

i was just able to set time to my rtc module and could see that the time is set correctly using com monitor ...thats all i could do uptill now

i m wondering how i make nano to read rtc time and tell the led to get onn ????

PaulS:
Yes.
So, you have SOME code that reads time from the RTC and does something with it. That is a VERY good starting point. A couple of if statements, and two calls to digitalWrite() and your done.

may i please have the code structure as i have zero knowledge about programming

between the code i used to set rtc module is as below

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

//#ifndef ESP8266
//  while (!Serial); // for Leonardo/Micro/Zero
//#endif

  Serial.begin(9600);

  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
        rtc.adjust(DateTime(2016, 12, 20, 19, 06, 00));
  }
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
   /* Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println(); */
    delay(3000);
 
}

UKHeliBob: As Paul suggests, start with the code that you have that verifies that the time is retained by the RTC after a power loss. Write a few extra lines of code to test whether the time is 0800 or 2000 and print a message if it is. Now add code to turn the LED on or off instead of, or as well as, printing a message.

Give it a try and post the code here. If it does not do what you want then describe what it does do. If it prints the messages but does nothing with the LED then please post a circuit diagram even if it is a photograph of a hand drawn circuit.

how to post photo ??? sorry i m new to this forum

between the task is just to make digital pin 7 high at 8am and make it get low at 8pm

may i please have the code structure as i have zero knowledge about programming

And, clearly no intention of learning, so, no.

Writing an if statement is trivial. Calling digitalWrite() is trivial.

PaulS:
And, clearly no intention of learning, so, no.

Writing an if statement is trivial. Calling digitalWrite() is trivial.

I M ABLE TO CONTROL LED WITH ARDUINO CODE BUT I REALLY DONT HAVE ANY LOGIC ON HOW I MAKE RTC MODULE DS3231 TO DO THE TASK TO MAKE IT ONN AT 8AM AND OFF AT 8PM ???

#define LED1  7                        
void setup()

{    


Serial.begin(9600);
  pinMode(LED1, OUTPUT);       

}

  void loop()

{

   digitalWrite(LED1,0);           // Turns ON LED 1
   Serial.println("Light ON");
   delay(2000);                                      // Wait 2 seconds

   digitalWrite(LED1,1);          // Turns OFF LED 1
   Serial.println("Light OFF");
   delay(2000);
   
}

In the program that you posted you have this line of code     Serial.print(now.hour(), DEC);It appears to print the current hour.

Do you know how to test whether a variable is equal to a value ? You need to compare the current hour with the target hour(s) and if (big hint !) they match then turn the LED on or off. In English that would be "if the hour is 8 then turn on the LED". You say that you already know how to turn the LED on and off so you are already close to a solution.

if (big hint !) you do some research if (big hint !) you apply what you find if (big hint !) you incorporate the logic into the program then you will be able to change the state of the LED

mrugen1991: I M ABLE TO CONTROL LED WITH ARDUINO CODE BUT I REALLY DONT HAVE ANY LOGIC ON HOW I MAKE RTC MODULE DS3231 TO DO THE TASK TO MAKE IT ONN AT 8AM AND OFF AT 8PM ???

First step should be: MAKE YOUR HARDWARE WORK!

TEST IT!

Have you connected the DS3231 to your Arduino board?

RTC-VCC connected to Arduino-5V RTC-GND connected to Arduino-GND RTC-SDA connected to Arduino-SDA RTC-SCL connected to Arduino SCL

Ready?

Then upload the "I2C scanner" sketch to your Arduino board, open the serial monitor and watch!

"Does the I2C scanner" find your RTC at its address 0x68 on the I2C bus?

YOU CANNOT DO ANYTHING WITH YOUR RTC MODULE UNLESS IT IS CONNECTED TO THE ARDUINO BOARD AND THE HARDWARE IS WORKING!

jurs: First step should be: MAKE YOUR HARDWARE WORK!

TEST IT!

Have you connected the DS3231 to your Arduino board?

RTC-VCC connected to Arduino-5V RTC-GND connected to Arduino-GND RTC-SDA connected to Arduino-SDA RTC-SCL connected to Arduino SCL

Ready?

Then upload the "I2C scanner" sketch to your Arduino board, open the serial monitor and watch!

"Does the I2C scanner" find your RTC at its address 0x68 on the I2C bus?

YOU CANNOT DO ANYTHING WITH YOUR RTC MODULE UNLESS IT IS CONNECTED TO THE ARDUINO BOARD AND THE HARDWARE IS WORKING!

yes my arduino is connected with rtc module properly since the time as set to it with rtc code is able to see in com monitor

my arduino is connected with rtc module properly since the time as set to it with rtc code is able to see in com monitor

Earlier in this thread you said that you knew that the RTC was reating the time through being powered down. How did you establish that ?

UKHeliBob: Earlier in this thread you said that you knew that the RTC was reating the time through being powered down. How did you establish that ?

i simply removed the power suppply to my arduino nano and after few hrs i hoocked up back to laptop and checked using arduino ide com monitor... time show was the correct real time.

    DateTime now = rtc.now();
    if(now.hour() == 8)
    {
       // turn the led on
    }

    // Determining if it is time to turn the led off is up to you

PaulS: ```     DateTime now = rtc.now();     if(now.hour() == 8)     {       // turn the led on     }

    // Determining if it is time to turn the led off is up to you

i add this piece of code with the led code ???

or do i add this code with rtc code which i used to set and use my logic of led in those curly braces

i add this piece of code with the led code

yes.

PaulS:
yes.

I DID THIS BUT NO LED ONN
NO OUTPUT IN COM MONITOR TOOO

if(now.hour() == 24) I DID 24 BCOZ ITS MIDNIGHT 12 HERE

 #define LED1  7   
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;
DateTime now = rtc.now();
   
void setup()

{    


Serial.begin(9600);
  pinMode(LED1, OUTPUT);       

}

  void loop()
  
 
{
  
 DateTime now = rtc.now();
    if(now.hour() == 24)

   digitalWrite(LED1,0);           // Turns ON LED 1
   Serial.println("Light ON");
   delay(2000);                                      // Wait 2 seconds

   digitalWrite(LED1,1);          // Turns OFF LED 1
   Serial.println("Light OFF");
   delay(2000);
   
}

if(now.hour() == 24) I DID 24 BCOZ ITS MIDNIGHT 12 HERE

I've NEVER seen an RTC return 24 for the hour. 0, yes. 1 to 22, yes. 23, yes. 24, no.

You need to configure your RTC to return time in 24 hour format.

Why are you SHOUTING at the people you are asking for help.

Have you used Google to research what others have done with the DS3231?

You really should understand basic programming before starting out on your own projects. https://www.arduino.cc/en/Reference/HomePage

.

RTC clocks in 24-hour mode run from 00:00:00 (midnight to 23:59:59 (one second before midnight.
Hour is always in the range 0…23, there is no hour 24. Nowhere on this world.

Correct switching logic in 24 hour mode from 8:00am(ON) to 8:00pm(20:00 OFF) would be:

bool state=((now.hour() >= 8 && now.hour<20)

digitalWrite(LED1,state);

This will turn on and off your LED, no matter when you power-on the device.