B4R Tutorial RTC DS1302 Example

Hi,

this is an example of using the Real Time Clock DS1302 (from sunfounder).
There are two versions:
1. Manual: Set the time manual befor uploading. After uploading the time is logged every seconds by a timer.
2. Commands: Various commands are accepted via the serial line (using Asyncstream):
  • set: Set the RTC time year, month , dayofmonth , hour , minute , second , dayofweek
  • get: Get the RTC timeformat YYYY-mon-dayofweek hour:minute:second dayofweekname
  • getbytes: Get the RTC time as an array of bytes with year, month , dayofmonth , hour , minute , second , dayofweek
  • start / stop: Start & Stop Logging
Wiring
B4X:
DS1302 ---- Arduino (Wirecolor)
SCL   ------- 7 yellow
SDA  ------- 6 brown
RST  ------- 5 white
VCC  ------- 3.3v red
GND ------- GND black

upload_2016-6-2_13-1-59.png


2016.06.2 First Version
  • The DS1302 library is included. Copy to the Arduino IDE Libraries in folder ds1302.
  • The code uses Inline C with ds1302.h included.
  • Have not been successful with the attempt to wrap the library DS1302, therefor solved using Inline C.
  • A type is used for the timestamp.
  • Overall, must say, was not easy to get started - several wrapping attemps not successful, then finding best way to handle timestamp went back and forth ... but anyhow this is now working fine.
  • Next: when the I2C LCD arrives will amend with a clock.
  • Source code attached and documented.
2016.06.03 Update
  • New library rDS1302 wrapping DS1302.
  • Build two versions Manual and Commands using the rDS1302 Library.
  • Attachment updated which contains:
  • Example using Inline C with the DS1302 Library (rtcds1302-inlinec-command, rtcds1302-inlinec-manual).
  • Example using the wrapped library rDS1302 (rtcds1302-lib-command, rtcds1302-lib-manual).
  • Examples tested with an Arduino UNO and Sunfounder DS1302.
  • The new rDS1302 is in the attachment folder Library rDS1302.
B4R Serial Connector example for setting the date & time. A must tool for testing.
upload_2016-6-3_17-34-45.png
 

Attachments

  • B4RHowToRTCDS1302.zip
    347.2 KB · Views: 1,134
Last edited:

Toley

Active Member
Licensed User
Longtime User
The best way to test it is to use this kind of module available everywhere in different shape. (don't mind the link I've post it's random choice of a nice looking one).
sku_417961_1.jpg
 

rwblinn

Well-Known Member
Licensed User
Longtime User
This is how I would have wrapped this library.

I don't have this chip so I cannot fully test it.
First of all, thanks for taking the time and providing another great howto wrap a library example.
Have tested and received error:
B4X:
In file included from sketch\B4RDefines.h:23:0,
                 from sketch\rDS1302.h:2,
                 from sketch\DS1302.cpp:6:
b4r_main.h:13: error: 'B4RDS1302' in namespace 'B4R' does not name a type
static B4R::B4RDS1302* _ds1302;
        ^
b4r_main.h:14: error: 'DSTime' in namespace 'B4R' does not name a type
static B4R::DSTime* _ds1302time;
        ^
exit status 1
'B4RDS1302' in namespace 'B4R' does not name a type

The B4R Code:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public ds1302 As DS1302
    Public ds1302time As DSTime
    Public Timer1 As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    SetRTCTime
    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True
End Sub

Sub SetRTCTime
    ds1302time.Initialize
    ds1302time.Years = 2016
    ds1302time.Months = 6
    ds1302time.DayOfMonth = 2
    ds1302time.Hours = 16
    ds1302time.Minutes = 53
    ds1302time.Seconds = 9
    ds1302time.DayOfWeek = 4
    ds1302.Initialize(5,6,7)
    ds1302.SetTime(ds1302time)
End Sub

Sub GetRTCTime
    Dim currenttime As DSTime = ds1302.CurrentTime
    Log("RTC Time ", _
        NumberFormat(currenttime.years,4,0), " ", NumberFormat(currenttime.months,2,0), "-", NumberFormat(currenttime.dayofmonth,2,0), _
        " ", _
        NumberFormat(currenttime.hours,2,0), ":", NumberFormat(currenttime.minutes,2,0), ":", NumberFormat(currenttime.seconds,2,0), _
        " ", _
        currenttime.dayofweek)
End Sub

Sub Timer1_Tick
    GetRTCTime
End Sub
 

Beja

Expert
Licensed User
Longtime User
Hi rwblinn and thanks for the promising library..
Would you please redraw the circuit diagram with the module of the RTC instead of the chip?
The module has a lithium battery that will keep the TIME even if power is turned off. (for limited time though)

Thanks in advance.
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

could not find a full DS1302 part in fritzing. Only the chip was listed - so took that one.
For the experiment, used a sunfounder RTC ds1302 with the pins marked:
upload_2016-6-19_10-1-58.png
 

Mostez

Well-Known Member
Licensed User
Longtime User
hello,
I followed your example and updated libraries as instructed, but i've got this strange results,

AppStart
RTC Time 2011 83-01 11:24:08 Tue
RTC Time 2016 06-03 16:48:11 Fri
RTC Time 2011 83-01 11:24:09 Tue
RTC Time 2016 06-03 16:48:13 Fri
RTC Time 2011 83-01 11:24:10 Tue
RTC Time 2016 06-03 16:48:15 Fri

i'm using Arduino Mega 2560 and I assembled the DS1302 board on test board, the very strange thing is, clock is running correctly but i don't know what is that 2011 line, I tested my DS1302 test board with this native arduino code and it worked OK


DS1302 Real Time Clock
Version 2, March 2013
Time = 10:08:00, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:05, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:10, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:15, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:20, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:25, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
Time = 10:08:30, Date(day of month) = 15, Month = 4, Day(day of week) = 2, Year = 2013
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

thats strange. Just tested the DS1302 connected to an Arduino MEGA.
Versions used: Win 10, Arduino IDE 1.6.11, B4R 1.20, rDS1302 Lib 1.00.
Wiring
B4X:
'DS1302 ---- Arduino MEGA =Wirecolor
'SCL ------- 8 =yellow
'SDA ------- 7 =brown
'RST ------- 6 =white
'VCC ------- 3.3v =red
'GND ------- GND =black

Init the DS1302
B4X:
ds1302.Initialize(6,7,8)

Output
B4X:
AppStart
RTC Time 2016 09-27 16:20:10 Tue
RTC Time 2016 09-27 16:20:11 Tue
RTC Time 2016 09-27 16:20:12 Tue
RTC Time 2016 09-27 16:20:13 Tue
RTC Time 2016 09-27 16:20:14 Tue
RTC Time 2016 09-27 16:20:15 Tue
RTC Time 2016 09-27 16:20:16 Tue
 

Mostez

Well-Known Member
Licensed User
Longtime User
I soldered 1uf decoupling capacitor close to ds1302 and replaced the 3V coin cell with fresh one, it works OK now. let me ask you one more question please, how can I change time format from 24H to 12H ?
Thanks
 

Mostez

Well-Known Member
Licensed User
Longtime User
I faced same problem, the chip should be powered by 3.3V only, not 5V
 

Mostez

Well-Known Member
Licensed User
Longtime User
what module you're using? try to pull up RST, DAT and CLK pins with 10K resistors, did you connect the time keeping coin cell (CR1220 or CR2032)
 

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
what module you're using? try to pull up RST, DAT and CLK pins with 10K resistors, did you connect the time keeping coin cell (CR1220 or CR2032)
..you think, I need to pull em up? thought, they were defined as MODE_INPUT_PULLUP, but shurely, you can be right... thank you

time keeping cell is in, but makes no difference.

...no, pullup makes no changes!
 
Last edited:

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
... there is an array problem with day of week on sunday too - the c#include adresses from 1 to 7, the b4a from 0 to 6
 

Shay

Well-Known Member
Licensed User
Longtime User
Hi
I also have this issue:
I can set the date and time, if I disconnect the usb from the nano, and connect back the time is ok (I assume battery is ok)
but time is not counting up (Same all the time)
how do I solve it? (I am using 3.3v)
 

Shay

Well-Known Member
Licensed User
Longtime User
seems to work ok if I use the rtcds1302-inlinec and not the rtcds1302-lib
 
Top