Go Down

Topic: DS1302 RTC acting weird (Read 270 times) previous topic - next topic

Lumis

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.
Code: [Select]
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:
Code: [Select]
// 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?

Delta_G

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?
If at first you don't succeed, up - home - sudo - enter.

jremington

#2
Nov 14, 2017, 04:39 am Last Edit: Nov 14, 2017, 04:40 am by jremington
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.

Delta_G

@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?
If at first you don't succeed, up - home - sudo - enter.

Lumis

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 https://www.velleman.eu/products/view/?id=435516
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 http://www.rinkydinkelectronics.com/library.php?id=5

jremington

Quote
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.

Delta_G

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.
If at first you don't succeed, up - home - sudo - enter.

jremington

Quote
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.

jremington


Delta_G

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.
If at first you don't succeed, up - home - sudo - enter.

jremington

#10
Nov 14, 2017, 08:12 pm Last Edit: Nov 14, 2017, 08:14 pm by jremington
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.

Delta_G

#11
Nov 14, 2017, 08:14 pm Last Edit: Nov 14, 2017, 08:18 pm by Delta_G
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.
If at first you don't succeed, up - home - sudo - enter.

Delta_G

Ok.  So it says that isn't an SPI bus but some abomination.  In that case carrion. 
If at first you don't succeed, up - home - sudo - enter.

Lumis

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: https://www.velleman.eu/products/view/?id=435516 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.

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.

Go Up