RTC DS3234 printing everything wrong!!

I am using the Sparkfun DS3234 RTC clock connected to Arduino MEGA 2560. I run this code:

Code:

#include <SPI.h>
const int  cs=8; //chip select 

void setup() {
  Serial.begin(9600);
  RTC_init();
  //day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
  SetTimeDate(11,12,13,14,15,16); 
}

void loop() {
  Serial.println(ReadTimeDate());
  delay(1000);
}
//=====================================
int RTC_init(){ 
	  pinMode(cs,OUTPUT); // chip select
	  // start the SPI library:
	  SPI.begin();
	  SPI.setBitOrder(MSBFIRST); 
	  SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work 
	  //set control register 
	  digitalWrite(cs, LOW);  
	  SPI.transfer(0x8E);
	  SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
	  digitalWrite(cs, HIGH);
	  delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){ 
	int TimeDate [7]={s,mi,h,0,d,mo,y};
	for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		int b= TimeDate[i]/10;
		int a= TimeDate[i]-b*10;
		if(i==2){
			if (b==2)
				b=B00000010;
			else if (b==1)
				b=B00000001;
		}	
		TimeDate[i]= a+(b<<4);
		  
		digitalWrite(cs, LOW);
		SPI.transfer(i+0x80); 
		SPI.transfer(TimeDate[i]);        
		digitalWrite(cs, HIGH);
  }
}
//=====================================
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(cs, LOW);
		SPI.transfer(i+0x00); 
		unsigned int n = SPI.transfer(0x00);        
		digitalWrite(cs, 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[i]=a+b;
		}
		else if(i==4){
			int b=(n & B00110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==5){
			int b=(n & B00010000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==6){
			int b=(n & B11110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else{	
			int b=(n & B01110000)>>4;
			TimeDate[i]=a+b*10;	
			}
	}
	temp.concat(TimeDate[4]);
	temp.concat("/") ;
	temp.concat(TimeDate[5]);
	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);
}

And I get these results:

0/0/0     0:85:1
0/0/0     0:0:2
0/0/0     0:85:3
0/0/0     0:0:4
0/0/0     0:85:5
0/0/0     0:0:6
0/0/0     0:85:7
0/0/0     0:0:8
0/0/0     0:85:9
0/0/0     0:0:10
0/0/0     0:85:11
0/0/0     0:0:12
0/0/0     0:85:13
0/0/0     0:0:14
0/0/0     0:85:15

I have read this post:

https://forum.arduino.cc/index.php?topic=632810.0 but it did not help. Any idea?

I use VCC = 3.3 Volt as this datasheet suggests:

alex5678:
I use VCC = 3.3 Volt as this datasheet suggests:
https://www.sparkfun.com/datasheets/BreakoutBoards/DS3234.pdf

In which paragraph does it suggest that? It lists 3.3V as a "typical" supply voltage, maximum is 5.5V. They're not saying you "should" run it on 3.3V, just "it usually is" or "can". If you run it on 3.3V and connect it to the Mega 5V I/O pins, you might risk damaging one of the devices.

From the Sparkfun product hook-up guide:

Powering the DS3234

The Deadon RTC breakout board does not include any voltage regulation, so power supplied to the "VCC" pin should be kept within the DS3234's (wide) recommended operating range: 2.0 to 5.5V. Fortunately, the breakout should work with either 3.3V or 5V development boards!

Ok, I changed it to 5 Volt VCC. Nothing, again. The same output as before.

Why are you concerned? It's a demo sketch. Let it be wrong! For practical purposes, wouldn't you use the SparkFun_DS3234_RTC_Arduino_Library ?

@aarg: You are amazing! Thanks! It works...

Hello, I have uploaded this code:

to the Arduino MEGA 2560. It works fine until I plug it off the power. In order to have correct time/hour etc, it needs to upload each time the code otherwise the clock prints wrong results..... Any idea how to fix that??

Thanks

Is the battery in backwards?

I have just checked it. The battery is correctly installed.

Do you see something wrong on the code??? (I copied from the previous link that I have posted):

/******************************************************************************
DS3234_RTC_Demo.ino
Jim Lindblom @ SparkFun Electronics
original creation date: October 2, 2016
https://github.com/sparkfun/SparkFun_DS3234_RTC_Arduino_Library

Configures, sets, and reads from the DS3234 real-time clock (RTC).

Resources:
SPI.h - Arduino SPI Library

Development environment specifics:
Arduino 1.6.8
SparkFun RedBoard
SparkFun Real Time Clock Module (v14)
*****************************************************************************/
#include <SPI.h>
#include <SparkFunDS3234RTC.h>

// Comment out the line below if you want date printed before month.
// E.g. October 31, 2016: 10/31/16 vs. 31/10/16
#define PRINT_USA_DATE

//////////////////////////////////
// Configurable Pin Definitions //
//////////////////////////////////
#define DS13074_CS_PIN 10 // DeadOn RTC Chip-select pin
//#define INTERRUPT_PIN 2 // DeadOn RTC SQW/interrupt pin (optional)

void setup() 
{
  // Use the serial monitor to view time/date output
  Serial.begin(9600);
#ifdef INTERRUPT_PIN // If using the SQW pin as an interrupt
  pinMode(INTERRUPT_PIN, INPUT_PULLUP);
#endif

  // Call rtc.begin([cs]) to initialize the library
  // The chip-select pin should be sent as the only parameter
  rtc.begin(DS13074_CS_PIN);
  //rtc.set12Hour(); // Use rtc.set12Hour to set to 12-hour mode

  // Now set the time...
  // You can use the autoTime() function to set the RTC's clock and
  // date to the compiler's predefined time. (It'll be a few seconds
  // behind, but close!)
  rtc.autoTime();
  // Or you can use the rtc.setTime(s, m, h, day, date, month, year)
  // function to explicitly set the time:
  // e.g. 7:32:16 | Monday October 31, 2016:
  //rtc.setTime(16, 32, 7, 2, 31, 10, 16);  // Uncomment to manually set time

  // Update time/date values, so we can set alarms
  rtc.update();
  // Configure Alarm(s):
  // (Optional: enable SQW pin as an interrupt)
  rtc.enableAlarmInterrupt();
  // Set alarm1 to alert when seconds hits 30
  rtc.setAlarm1(30);
  // Set alarm2 to alert when minute increments by 1
  rtc.setAlarm2(rtc.minute() + 1);
}

void loop() 
{
  static int8_t lastSecond = -1;
  
  // Call rtc.update() to update all rtc.seconds(), rtc.minutes(),
  // etc. return functions.
  rtc.update();

  if (rtc.second() != lastSecond) // If the second has changed
  {
    printTime(); // Print the new time
    
    lastSecond = rtc.second(); // Update lastSecond value
  } 

  // Check for alarm interrupts
#ifdef INTERRUPT_PIN
  // Interrupt pin is active-low, if it's low, an alarm is triggered
  if (!digitalRead(INTERRUPT_PIN))
  {
#endif
    // Check rtc.alarm1() to see if alarm 1 triggered the interrupt
    if (rtc.alarm1())
    {
      Serial.println("ALARM 1!");
      // Re-set the alarm for when s=30:
      rtc.setAlarm1(30);
    }
    // Check rtc.alarm2() to see if alarm 2 triggered the interrupt
    if (rtc.alarm2())
    {
      Serial.println("ALARM 2!");
      // Re-set the alarm for when m increments by 1
      rtc.setAlarm2(rtc.minute() + 1, rtc.hour());
    }
#ifdef INTERRUPT_PIN
  }
#endif
}

void printTime()
{
  Serial.print(String(rtc.hour()) + ":"); // Print hour
  if (rtc.minute() < 10)
    Serial.print('0'); // Print leading '0' for minute
  Serial.print(String(rtc.minute()) + ":"); // Print minute
  if (rtc.second() < 10)
    Serial.print('0'); // Print leading '0' for second
  Serial.print(String(rtc.second())); // Print second

  if (rtc.is12Hour()) // If we're in 12-hour mode
  {
    // Use rtc.pm() to read the AM/PM state of the hour
    if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
    else Serial.print(" AM");
  }
  
  Serial.print(" | ");

  // Few options for printing the day, pick one:
  Serial.print(rtc.dayStr()); // Print day string
  //Serial.print(rtc.dayC()); // Print day character
  //Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
  Serial.print(" - ");
#ifdef PRINT_USA_DATE
  Serial.print(String(rtc.month()) + "/" +   // Print month
                 String(rtc.date()) + "/");  // Print date
#else
  Serial.print(String(rtc.date()) + "/" +    // (or) print date
                 String(rtc.month()) + "/"); // Print month
#endif
  Serial.println(String(rtc.year()));        // Print year
}

Is there voltage on the battery? Is it the right battery? Sparkfun would not publish a non-working test sketch. Check your connections also. Is there any chance the pins are in use by another peripheral? Etc. etc…

I would like to see photos of your hardware connections.

I am trying to find out what is wrong!!

This is the output I got:

eeee
ALARM 2!
ALARM 1!
ALARM 1!
ALARM 1!
ALARM 1!
ALARM 1!
ALARM 1!
ALARM 1!
2:130:85 |  - 48/1/2
ALARM 2!
ALARM 1!
1:135:85 |  - 48/1/2
ALARM 2!
ALARM 1!
ALARM 1!
18:15:00 | {s⸮⸮⸮⸮⸮⸮W⸮⸮}<⸮⸮o⸮+ֿ1⸮⸮⸮Z?⸮⸮x⸮⸮h⸮⸮⸮s⟭74⸮Y⸮⸮⸮⸮k⸮n⸮⸮p%⸮6⸮⸮⸮⸮]⸮⸮⸮⸮?⸮⸮ Ǡϵ%⸮⸮6⸮קa⸮k⸮⸮⸮⸮|⸮}⸮⸮Rd⸮(_⸮⸮Wի ⸮⸮⸮F⸮⸮⸮1}]⸮V⸮h⸮7⸮⸮⸮⸮_⸮⸮⸮⸮䷡u輇⸮l⸮u⸮_%⸮⸮
⸮h⸮U⸮⸮⸮w⸮6gG⸮⸮⸮z⸮⸮Ե⸮ ⸮⸮_⸮{ɔ⸮2⸮j⸮;⸮&⸮ԇEGU⸮⸮⸮⸮⸮⸮=⸮c⸮⸮~⸮⸮⸮⸮곱⸮ϖ1⸮⸮⸮⸮f⸮⸮⸮⸮؉⸮⸮w]⸮⸮⸮<O⸮⸮)u&⸮⸮\⸮⸮W⸮⸮⸮⸮on⸮⸮⸮⸮⸮⸮⸮eY⸮}⸮Qt܌⸮q8⸮⸮⸮⸮⸮⸮]}I⸮⸮⸮⸮7⸮⸮⸮:⸮u|⸮)⸮z⸮⸮fs - 0/41/29
ALARM 2!
4:164:03 | ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ - 10/40/40
ALARM 2!
10:162:07 | ⸮eeeeeeeeeeeeeeeeeeeeeeeeeeYeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

I run the code on Arduino MEGA 2560 with CS = 53, and I have connected 2 SPI devices in total on the Arduino, this, and an ethernet shield (CS = 4). About the battery, is CR1220 Maxell, 3Volt, and I have just measured 2,5 Volt on it with the Voltmeter. Any ideas?? Should the battery be rechargeable? Does the RTC module recharge the battery?