data logging shield with arduino mega

I have an arduino Mega card , and is connected to a Deek-Robot SD/RTC datalogging shield(V1.0). I am powering this off the USB port. I’ve been able to get the SD read/write/access to work just fine. But after trying many dfferent sketches/libraries to set the Real Time clock I am stumped. The shield uses a DS1307 chip in it, with a battery (it is plugged in, and has voltage).
I’ve noticed that when plugging the boards together not ALL the shields pins, connect to the Arduino Mega’s pins (i.e. Shields SDA & SCL not connected to MEGA. On the opposite side the Shields IO & Ref pins don’t connect. When I run “TimeRTCSet” sketch to set the clock I get the following output…

Unable to sync with the RTC
0:00:00 0 0 1970
0:00:01 1 1 1970
0:00:02 1 1 1970
0:00:03 1 1 1970
0:00:04 1 1 1970

When I run the “Set Time” sketch from the “DS1307RTC” library I get these results…

RTC is NOT running!
2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39

2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39

I understand the time needs to be set before can use this, but none of the sketch’s I’ve tried seem to be able to SET the time here.
On the MEGA card the SDA is pin 20, and SCL is pin 21. On the shield there are the two pins that didn’t conect via header between the two boards. I tried running jumpers from the Shield to the MEGA to see if that would make any difference… it didn’t.

Another sketch “Set Time” in “DS1307RTC” libary gave me the following results…

DS1307 Communication Error :-{
Please check your circuitry
I’ve attached that code here…
Code: [Select]
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,
“Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”

tmElements_t tm;

void setup() {
bool parse=false;
bool config=false;

// get the date and time the compiler was run
if (getDate(DATE) && getTime(TIME)) {
parse = true;
// and configure the RTC with this info
if (RTC.write™) {
config = true;

while (!Serial) ; // wait for Arduino Serial Monitor
if (parse && config) {
Serial.print(“DS1307 configured Time=”);
Serial.print(", Date=");
} else if (parse) {
Serial.println(“DS1307 Communication Error :-{”);
Serial.println(“Please check your circuitry”);
} else {
Serial.print(“Could not parse info from the compiler, Time=”");
Serial.print("", Date="");

void loop() {

bool getTime(const char *str)
int Hour, Min, Sec;

if (sscanf(str, “%d:%d:%d”, &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;

bool getDate(const char *str)
char Month[12];
int Day, Year;
uint8_t monthIndex;

if (sscanf(str, “%s %d %d”, Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;

i try all these lib to UNO and it is working 100%…

Any Geniuses that can help me here?

Do not cross-post. Other thread removed.

Since the Wire library is used you need to connect to the SCL and SDA pins. On the UNO those are duplicates of A4 and A5 but on the MEGA they are duplicates of the Pin 20 and Pin 21. Try bending the shield’s A4 and A5 pins out to the side so they don’t connect to the MEGA and running wires from A4 to Pin 20 and A5 to Pin 21.

I just one to confirm that the post from “johnwasser” above works! Just bend the pins and connect according the his reply and you are up running!

Thanks! =)

I’m searching and searching through all these posts to try and figure out why my cheapie Deek Robot SD logger won’t work on a mega. I had it running smoothly on an uno but I realised I needed extra serial ports for my application and slapped the whole setup onto a mega. What I have done:

  1. changed chipSelect to 53
  2. connected wires from A4 (SDA) to mega 20, and A5 to 21
  3. followed @johnwasser 's advice and bent the A4 and 5 pins out so they don’t connect to the mega

Still no luck.

Using a basic sketch from examples:

  SD card test

 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.

 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		Pin 4 used here for consistency with other Arduino examples

 created  28 Mar 2011
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
// include the SD library:
#include <SPI.h>
#include <SD.h>
#include <Wire.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;

void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
  } else {
    Serial.println("Wiring is correct and a card is present.");

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
    case SD_CARD_TYPE_SD2:

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");

  // list all files in the card with date and size | LS_DATE | LS_SIZE);

void loop(void) {


You changed the CS to 53, .....why? The shield is hardwired for CS on D10. There isn't any reason to NOT use D10 on the Mega, is there? MOSI, MISO, and CLK are 11, 12, and 13 on the shield, same for the Mega

tinman13kup: MOSI, MISO, and CLK are 11, 12, and 13 on the shield, same for the Mega

Nonsense. The SPI bus is on pins 50>52 on the Mega, not 11>13. It is also on the ICSP 6-pin cluster which is common with the Uno. I imagine your shield can be painlessly adapted for the Mega by installing a 6-pin female cluster, and use the ICSP for that purpose.

I would be inclined to clip pins 11>13 from the shield. It is not as if you are short of them. On shields properly designed for both Uno and Mega, these pins are just pass-through.

The chip select for SD is what the chip select pin is wired to, which is unchanged. In your case, I assume it is pin 10, and I don't think there is any need to change it. But, with the Mega, you need to nominate pin 53 for OUTPUT, which you do not appear to have done. This is required even if you don't use it. I think you will see comment to this effect in the SD examples included in the IDE.

So what I missed was to connect was to connect the following from SD to MEGA:

10 to 53 11 to 51 12 to 50 13 to 52, and now it works.

Like I mentioned earlier also: A4 to 20 A5 to 21

And I bent out A4 and A5 pins so they don't connect into to the mega. I just left them like that after doing the 10, 11, 12, 13 connections so I cannot confirm whether it is really necessary.


pinMode(53, OUTPUT);

10 to 53

No. You can continue to use pin 10 as nominated for CS.

11 to 51
12 to 50
13 to 52, and now it works.

OK. You also had the more elegant option of using the ICSP pins as described, which are there for that purpose.

Like I mentioned earlier also:
A4 to 20
A5 to 21
And I bent out A4 and A5 pins so they don’t connect into to the mega.

OK. I don’t think there is any choice about that.


pinMode(53, OUTPUT);

Yes, you have to call the pin, but you don’t have to use it. See my previous.

None of the replies really addressed the inability to set the date.

I'm using a leonardo with Keyes data logging shield. Tried all the options above with no success.

Any help appreciated.

w00dee: None of the replies really addressed the inability to set the date.

No surprise there, the thread was about getting the connections right on the Mega. This has nothing to do with a Leonardo.