DeadON RTC, Micro SD Sheild, SPI - OH My!

Help!

I am a noob but having fun with Arduino. I recently embarked on my first project after several demo sketches.

I am trying to build a datalogger that keeps track of the temperature and humidity and writes the data to a MicroSD card.

I think I am running into an issue with SPI and having two ‘devices’ communicating over it.

I am running the DeanON RTC breakout, a MicroSD shield, and the RHT03 sensor all on a RedBoard.

I have researched SPI and moved the SS pin on the RTC to 7, leaving the SD Shield on 8.

Admittedly I am a noob at programming and have minimal experience but some exposure over time. I took the demo sketches from the various components of my project and have tried to weave them into one sketch and added some of my own research to resolve the issue.

The problem is once I enable writing to the SD card, I get the first read of the RTC fine and the data is written to the SD card as expected, upon the second query of the RTC I get an inaccurate value, usually something like 0/1/0 1:8:1 or something similar.

If I comment out the write to SD, the sketch runs fine dumping the values to the serial monitor correctly.

I am still researching, but any input would be greatly appreciated! I am so close! Sketch below.

Thanks! :smiley:

Sketch:

//Libraries

#include <SD.h> //SD Card Library

#include <SPI.h> //Serial Communication Library for RTC

#include <DHT.h> //Temp / Humidity Sensor Library

//DHT Sensor Initialization

#define DHTPIN 2 // what pin we’re connected to

#define DHTTYPE DHT22 // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//Variables

int chk;

float hum; //Stores humidity value

float temp; //Stores raw temperature value in Celsius

float temp_f; //Stores raw temperature converted to Fahrenheit

char SaveTimeDate[25];

//SPI SS Pin Definition

const int SS_SDCard=8;

const int SS_RTC=7;

void setup()

{

// SPI Initialization

pinMode(SS_SDCard, OUTPUT);

pinMode(SS_RTC, OUTPUT);

digitalWrite(SS_SDCard, HIGH); //SD Card not ACTIVE

digitalWrite(SS_RTC, HIGH); //RTC Clock not ACTIVE

// Open Serial Communication

Serial.begin(9600);

while (!Serial){

;

}

// SD Card Initialization

Serial.print(“Initializing SD card…”);

digitalWrite(SS_SDCard, LOW); // SD Card Active

if (!SD.begin(SS_SDCard)) {

Serial.println(“Card failed or not present”);

return;

}

Serial.println(“SD Card Initialized”);

digitalWrite(SS_SDCard, HIGH); // SD Card Inactive

delay(1000);

// RTC Initialization

SPI.begin();

SPI.setBitOrder(MSBFIRST);

SPI.setDataMode(SPI_MODE3); // both mode 1 & 3 should work

digitalWrite(SS_RTC, LOW);

SPI.transfer(0x8E);

SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled

digitalWrite(SS_RTC, HIGH);

delay(1000);

// DHT Sensor Initialization

dht.begin();

}

void loop()

{

GetData();

PrintToScreen();

// PrintToSD();

delay(15000); // Delay 15 seconds

}

void GetData() // this function will get the sensor data

{

//Read data and store it to variables hum and temp

hum = dht.readHumidity();

temp = dht.readTemperature();

temp_f = (1.8 * temp) + 32;

ReadTimeDate().toCharArray(SaveTimeDate, 50);

}

void PrintToScreen() // this function will Output to Serial

{

//Print time, temp and humidity values to serial monitor

Serial.print("Time: ");

Serial.print(SaveTimeDate);

Serial.print(" Humidity: ");

Serial.print(hum);

Serial.print(" %, Temp: ");

Serial.print(temp_f);

Serial.println(" Fahrenheit");

}

void PrintToSD() // this function will Output to SD Card

{

digitalWrite(SS_SDCard, LOW); // SD Card Active

// open the file. note that only one file can be open at a time,

// so you have to close this one before opening another.

// this opens the file and appends to the end of file

// if the file does not exist, this will create a new file.

File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// if the file is available, write to it:

if (dataFile) {

//Print date, time, temp and humidity value to SD card

dataFile.print(SaveTimeDate);

dataFile.print(“,”);

dataFile.print(hum);

dataFile.print(“,”);

dataFile.println(temp_f);

dataFile.close();

}

else

{

Serial.println(“error opening datalog.txt”);

}

digitalWrite(SS_SDCard, HIGH); // SD Card Inactive

}

//=====================================

String ReadTimeDate()

{

String temp;

int TimeDate [7]; //second,minute,hour,null,day,month,year

for(int i=0; i<=6;i++){

if(i==3)

i++;

digitalWrite(SS_RTC, LOW);

SPI.transfer(i+0x00);

unsigned int n = SPI.transfer(0x00);

digitalWrite(SS_RTC, HIGH);

int a=n & B00001111;

if(i==2){

int b=(n & B00110000)>>4; //24 hour mode

if(b==B00000010)

b=20;

else if(b==B00000001)

b=10;

TimeDate*=a+b;*
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate=a+b*10;
}
}
temp.concat(TimeDate[5]);
temp.concat(“/”) ;
temp.concat(TimeDate[4]);
temp.concat(“/”) ;
temp.concat(TimeDate[6]);
temp.concat(" , ") ;
temp.concat(TimeDate[2]);
temp.concat(“:”) ;
temp.concat(TimeDate[1]);
temp.concat(“:”) ;
temp.concat(TimeDate[0]);
return(temp);
}

I figured it out.

Had to change the SPI mode between accessing the RTC and writing to the SD card!

Also found a custom library I might try…

http://playground.arduino.cc/Main/DS3234SoftwareLibrary

Thanks for looking! :shock: