1Wire Real Time Clock With Arduino

Posted by Ril3y on January 20, 2010

So I ordered a 1-Wire RTC (real time clock) from the maxim corporation.  To be exact its a ds1904.  What is really cool about the 1-Wire protocol is it only needs 1 wire to send data on.  It is a bit misleading however since it does need a ground as well.  You really need to read the data sheet on these guys to understand fully how they work.  But in the mean time basically one button gets to talk at a time.  Every button has a unique serial number which is kind of a cool feature in it self.  Since its in a steel “can” its really rugged so you could place it on say a card, keychain etc?  It would be cool to use as a key FOB to get into my workshop.  However no time for a new project at the moment.  The reason I am messing with this stuff is I had to take a break from the project I have been working on for work.  Call it a mind break.

I created a DIY (with a laser cutter that is) OneWire button mount as you can see it just holds 2 wires against the top and bottom of the steel can.  Pretty simple.  Not really useful as an end solution but good for testing.

DSC_5387.JPG



Anyhow there is a nice Arduino lib for the 1-wire stuff here.  Just extract this to your arduinoxxx/hardware/lib/OneWire folder.


This is the source code that I used and edited to get the RTC DS1904 working.  Note that you need to uncomment the //write Section upload it to the board let it run then re-comment it and upload it again.  Otherwise the oscillators will not turn on and it does not “count seconds” or more to the point at as a normal clock.

This is the source code that I used and edited to get the RTC DS1904 working.  Note that you need to uncomment the //write Section upload it to the board let it run then re-comment it and upload it again.  Otherwise the oscillators will not turn on and it does not “count seconds” or more to the point at as a normal clock.


#include 
 
// DS1904 Real Time Clock iButton I/O
OneWire ds(10);  // on pin 10
 
void setup(void) {
  Serial.begin(9600);
}
 
void loop(void) {
  byte i;
  byte present = 0;
  byte data[8];
  byte addr[8];
 
  if ( !ds.search(addr)) {
      Serial.print("No more addresses found.\n");
      ds.reset_search();
      delay(500);  // for readability
      return;
  }
 
  Serial.print("ROM: ");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }
 
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }
 
  if ( addr[0] != 0x24) {
      Serial.print("\t\tDevice is not a DS1904 family device.\n");
      return;
  }
 
/*
  // write!
  Serial.println("writing to RTC...");
  present = ds.reset();
  ds.select(addr);
  ds.write(0x99,1);   // write RTC - this is the write code
  ds.write(0xAC);  //This is the control byte.  AC in hex = 10101100
                   //read the datasheet and you will see that this is important
                   //to start the internal osc's... Or to make the clock start
                   //counting seconds.  --ril3y
  ds.write(0x02);  //0x02 is a random time set it with your own
  ds.write(0x03);  //same with this
  ds.write(0x05);  //this
  ds.write(0x08);  //and this
  present = ds.reset();
  delay(1500);     // unknown if wait needed
*/
 
  // read!
  present = ds.reset();
  ds.select(addr);
  ds.write(0x66,1);   // read RTC
 
  Serial.print("PR: ");
  Serial.print(present, HEX);
  for ( i = 0; i < 5; i++) {
    data[i] = ds.read();
  }
  Serial.print(" CTRL BYTE:  ");
  Serial.print(data[0], BIN);
  Serial.print("\n\ttime:  ");
  for ( i = 1; i < 5; i++) {
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
 
  Serial.println();
 
}

Note that I did not write most of this code. You can see the original thread here:  This should be enough to get you starting using a 1Wire RTC.  Enjoy!

Google Buzz

Tags: , , , , ,

Incoming links

Do you have anything to say?

Powered by Wordpress and Stripes Theme Entries (RSS) | Comments (RSS)

Bad Behavior has blocked 104 access attempts in the last 7 days.