DS18b20 Working Example?

I stripped this code down to the code below to get a copy-able format of the address, without caring to take temperatures.
It will get the ROM of all the chips on the wire, so I suggest wiring one sensor at a time to get its ROM. It may take up to 3 seconds to show in serial because I wanted it to go slow thus allowing me to copy the address.
You´ll need to include only the onewire library from the IDE. Note I´m not using dallas library here.
Output:

Code:

// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"



OneWire ds = OneWire(D3);  // on pin 10 (a 4.7K resistor is necessary)
unsigned long lastUpdate = 0; 
void setup() {
  Serial.begin(9600);
}
 
void loop() {

 unsigned long now = millis();
    if((now - lastUpdate) > 3000)
    {
        lastUpdate = now;
        byte i;
        byte present = 0;
        byte addr[8];
 
      if ( !ds.search(addr)) {
        Serial.println("No more addresses.");
        Serial.println();
        ds.reset_search();
        //delay(250);
        return;
      }
            // the first ROM byte indicates which chip
      switch (addr[0]) {
        case 0x10:
          Serial.println("Chip = DS18S20");  // or old DS1820
          break;
        case 0x28:
          Serial.println("Chip = DS18B20");
          break;
        case 0x22:
          Serial.println("Chip = DS1822");
          break;
        default:
          Serial.println("Device is not a DS18x20 family device.");
          return;
      }
      
     
      Serial.print("ROM = ");
      Serial.print("0x");
        Serial.print(addr[0],HEX);
      for( i = 1; i < 8; i++) {
        Serial.print(", 0x");
        Serial.print(addr[i],HEX);
      }
     
      if (OneWire::crc8(addr, 7) != addr[7]) {
          Serial.println("CRC is not valid!");
          return;
      }
      
     
    Serial.println();
      ds.reset();
        
    }
}
1 Like