RTC vs SD Card

Hi guys,

I'm working on a datalogger with Arduino and everything was working fine until I resolve to write a timestamp in each line on my data file. After this the SD stopped to work, but if I try to run the examples code of each libraries separated , SD or RTC, it works fine.

The problem is when I try to run SD and RTC libraries together.

I tried others SD libraries instead of SD.h, now I'm using SdFat.h and other RTC libraries but the same problem.

This is the examples codes of SdFat.h and DS1307.h libraries merged.

If I comment out the line "myFile.print(RTC.get(DS1307_YR,false), DEC);" the SD card stops to work.

Any ideas?

Thanks

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson
#include <SdFat.h>
SdFat sd;
SdFile myFile;
const int chipSelect = 4;

void setup() {  
  
  pinMode(3, OUTPUT);
  
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  // re-open the file for reading:
  // read from the file until there's nothing else in it:
  int data;
 
}

void loop() {
  
  digitalWrite(3, HIGH);
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  myFile.println("testing 1, 2, 3.");
      
//  myFile.print(RTC.get(DS1307_YR,false), DEC);
  myFile.print('/');

  // close the file:
  myFile.close();
  digitalWrite(3, LOW);
  delay (1000);
  
}
void loop() {
  
  digitalWrite(3, HIGH);
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

sd.init() should be performed ONCE, not every pass through loop.

If I comment out the line "myFile.print(RTC.get(DS1307_YR,false), DEC);" the SD card stops to work.

Separate the code into reasonable pieces. What does RTC.get() return, if anything? What does "the SD card stops to work" mean? Does the SD card then stop working? Or does it start working?

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  // re-open the file for reading:
  // read from the file until there's nothing else in it:
  int data;

So, where is the code to implement the comments? What is data for?

Hi Paul thanks to reply.

sd.init() should be performed ONCE, not every pass through loop.

Yes, you are right. Originally sd.init() is in setup() on the sdfat.h examples, but I made some tests and forgot to move it back.

Separate the code into reasonable pieces. What does RTC.get() return, if anything? What does "the SD card stops to work" mean? Does the SD card then stop working? Or does it start working?

"Does the SD card then stop working"

So, where is the code to implement the comments? What is data for?

I removed some lines from original example code that is not important to reproduce the problem and forgot the comments and data. This is not important.

Thanks
Alex

Have you run out of code or ram space? The RTC library uses the I2C bus which doesn't conflict with the SDcard on the SPI bus, I can only think that you've got too much code or ram usage when using both libraries.

Hi Mark.

I found a code to get available memory and... 8 bytes free. If I remove the libraries I got 712 bytes free :frowning:

I put some blinks on the code to test what's wrong and I found that uC is resetting in the sd.init().

I think the solution is change to a atmega328. What you think?

Thanks.
Alex

extern int __bss_end;
extern int *__brkval;

int get_free_memory()
{
  int free_memory;

  if((int)__brkval == 0)
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);

  return free_memory;
}

I think the solution is change to a atmega328. What you think?

Depends whether you are changing from a 168 or a Mega, doesn't it.

8 bytes free.

That's not very many...

Yes, SD library takes a lot of room, the 328 is pretty much required - unless you can find a smaller SD library that does enough for you and allows everything to fit on a 168.

I have a 168.

Thanks guys.

Alex

Hey guys,I am new arduiners!!!

I have a problem with my RTC mod isn't the famous ref DS1307 which is well documented but RTC PCF8563 and i would like to get the hour in a file.txt on my SD card!!

I have a code of RTC which is working and i was trying to see how to write and erase data on SD card but i don't know how can i handling this problem!!

Thank you guys

I have a code of RTC which is working

Well, that's a start.

and i was trying to see how to write and erase data on SD card

Depending on how you open the file, erasing may not be necessary. There are examples in the SD library. I fail to see what the problem is. (And I fail to see your code.)