#include OneWire ds(4); //D2 GPIO4 DS18B20 IN OUT void setup() { Serial.begin(115200); delay(1000); } void loop() { float celsius; byte data[12]; byte i; byte present = 0; byte type_s; ds.reset(); ds.write(0xCC, 1); ds.write(0x44, 1); delay(1000); ds.reset(); ds.write(0xCC, 1); ds.write(0xBE, 1); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } for ( i = 0; i < 9; i++) { // we need 9 bytes Serial.print( "0x"); Serial.print( data[i],HEX); Serial.print( " ; "); } celsius = (float)raw / 16.0; Serial.print("Temp= "); Serial.println(celsius); }