HOME  »  ARDUINO MAIN PAGE »  HINTS AND HOW-TOs »  NuELEC DATALOG  t.o.c.

Reading the RTC in the nuElectronics Datalogging Shield

Page's URL: Ar3Ne1Rtc.htm



This tutorial shows you how to access the shield's real time clock, its DS1302. Don't be scared by the "DS"... yes, this is a chip from Dallas's 1-wire family, but you don't have to deal with the usual 1-Wire programming things. You DON'T need the general Arduino "OneWire" library.

N.B. This guide is a little crude for the moment. But the code at the bottom WORKS... once you have installed the "sensor_pff" library which came from nuElectronics on your system. At 3/13, the nuElectronics site had been unreachable for some time. I have put a copy of the "sensor_pff" library, as a .zip file, on my site for you to download. I didn't look at it very closely, if you see things in it I should not be distributing, please let me know. (I've no doubt the contents are safe and decent... but there may be copyright issues?) (I've done notes on how to add libraries, if you are unfamiliar with that. Don't worry, it isn't hard, (I did it!) and won't "mess up" your Arduino development environment. ). If there are elements of this guide which are hard to follow, or seem wrong(!), do please contact the author and complain!

A detail: You will see "one wire" and "one wire interface" in the nuElectronics documentation. This is not always connected with "1-Wire" (a Dallas trademark) at all, and even when you are connecting a 1-Wire device to a nuElectronics "one wire" connection point, you won't often (ever?) get dragged into some of the more complex 1-Wire issues. Don't get me wrong... I like 1-Wire... it is powerful. But to get everything you can from 1-Wire gets dangerously close to Serious Work. The nuelectonics datalogging shield lets you use some of the (marvelous) 1-Wire devices, e.g. the DS18B20 temperature sensor, without the work! (I've written a brief note about the use of the terms, if you want to read more.)

The nice people at nuElectronics have given you various libraries you can "#include" in your code, which make reading the RTC chip REALLY easy... compared to what you'd be doing if it was be accessed via 1-Wire protocols.

Don't worry too much about the "libraries" thing. As I mentioned earlier, I've written a separate guide to Arduino software library use for you. The nuElectronics datalogging shield doesn't need libraries in general, but some of the things you may want to use on it, or through it, do require libraries, the RTC being one of them.

In this "How To", we'll do a program which reads (and displays) the date and time (and day of week) information which is inside the RTC chip in the datalogging shield. You may find that it was set to the correct date and time before it was shipped to you, but we will do a "set the clock" program in an associated "How To", just in case.

A word about "day of week": This is information from the RTC chip to tell you if it is Monday, Tuesday, Wednesday, etc. I believe it will always be one of 1,2,3,4,5,6 and 7 (but it might be 0-6). It is "arbitrary"... You can call Thursday "1", if you want to! The RTC chip just, every midnight, adds 1 to the value it returns for DOW, going back to 1 after 7 (or is it back to 0 after 6? One or the other. Simples, anyway.)

Have a little skim through the accompanying program, put it in your Arduino, start it running, and then open your serial monitor. You should get lines like....

6-7-2010, Day of week: 2. Time is 0:19:30
6-7-2010, Day of week: 2. Time is 0:19:31
6-7-2010, Day of week: 2. Time is 0:19:32
6-7-2010, Day of week: 2. Time is 0:19:33

... although the date or time may be wrong. But the time should go up, in real time. (And the date, if you watch long enough!)

I may be misusing the terms, but bear with me?

When you execute....

ds1302.gettime(&rtc);

... you cause a record named "rtc" to be filled with data.

Once you have done that, elements built into the record can be accessed as illustrated in the demo program, e.g. iDOW=rtc.wday fills the variable "iDOW" with something taken from he wday element of the rtc record.

Note that the names of the elements of the record are determined inside one of the files you #included. You can't (easily) change the ".wday" part. You can store the value from rtc.wday in whatever integer variable suits you. You can even change the "rtc" part of the name. Early in the program, the line....

RTC rtc;

... said "We're going to have a record (fancy variable, with elements) ("record" may not be quite correct for C, and "elements" is used in the general sense) called "rtc". Before you read the next sentence, bring to mind that in Arduino-speak "rtc" and "RTC" are DIFFERENT things. "rtc" will be of type "RTC", which the compiler must know about, if this line is to be compiled. Happily, the compiler was told about data type "RTC" in one of the #included files.

(Sorry... remember: This guide is in draft form, AND the code DOES WORK!)

We could have used a different name for the record, e.g., we could have had....

RTC MyRtcRecord

... in which case, later in the program we would have said...

iYr=MyRtcRecord.year;

... to fill our variable iYr from the "year" element of the type RTC record, which would have been filled with a call to....

ds1302.gettime(&MyRtcRecord);

Where is the real time clock chip?

Something you might have wondered about: How does the software know where to find the DS1302? How, where is it connected into the circuits? (You can have extended answers to the latter question at my guide to the signal line usage.) The software library we are using by having...

#include <DS1302.h>

...at the start of our code was written ASSUMING that the RTC chip is connected to the Arduino via digital lines 3,4 and 6. This will be the case if you have plugged the shield into, say, a Diecimila... but be sure to connect it that way if you are, for instance, using a ModernDevice RBBB. There is no easy way to re-assign the interface. The shield will also need connecting to the RBB (or other clone) 5v and 0v lines for the RTC to work. The shield has the rest of the things the RTC needs, as part of the shield... including a battery to keep certain circuits alive when the Arduino is dead. You don't have to re-set the RTC each time the datalogging shield is powered up. Whew.

Note also, if you know a bit about 1-Wire chips, that you don't need to know the DS1302 chip's unique 1-Wire ID.

/*FirstRTC
vers: 6 July 2010

Simple, simple test of access to DS1302
in nuelectonics.com datalog shield*/

#include <DS1302.h>
#include <mmc.h>
#include <RTC_define.h>

DS1302 ds1302;
RTC rtc;

void setup() {
Serial.begin(9600);
//9600 to match the data rate being used by the
//serial monitor on my system, which is set to
//the Arduino default. (Sample code published
//by nuElectronics used a faster baud rate.)

Serial.println("Welcome to RTC reader....");
}

void loop() {

int iYr,iMth,iDay,iDOW,iHr,iMin,iSec;

ds1302.gettime(&rtc);//As usual: the case of letters matters.

iYr=rtc.year;//Must be put into "int" type variable... byte won't do.
iMth=rtc.month;//The "rtc" part is chosen by the programmer of this code.
iDay=rtc.mday;//Day of month, e.g. Christmas's is "25"
iDOW=rtc.wday;//Day of week... 0,1,2...6. Or is it 1,2,3...7?
iHr=rtc.hour;//What comes after the "." is defined within the
iMin=rtc.min;//code included with #include, above. Not easily changed.
iSec=rtc.sec;

//Yes... what is in next block IS done crudely. I know.
//The point is to show you where things are... not to be
//clever with printing to the serial monitor!
Serial.print(iDay);Serial.print("-");//Print Jan 2nd, 2010 as 2-1-2010
Serial.print(iMth);Serial.print("-");
Serial.print(iYr);Serial.print(", Day of week: ");
Serial.print(iDOW);Serial.print(". Time is ");

Serial.print(iHr);Serial.print(":");//Then the time
Serial.print(iMin);Serial.print(":");
Serial.print(iSec);
Serial.println();//and start new line


delay(1002);//Will print the time ALMOST once per second. Once
//in a while, the DISPLAY will, skip, leave out a second, i.e.
//jump, say, from 12:13:14 to 12:13:16... but the time reported
//will always be correct.

}


"Time" to go....

I hope that gave you what you came here for? If it did, please use the "StumbleUpon" button at the top of the page? Let me know if there are bits needing work. Contact details below.


My tutorials about programming for the Dallas Semiconductor 1-Wire (tm) chips, as used on a MicroLan (tm), have been moved to here.. Those tutorials are written for Delphi (language) programmers, but they contain much information that would apply to other language environments. I also maintain pages which introduce MicroLans and explain the hardware.


-------------------

See Also: The Arduino programming course from Sheepdog Guides:

Further to the Arduino ideas the page you are reading now will take you to, I have posted a series of essays which try to help you become a better Arduino programmer and engineer... but, for the best result, you will have to buckle down and work your way through them in sequence. The "How To's" here can be accessed in whatever order you like.


Feel free to use this information in programming courses, etc, but a credit of the source would be appreciated. If you simply copy the pages to other web pages you will do your readers a disservice: Your copies won't stay current. Far better to link to these pages, and then your readers see up-to-date versions. For those who care- thank you- I have posted a page with more information on what copyright waivers I extend, and suggestions for those who wish to put this material on CDs, etc.






Editorial Philosophy

See the discussion near the bottom of the "top level" page covering the bulk of my Arduino contributions. There is information there, too, about things like "May I copy your material?", and the system of file names I am trying to work to.


   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the information about the nuElectronics data shield of which this page is part, I have other sites with material you might find useful.....

Tutorials about the free database which is part of the free Open Office.
Sequenced set of tutorials on Pascal programming and electronics interfacing.
Some pages for programmers.
Using the parallel port of a Windows computer.

If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service... although I was less than pleased the other day to have what I was doing interrupted by a telephone call from their sales team, trying to get me to extend my involvement. Sigh. Hardly a rare event, but I'd thought 1&1 were a bit classier that some of the people who have my telephone number.



Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest site.

Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?Frmar3ne1rtc" I added to that link causes your browser problems. Please let me know, if so?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.



To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite the page's URL, "ar3ne1rtc.htm".

Click to check for W3.org HTML validity tester Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Click to check CSS validity


One last bit of advice: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .