DS1302 RTC acting weird

Just got DS1302rtc for little project and cannot figure out how to make it work. I have tried few different libraries and the problem still exists. 1 second it showing 00:00:00 time and 00:00:2000 date the other second it showing good time.

02:06:25
14.11.2017
00:00:00
00.00.2000
02:06:27
14.11.2017
00:00:00
00.00.2000
02:06:29
14.11.2017
00:00:00
00.00.2000

and here is the code:

// DS1302:  CE pin    -> Arduino Digital 2
//          I/O pin   -> Arduino Digital 3
//          SCLK pin  -> Arduino Digital 4
#include <DS1302.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
DS1302 rtc(2, 3, 4);
LiquidCrystal_I2C lcd(0x27,20,4);
void setup()
{
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);

Serial.begin(9600);
 
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
}

void loop()
{

  Serial.println(rtc.getTimeStr());
  Serial.println(rtc.getDateStr());
  lcd.setCursor(0,0);
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print(rtc.getDateStr());

  delay (1000);
}

does any one know how to fix it?

This isn't odd on several counts. I'm not sure what RTC library you are using, but I am accustomed to seeing an rtc.begin() line somewhere. It is also odd that you have an SPI device but aren't using the SPI hardware to talk to it and instead are using other pins.

Can you describe where you got your library and why you aren't using the normal SPI pins?

Post links to the exact DS1302 module and library that you are using. Did you connect the grounds?

@Delta_G: most people use bit-banged I/O for that chip, such as in the library found here.

jremington:
@Delta_G: most people use bit-banged I/O for that chip, such as in the library found here.

Why? I see lots of libraries that are written by people who obviously missed some important point. I just dealt with one the other day that used a SoftwareSerial instance on pins 0 and 1. So the fact that some library did it doesn’t impress me. Why would one want to bit bang that when there is a hardware SPI port right there already?

Delta_G:
Why? I see lots of libraries that are written by people who obviously missed some important point. I just dealt with one the other day that used a SoftwareSerial instance on pins 0 and 1. So the fact that some library did it doesn't impress me. Why would one want to bit bang that when there is a hardware SPI port right there already?

I'm using this module VMA301: DS1302 REAL-TIME CLOCK MODULE / WITH BATTERY CR2032 (2 pcs) – Velleman – Wholesaler and developer of electronics
tried using library from the downloads section but can't even set time. When compiled SerialSet getting bunch of unreadable characters. If i set time with different library then using above library example DS1302_Serial.ino then first second it boots it shows up time good after one second im getting this error "DS1302 read error! Please check the circuitry."

Now library is from DS1302 - Rinky-Dink Electronics

Why would one want to bit bang that when there is a hardware SPI port right there already?

For the same reasons people use software serial.

Can't be. People usually use SoftwareSerial because the normal serial interface is being used for something else. But with I2C they could just share the bus. The only reason I could imagine would be if the two devices somehow had the same address.

When compiled SerialSet getting bunch of unreadable characters.

Sounds like you have a serial port Baud rate mismatch.

If you are getting read errors, the module may be defective or have poorly soldered connections. If you bought two, try the other one.

Can't be.

I disagree.

Ok. What would be another good reason to waste those resources? Remember, we are talking about a scenario where the programmer is already using Wire for something. So it isn't to avoid Wire.

One possible explanation is that SPI hardware can't be used (or, no one has figured out how) with this device, as suggested by this Playground entry. There is this discussion as well.

A quick search failed to turn up an SPI hardware implementation of the protocol.

Wire is I2C and would not be appropriate.

Sorry I said I2C earlier. I meant SPI. I would swear that I've used that RTC before and I've never used any sort of bit banged SPI. Maybe it was a different chip. (EDIT. It was and now I remember why. These chips suck badly)

What can you do with software SPI that you can't do with the hardware version.

Ok. So it says that isn't an SPI bus but some abomination. In that case carrion.

jremington:
Sounds like you have a serial port Baud rate mismatch.

If you are getting read errors, the module may be defective or have poorly soldered connections. If you bought two, try the other one.

I tried both and it's acting same... It don't think they both can be defective. At first when I tried library from here: VMA301: DS1302 REAL-TIME CLOCK MODULE / WITH BATTERY CR2032 (2 pcs) – Velleman – Wholesaler and developer of electronics it was showing time how it's suppose to be every second. But the date was not set correctly after few tries to set date it started to showing that error. I tried to remove the battery tired both modules and now it's the same every time.

I suggest to use a standard I2C RTC module like the DS1307 or (much better) DS3231.

These are readily available and are what most people use, so there is much more help available if you run into trouble.

jremington:
DS3231.

That’s the one I used.

Hey sorry about earlier, I got confused between this thread and another where they were using Wire and SoftI2C at the same time and were running into issues that Wire would have solved. For some reason I got the two threads confused and didn’t know what I was talking about.

jremington:
I suggest to use a standard I2C RTC module like the DS1307 or (much better) DS3231.

These are readily available and are what most people use, so there is much more help available if you run into trouble.

yeah I heard that... I have already ordered DS3231. But shipping to my country sometimes takes more than 1 month so I thought to play around with DS1302 while waiting because it's the only module what local store can offer.

The DS1302 is a real time clock but works with a trickle charger for battery back up. Unless you specifically want that feature, most folk start with the DS1307 or the more accurate DS3231. DS1307 is available in DIP8, but the 3231 comes in a SOIC package - just use a SOC to DIP adaptor
There are libraries for both for example DS1307RTC (the DS1307 and DS3231 are pretty much interchangeable with these libraries if you just want the basic times and dates)
You will also need the Wire.h library for comms and TimeLib.h to really get at the functions.
If you really want to get down to basics and work without a specific clock library, have a look athttp://tronixstuff.com/2014/12/01/tutorial-using-ds1307-and-ds3231-real-time-clock-modules-with-arduino/
Have a look at Paul Stoffregens Github repository (look for DS1307RTC library)
Jack Christensen has a TimeZone library if you want daylight saving etc.
Just using the DS1307RTC library and one of the examples like ReadTime or SetTime will get you started
The TimeLib library takes the time from the RTC - nice combination

tigger:
The TimeLib library takes the time from the RTC - nice combination

Yup, use the setSyncProvider method and you never have to think about the RTC again. Just pretend that the time library is accurate now.