Please help newbie regarding a DS1302 RTC module[Solved]

Hi,
I bought a DS1302 RTC module (the blue one) from Dx.com [http://dx.com/p/ds1302-real-time-clock-module-blue-151999]

Now I've managed to connect the RTC module to the board. I'm using Henning Karlsen's DS1302 library [Electronics - Henning Karlsen]. It is working so and so, I'm getting weird output:

Saturday 08.06.2013 -- 14:40:13
xxxxxxxxx 02.01.2004 -- 05:10:05
Saturday 08.06.2013 -- 14:40:15
xxxxxxxxx 02.01.2004 -- 05:10:05
Saturday 08.06.2013 -- 14:40:17

The code I'm using is one of the included examples; DS1302_Serial_Easy

Edit: Original thread was started to clarify how to connect the module to the Arduino.

Solution was that CE is the same as RST and that I should use VCC2 (connect to the 5V pin of the Arduino)

Edit2: Use the code from Arduino Playground - HomePage instead of the Henning Karlsen library

Edit3: I'm unable to get the RTC to keep time after partial power off (with the battery connected though) see Erdins post for that.

Thanks Erdin for the help!

/Daggeteo

This page shows a drawing how to connect it, Arduino Playground - DS1302

Erdin:
This page shows a drawing how to connect it, Arduino Playground - HomePage

Thanks for the link, it isn't working at the moment but I have seen that page before. The picture caption answered one of my questions, RST appears to be the old name for CE. The way I interpreted that page is that it was for the chip and not the module. Perhaps there it doesn't make a difference.

The DS1302 can be easily connected to the Arduino. Three pins are needed for the interface (CE, I/O, SCLK), and Vcc2 should be connected to +5V (or +3.3V). The Vcc1 is for a battery or a rechargable battery or a supercap.

I don't follow this. If I use a battery for the module should I still connect any of the VCC1/2 to the board at all?

Yes, RST and CE are the same.

You board has a holder for a battery. That battery is connected to Vcc1. So don't connect anything else to Vcc1.
To be able to communicate with the chip, you have to use Vcc2.
Connect the 5V pin of the Arduino to Vcc2.

Vcc2 can be 5V or no voltage, the chip keeps on running with the battery at Vcc1.

Erdin:
Yes, RST and CE are the same.

You board has a holder for a battery. That battery is connected to Vcc1. So don't connect anything else to Vcc1.
To be able to communicate with the chip, you have to use Vcc2.
Connect the 5V pin of the Arduino to Vcc2.

Vcc2 can be 5V or no voltage, the chip keeps on running with the battery at Vcc1.

Awesome, much thanks, you answered my question! Will give this a go tomorrow.

I'll update this post tomorrow.

Erdin,
Is there any chance you could evolve how to solve the issue in this thread [DS1302 issue - Networking, Protocols, and Devices - Arduino Forum, the same as which I'm having problems with now.

Thanks!

I don't know what the problem could be.
Like I wrote in the other thread, sometimes a bad crystal can have such results.
If the time is set in setup(), you should do that once. Every time you open the serial monitor, the Arduino board is reset and that could program the same time in to the RTC.

You could buy a DS1307. That one has a normal I2C bus and many and good example code is available.
But also the DS1307 could have a bad crystal, you will find a number of topics on that in this forum.

The DS1302 has strange timings and delays. Code that has it 99% correct, is not good enough for the DS1302. Could you test the code from the playground ?

Erdin:
I don't know what the problem could be.
Like I wrote in the other thread, sometimes a bad crystal can have such results.
If the time is set in setup(), you should do that once. Every time you open the serial monitor, the Arduino board is reset and that could program the same time in to the RTC.

You could buy a DS1307. That one has a normal I2C bus and many and good example code is available.
But also the DS1307 could have a bad crystal, you will find a number of topics on that in this forum.

The DS1302 has strange timings and delays. Code that has it 99% correct, is not good enough for the DS1302. Could you test the code from the playground ?

You are awesome! It seems to be working :slight_smile: Very satisfying to see it ticking away!

Next project will be to figure out how to store the value when I power off the Arduino. But for now this is what I needed. Thanks Erdin, your answers were swift and helpful!

I'm glad that I could help.

If the battery is connected, the RTC continues with the clock.
So the next time you turn on the Arduino and read the time, it should have the current time.

Erdin:
I'm glad that I could help.

If the battery is connected, the RTC continues with the clock.
So the next time you turn on the Arduino and read the time, it should have the current time.

I haven't been able to figure out how to get RTC to keep and store the time.

Basically I used the code from the playgrounds-link that you provided and tried modifying it(mainly to get it to run on my LCD, which works) but I'm clearly missing something. I upload the sketch, pull out the USB, plug it in to the power socket with an adapter the clock starts over from the beginning.

The main reason for this I believe is because I set the clock through the code and it runs through that code each time it starts up. I tried handling that by adding a Boolean but that didn't work obviously since that too is also reset at power up. I figured that with a boolean I could get that part of the code to run only once. Then I attempted to store the variable in the clocks ram but I didn't grasp how to do that properly so I'd love some pointers.

Link to code - https://dl.dropboxusercontent.com/u/7067820/RTC%26LCD.txt

Edit: Since code is too lengthy for posting in the forum I'll put a link to a dropbox file...

Clarification: The battery is connected and brand new so it shouldn't be the source of the error, I am certain that it is my programming skills at fault here.

See this:

// Remove the next define, 
// after the right date and time are set.
#define SET_DATE_TIME_JUST_ONCE

If that define is declared, the code to set the date and time is executed.
You can remove that define by commenting it out.
Like this:

// #define SET_DATE_TIME_JUST_ONCE

After that the define is no longer declared and the part between "#ifdef SET_DATE_TIME_JUST_ONCE" and "#endif" is not compiled and not put in the Arduino.

#ifdef means: if defined ...

Erdin:
See this:

// Remove the next define, 

// after the right date and time are set.
#define SET_DATE_TIME_JUST_ONCE




If that define is declared, the code to set the date and time is executed.
You can remove that define by commenting it out.
Like this:


// #define SET_DATE_TIME_JUST_ONCE




After that the define is no longer declared and the part between "`#ifdef SET_DATE_TIME_JUST_ONCE`" and "`#endif`" is not compiled and not put in the Arduino.

#ifdef means: if defined ...

Embarrassing that I missed that! Did as you suggested and commented out that line after the time was set and it worked just like I wanted.

Again, many thanks!

#ifdef SIMPLE
char answer[] = "Cool !";
#else
char answer[] = "I am glad that I could help and that my post had a positive effect on your project";
#endif

Erdin:

#ifdef SIMPLE

char answer[] = "Cool !";
#else
char answer[] = "I am glad that I could help and that my post had a positive effect on your project";
#endif

Lol, you learn something new everyday :slight_smile:

Hello, I have been looking for the answer all the afternoon, and finally I have reach the solution :
Keep de battery on, but you will have to unplug de Vcc wire from the arduino to the RTC, then IT WORKS.

The instructions above worked for me but I haven't tried unplugging the VCC wire. As long as it works, right? :slight_smile: