1 wire/RTC iButton

Oddly enough, I found your post when searching for tips on how to get started with my DS1904. :slight_smile: After a really boneheaded reading of the datasheet, I figured out what I was doing wrong and everything clicked into place. Derived from the sample at the Playground.

From your code and output, everything is working just fine. The output you get with the ** TESTING ** area commented out is all kosher as well.

Excuse the demo code- wanted to make it clear for future readers (and future-me) :slight_smile: :

#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
  ds.write(0xA0);
  ds.write(0x02);
  ds.write(0x03);
  ds.write(0x05);
  ds.write(0x08);
  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();
}

Uncomment the // write section if you want to set an initial time, or to start the RTC. Out of the box, my RTC had a control byte of 0x50 and all four time bytes were 0x55. To get use out of it, you need to turn on the oscillator- if you don't, it's essentially a 4-byte battery backed RAM store. :slight_smile:

Control Byte (see DS1904 Datasheet)
          OSC1 OSC2
          Us \ /
byte     /--\ ||xx      
0x50     0101 0000 = user bits are 0101; OSC1 and OSC2 are 0 => clock not running
0xA0     1010 0000 = user bits are 1010; OSC1 and OSC2 are 0 => clock not running
0xAC     1010 1100 = user bits are 1010; OSC1 and OSC2 are 1 => clock running

Example Output:
ROM: 24 C7 7B 33 0 0 0 DC PR: 1 CTRL BYTE:  10101100
      time:  A0 4 5 8

Hopefully that'll get you started! :slight_smile:

Regards,
Aaron