2 #include <DallasTemperature.h>
4 // Data wire is plugged into port 2 on the Arduino
7 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
8 OneWire oneWire(ONE_WIRE_BUS);
10 // Pass our oneWire reference to Dallas Temperature.
11 DallasTemperature sensors(&oneWire);
13 // arrays to hold device address
14 DeviceAddress insideThermometer;
20 Serial.println("Dallas Temperature IC Control Library Demo");
22 // locate devices on the bus
23 Serial.print("Locating devices...");
25 Serial.print("Found ");
26 Serial.print(sensors.getDeviceCount(), DEC);
27 Serial.println(" devices.");
29 // report parasite power requirements
30 Serial.print("Parasite power is: ");
31 if (sensors.isParasitePowerMode()) Serial.println("ON");
32 else Serial.println("OFF");
34 // assign address manually. the addresses below will beed to be changed
35 // to valid device addresses on your bus. device address can be retrieved
36 // by using either oneWire.search(deviceAddress) or individually via
37 // sensors.getAddress(deviceAddress, index)
38 //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
41 // search for devices on the bus and assign based on an index. ideally,
42 // you would do this to initially discover addresses on the bus and then
43 // use those addresses and manually assign them (see above) once you know
44 // the devices on your bus (and assuming they don't change).
45 if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
48 // search() looks for the next device. Returns 1 if a new address has been
49 // returned. A zero might mean that the bus is shorted, there are no devices,
50 // or you have already retrieved all of them. It might be a good idea to
51 // check the CRC to make sure you didn't get garbage. The order is
52 // deterministic. You will always get the same devices in the same order
54 // Must be called before search()
55 //oneWire.reset_search();
56 // assigns the first address found to insideThermometer
57 //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
59 // show the addresses we found on the bus
60 Serial.print("Device 0 Address: ");
61 printAddress(insideThermometer);
64 // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
65 sensors.setResolution(insideThermometer, 9);
67 Serial.print("Device 0 Resolution: ");
68 Serial.print(sensors.getResolution(insideThermometer), DEC);
72 // function to print the temperature for a device
73 void printTemperature(DeviceAddress deviceAddress)
76 //Serial.print("Temp C: ");
77 //Serial.print(sensors.getTempC(deviceAddress));
78 //Serial.print(" Temp F: ");
79 //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
82 float tempC = sensors.getTempC(deviceAddress);
83 Serial.print("Temp C: ");
85 Serial.print(" Temp F: ");
86 Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
91 // call sensors.requestTemperatures() to issue a global temperature
92 // request to all devices on the bus
93 Serial.print("Requesting temperatures...");
94 sensors.requestTemperatures(); // Send the command to get temperatures
95 Serial.println("DONE");
97 // It responds almost immediately. Let's print out the data
98 printTemperature(insideThermometer); // Use a simple function to print out the data
101 // function to print a device address
102 void printAddress(DeviceAddress deviceAddress)
104 for (uint8_t i = 0; i < 8; i++)
106 if (deviceAddress[i] < 16) Serial.print("0");
107 Serial.print(deviceAddress[i], HEX);