1 wire/RTC iButton

Great stuff guys this got me on the right track for getting this i button stuff working. One thing that I am hesitant to point out as it got me to really sit down and spend some time reading the data sheet. But the code posted below will only read the clock (after you uncomment it and write the "time" to it) and write to it. To turn the clock on you need to change the code in the "write" section so that it reads like this:

/*
  // 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
*/

So to repost my entire sketch that will start the clock and then read too..

#include <OneWire.h>

// 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();
  
}

I will say you get a lot of of reading the datasheet. Read it cover to cover and you will really get a better grasp of what is going on.

Ril3y
blog.synthetos.com