#include OneWire ds(4); //D2 GPIO4 DS18B20 IN OUT byte temp0[8] = { 0x28, 0x48, 0x90, 0x7A, 0x12, 0x21, 0x01, 0x90 }; byte temp1[8] = { 0x28, 0x58, 0xA7, 0x57, 0x12, 0x21, 0x01, 0xF3 }; byte temp2[8] = { 0x28, 0xFF, 0x64, 0x02, 0x19, 0x2E, 0x3E, 0x39 }; void setup() { Serial.begin(115200); while (!Serial) { } delay(1000); } void loop(void){ Serial.println("Temp Start"); reDS18B20(); //温度取得要求 delay(1000); Serial.println(getDS18B20C(temp0)); Serial.println(getDS18B20C(temp1)); Serial.println(getDS18B20C(temp2)); delay(1000); } void reDS18B20(void){ ds.reset(); ds.write(0xCC, 1); ds.write(0x44, 1); } float getDS18B20C(byte *addr){ byte i; byte present = 0; byte type_s; byte data[12]; float celsius; present = ds.reset(); ds.select(addr); ds.write(0xBE); 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; if (data[7] == 0x10) { raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); 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 } celsius = (float)raw / 16.0; return celsius; }