Rtc wont print date or time on serial or on data logger file

the code bellow works but prints zeros instead of date and time. It reads the sensor and prints it well enough. Sensor is an ldr using the ds1307. rtc pins are data = 7, clk = 6, rst = 8, gnd = gnd and the ldr, vcc = 3.3v ( i tried 5v same deal), the example will show a year and weekday, rest zeros.

// mosi = 11
//miso = 12
//clk = 13
//cs = 10
//rtc data pin 7
//rtc clk pin 6
#include <SD.h>
#include <SPI.h>
#include <RTCDS1307.h>
RTCDS1307 rtc(0x68);
uint8_t hour, minute, second, day, weekday, month, year;
String m[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String w[7] = {"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"};
File myFile;
const int chipSelect = 10;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
rtc.begin();
rtc.setDate(7, 31, 24);
rtc.setTime(8,30, 00);
while(!Serial) {
  ;
}
Serial.print("SD Ready");
if(!SD.begin(chipSelect)) {
  Serial.println("SD Card Failed");
  return;
}
Serial.println("SD Good");
}

void loop() {
  // put your main code here, to run repeatedly:
myFile = SD.open("lightS", FILE_WRITE);
 rtc.getDate(year, month, day, weekday);
  rtc.getTime(hour, minute, second);
  if (!(second % 3)) rtc.setMode(1 - rtc.getMode());
  rtc.getTime(hour, minute, second);

 
 myFile.print(hour);
  myFile.print('-');
    myFile.print(minute);
    myFile.print('-');
    myFile.print(second);
    myFile.print(' ');
    myFile.print(day);
    myFile.print('-');
    myFile.print(weekday);
    myFile.print(':');
    myFile.print(month);
    myFile.print(':');
    myFile.print(year);

myFile.write("    ");
int A0 = analogRead(A0);
myFile.print("LDR =");
myFile.print(A0);
myFile.write ("\n"); //new line
myFile.close();

delay(1000);
}

show schematic

Sorry I had to do it like this, circuit designer didn't have the components I'm using,

This is what your code printed

is ds1307 not I2C device?

Not sure, this is what I have says ds1302
Here's the data logger also

Your sketch says DS1307 but your schematic says DS1302. Which one is it?

Also, the RTCDS1307 library uses the standard Wire object, meaning SCL/A5 and SDA/A4. You can't just hook it up to other arbitrary pins and expect it to work.

seems it is not really I2C, but similar.

Ahh, so not a DS1307. I'd be willing to bet money that the RTCDS1307 library won't work with a DS1302. And I'm not a betting person.

Checkout here for ds1302.........
https://www.youtube.com/watch?v=MfmK55TREuQ

yup, i didnt even get Serial.print to work when the rtc was on the i2c pins

ya i guess i couldve huh, i thought i knew how to work the rtc

try the ArduinoRTClibrary
running
File>Examples>ArduinoRTClibrary-master>virtuabotixRTC_version1
on a UNO connected so

// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(6, 7, 8);   // SCLK IO (DAT) C_E (RESET);  

void setup()  {      
  Serial.begin(115200);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
}

the serial monitor displays

Current Date / Time: 10/1/2014  23:59:0
Current Date / Time: 10/1/2014  23:59:5
Current Date / Time: 10/1/2014  23:59:10
Current Date / Time: 10/1/2014  23:59:15
Current Date / Time: 10/1/2014  23:59:20
Current Date / Time: 10/1/2014  23:59:25
Current Date / Time: 10/1/2014  23:59:30

I'll try it tonight, thanks

working code, library from post 13 worked

// mosi = 11
//miso = 12
//clk = 13
//cs = 10
//rtc data pin 7
//rtc clk pin 6
// rst =8
//rtc 
#include <SD.h>
#include <SPI.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC (6, 7, 8);
File myFile;
const int chipSelect = 10;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
      //sec, min, day of week, day of month, month, year
myRTC.setDS1302Time(00, 35, 23, 4, 1, 8, 2024);


while(!Serial) {
  ;
}
Serial.print("SD Ready");
if(!SD.begin(chipSelect)) {
  Serial.println("SD Card Failed");
  return;
}
Serial.println("SD Good");
}

void loop() {
  // put your main code here, to run repeatedly:
myFile = SD.open("lightS", FILE_WRITE);

  myRTC.updateTime();
 
  myFile.print("Current Date / Time: ");                                                                 //| 
  myFile.print(myRTC.dayofmonth);                                                                        //| 
  myFile.print("/");                                                                                     //| 
  myFile.print(myRTC.month);                                                                             //| 
  myFile.print("/");                                                                                     //| 
  myFile.print(myRTC.year);                                                                              //| 
  myFile.print("  ");                                                                                    //| 
  myFile.print(myRTC.hours);                                                                             //| 
  myFile.print(":");                                                                                     //| 
  myFile.print(myRTC.minutes);                                                                           //| 
  myFile.print(":");                                                                                     //| 
  myFile.println(myRTC.seconds);                                                                         //| 
                                 
    
myFile.write("    ");
int A0 = analogRead(A0);
myFile.print("LDR =");
myFile.print(A0);
myFile.write ("\n"); //new line
myFile.close();
Serial.println(A0);
delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.