DS18x20 on Arduino

With a little effort, anything is possible. This was something I threw together in my spare time. Thanks to the brilliant people at the Arduino forums, the code I started with had all the bugs hammered out and the dallas one-wire code turned into a library. Thanks again Jim for putting together a library for one-wire interfacing. There is a page on arduino playground here. Word is that this all works fine and well. Now if I could just find the time…
I’ll leave the old code and notes below, and I’ll update when I get a chance to test out the new code. . . . . . Note that the temperature sensor I have doesn’t seem to do the conversion, and instead I’m getting back all the default values. However, write/read scratchpad does work. This is NOT adapted for “parasite power” mode. Also, this code is designed to work with only one slave, though at this point addressing the ROM wouldn’t be hard, even having the serial interface be a carrier for requests from a PC. Sorry, indentation isn’t working, at least there’s Ctrl+T in Arduino. Thanks to “JimS” for corrections, and advising that what chip I have may only do “parasite” power mode. I’ll have to do a little research now. Changes in code have been indicated in red.

/* DS18S20 Temperature chip i/o
 * ---------------
 *
 * See http://pdfserv.maxim-ic.com/en/ds/DS1820-DS1820S.pdf for the datasheet.
 *
 * (copyleft) 2006
 *                                 *** this code is obsolete ***
 *
 */
// variable declaration
int onepinio = 7; // i/o
int pres; // just a temporary variable to store return data  

void ds_reset(void) {
  pinMode(onepinio, OUTPUT);  // Pull low for 500mcs
  digitalWrite(onepinio,0);
  delayMicroseconds(500);
  pinMode(onepinio, INPUT);   // Release for at least 500mcs
  pres=0;
  delayMicroseconds(500);
  while (pres == 0) {
    pres=digitalRead(onepinio);
  }
}
void ds_write1(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(5);        // A "1" is when the line is pulled
  pinMode(onepinio, INPUT);    //     1mcs < t < 15mcs
  delayMicroseconds(45);       // so the write slots are even
}

void ds_write0(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(20);        // A "0" is when t > 15mcs (30 typical)
  pinMode(onepinio, INPUT);
  delayMicroseconds(30);        // evening up again...
}

int ds_readbit(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(1);          // A "read slot" is when 1mcs > t > 2mcs
  pinMode(onepinio, INPUT);
  delayMicroseconds(2);          // Wait just a bit
  return(digitalRead(onepinio)); // return what we see on the line
}

void ds_writebyte(byte dsbyte) {
  byte mask;                            // How heavy is this bit?
  int cwt;
  cwt=0;
  for (mask = 0x01; mask; mask <<= 1) {  // Thanks to mellis - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1147888882
    if (mask & dsbyte) {                 // Or whoever they got it from...
      ds_write1();
    }
    else {
      ds_write0();
    }
  }
}

int ds_readbyte(void) {
  byte mask;
  int cwt;
  cwt=0;
  for (mask = 0x01; mask; mask <<= 1) {  // Thanks again.
    cwt = cwt + mask * ds_readbit();
  }
  return(cwt);
}

void setup(void) {
  // initialize inputs/outputs
  // start serial port
  pinMode(onepinio,INPUT);
  digitalWrite(onepinio,1);
  Serial.begin(9600);
}

void loop(void) {
  ds_writebyte(0xCC);         // Skip ROM
  ds_writebyte(0x44);         // Supposed to work, but doesn't for me.
  pres=0;
  pinMode(onepinio, OUTPUT);  // Maybe I was going to try parasite power?
  digitalWrite(onepinio, 1);
  delay(1000);
  digitalWrite(onepinio, 0);  // In thst case, be sure to turn it back to 0
  pinMode(onepinio, INPUT);   // to turn off the internal pull-up resistor.
  //while (pres = 0) {          //   The chip's supposed to return a 0 until the
  //  pres=ds_readbit();        // conversion's done (can't do in parasite mode)
  //}
  ds_reset();
  ds_writebyte(0xCC);         // Skip ROM
  ds_writebyte(0xBE);         // Read Scratchpad
  int cby;                    // Call me lazy, or creative: "current byte"
  cby=0;
  while (cby <=8) {           // we need 9 bytes
    Serial.print(ds_readbyte(), HEX);
    Serial.print(" ");
    cby=cby+1;
  }
  Serial.println();
  delay(100);                 // Lets not flood.
}