cant we use RTC DS-1302 and sd card together?

i m trying to save the reading of LM35 temp sensor with date and time when reading was taken. Everything is working perfect when i m seeing the readings in serial monitor. but m not able to save the data in sd card using arduino UNO. same code working perfectly when m not using RTC but m having problem when i m using RTC.here is my code. plz suggest where m going wrong.

tmp_rtc.ino (1.8 KB)

const int chipSelect;

Did you mean to give a pin number for chipSelect?

String temp = String(tempc) + "," + String( tempf) + "," +String (tm.Hour)+":"+String (tm.Minute)+":"+String (tm.Second)+","+String (tm.Day)+"/"+String (tm.Month)+"/"+String (tmYearToCalendar(tm.Year));
  Serial.println (temp);

Break that into multiple prints instead of trying to build a single string with the String class. The String class is great on a PC with gigabytes of memory. On a microcontroller, this sort of thing makes swiss cheese out of your heap and eventually crashes your program.

DS1302RTC RTC(8, 9, 10);

You may have a conflict between the pin10 rtc clock and the default chip select pin10 in sd.h.

Try move the rtc clock to another pin.

Kindly assign a pin number to your variable named "chipSelect".

m using sd+ethernet shield. so i think there's no need to assign pin no

heenakhan:
m using sd+ethernet shield. so i think there’s no need to assign pin no

And you would be thinking wrong. Because without putting a number there it is getting assigned a 0. So unless you have your chip select plugged into pin 0 it’s using the wrong pin.

i had tried to break into multiple prints. but its still not working.

heenakhan: i had tried to break into multiple prints. but its still not working.

That error isn't the one causing it not to work. Was just giving you a heads up on the next bug. The pin number is your current problem.

i changed pin no of rtc and also assign pin no to chip select. it started to save the time and date… bt it’s saving incorrect data from rtc.Then, i burn the basic code of RTC only to check the settings and there, it’s displaying the correct date time.

here is the attached pic of changed pin no and output in serial

datetime.jpg

There are several different libraries for the ds1302. Please provide a reference to the one you are using.

Please post your latest complete code with the code symbols found in the toolbar icon </>.

The code should appear in a window like this

Place your code here

I’m not you are reading the rtc in loop, and the synchronization between the time library and the rtc may not be correct. The rtc time/date may be correct, but it is not being passed into the program.

http://playground.arduino.cc/Main/DS1302RTC
here’s the link of library of RTC.

yes i using it in loop but m also controlling it through switch.

here is the full code. now the data displaying in serial monitor is correct but in sd card ,only time and date is saving correct but year,month is saving incorrectly

tmp_rtc.ino (2.3 KB)

Please read the forum posting instructions at http://forum.arduino.cc/index.php/topic,148850.0.html

If you post your code as described, you will get more people to see it and you will get more help.

#include <DS1302RTC.h>
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
// Set pins:  CE, IO,CLK
DS1302RTC RTC(8,7,6);
tmElements_t tm;
File dataFile;
const int chipSelect = 4;

const int temp_sw1= 5;
 
float sensorValue = 0;
float tempc=0;
float tempf=0;
const int analogInPin = A0;

void setup()
{

   Serial.begin(9600);
   
   pinMode(analogInPin, INPUT);
   pinMode(temp_sw1, INPUT);
  
   digitalWrite(temp_sw1, HIGH);
   RTC.read(tm);
   while (!Serial) {
    ;
    }
    Serial.print("initializing SD card...");

    if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("initialized done");
  delay(1000);
 dataFile = SD.open("temp.csv",FILE_WRITE);
  if(dataFile){
  dataFile.println("temp(C), temp(F), Time, Date");
  dataFile.close();
  }
    
}

void loop()
{
 
  if(!(digitalRead(temp_sw1)))
  {
        
    Serial.print("Pls wait.......");
    Serial.println();
        
    delay(1000);
    if(analogRead(analogInPin))
     {
      
      sensorValue = analogRead(analogInPin);
      while(digitalRead(analogInPin));
      delay(10000);
     }

      tempc = (sensorValue*5*100)/1023;
      tempf = tempc * 1.8 + 32;

       Serial.print(" tempc= ");
       Serial.println(tempc );
      Serial.print(  "tempf="  );
      Serial.println(tempf );

      
 
  Serial.print("  Time = ");
  unsigned long int    hr =tm.Hour;
  unsigned long int mint =tm.Minute;
  unsigned long int  sec =tm.Second;
    String times = String(hr)+ ":"+ String(mint)+ ":"+ String(sec);
   Serial.println(times);
   Serial.print(" Date (D/M/Y) = ");
   unsigned long int  days =tm.Day;
   unsigned long int  mnth =tm.Month;
   unsigned long int  yr =tmYearToCalendar(tm.Year);
   String dtes = String(days)+ ":"+ String(mnth)+ ":"+ String(yr);
   Serial.println(dtes);

   delay(1000);
    dataFile = SD.open("temp.csv", FILE_WRITE);
    if(dataFile){
   Serial.println("Writing..");
   dataFile.print(tempc);
   dataFile.print(",");
   dataFile.print(tempf);
   dataFile.print(",");
   delay(10000);
   dataFile.print(times);
   delay(10000);
   dataFile.print(",");
   dataFile.print(dtes);  
   delay(10000);
   
   
   dataFile.close();
   Serial.println("done");
}else
{
    Serial.println("error opening test.txt");
  }
}
 
}

yes i using it in loop but m also controlling it through switch.

. now the data displaying in serial monitor is correct

Not that I can see when I run your program. The only place I see RTC.read™; is in setup. The printed time/date does not change. The data in the time elements structure does not change by itself with the code you are using. The statement needs to be in loop as well.

sd card ,only time and date is saving correct but year,month is saving incorrectly

I don’t see this.
Try a new line to make things more clear

dataFile.println(dtes);

thank you everyone its working now..