2 #include <DallasTemperature.h>
4 // Data wire is plugged into port 2 on the Arduino
6 #define TEMPERATURE_PRECISION 9
8 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9 OneWire oneWire(ONE_WIRE_BUS);
11 // Pass our oneWire reference to Dallas Temperature.
12 DallasTemperature sensors(&oneWire);
14 // arrays to hold device addresses
15 DeviceAddress insideThermometer, outsideThermometer;
21 Serial.println("Dallas Temperature IC Control Library Demo");
23 // Start up the library
26 // locate devices on the bus
27 Serial.print("Locating devices...");
28 Serial.print("Found ");
29 Serial.print(sensors.getDeviceCount(), DEC);
30 Serial.println(" devices.");
32 // report parasite power requirements
33 Serial.print("Parasite power is: ");
34 if (sensors.isParasitePowerMode()) Serial.println("ON");
35 else Serial.println("OFF");
37 // assign address manually. the addresses below will beed to be changed
38 // to valid device addresses on your bus. device address can be retrieved
39 // by using either oneWire.search(deviceAddress) or individually via
40 // sensors.getAddress(deviceAddress, index)
41 //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
42 //outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
44 // search for devices on the bus and assign based on an index. ideally,
45 // you would do this to initially discover addresses on the bus and then
46 // use those addresses and manually assign them (see above) once you know
47 // the devices on your bus (and assuming they don't change).
50 if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
51 if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
54 // search() looks for the next device. Returns 1 if a new address has been
55 // returned. A zero might mean that the bus is shorted, there are no devices,
56 // or you have already retrieved all of them. It might be a good idea to
57 // check the CRC to make sure you didn't get garbage. The order is
58 // deterministic. You will always get the same devices in the same order
60 // Must be called before search()
61 //oneWire.reset_search();
62 // assigns the first address found to insideThermometer
63 //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
64 // assigns the seconds address found to outsideThermometer
65 //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
67 // show the addresses we found on the bus
68 Serial.print("Device 0 Address: ");
69 printAddress(insideThermometer);
72 Serial.print("Device 1 Address: ");
73 printAddress(outsideThermometer);
76 // set the resolution to 9 bit
77 sensors.setResolution(insideThermometer, 9);
78 sensors.setResolution(outsideThermometer, 9);
80 Serial.print("Device 0 Resolution: ");
81 Serial.print(sensors.getResolution(insideThermometer), DEC);
84 Serial.print("Device 1 Resolution: ");
85 Serial.print(sensors.getResolution(outsideThermometer), DEC);
89 // function to print a device address
90 void printAddress(DeviceAddress deviceAddress)
92 for (uint8_t i = 0; i < 8; i++)
94 // zero pad the address if necessary
95 if (deviceAddress[i] < 16) Serial.print("0");
96 Serial.print(deviceAddress[i], HEX);
100 // function to print the temperature for a device
101 void printTemperature(DeviceAddress deviceAddress)
103 float tempC = sensors.getTempC(deviceAddress);
104 Serial.print("Temp C: ");
106 Serial.print(" Temp F: ");
107 Serial.print(DallasTemperature::toFahrenheit(tempC));
110 // function to print a device's resolution
111 void printResolution(DeviceAddress deviceAddress)
113 Serial.print("Resolution: ");
114 Serial.print(sensors.getResolution(deviceAddress));
118 // main function to print information about a device
119 void printData(DeviceAddress deviceAddress)
121 Serial.print("Device Address: ");
122 printAddress(deviceAddress);
124 printTemperature(deviceAddress);
130 // call sensors.requestTemperatures() to issue a global temperature
131 // request to all devices on the bus
132 Serial.print("Requesting temperatures...");
133 sensors.requestTemperatures();
134 Serial.println("DONE");
136 // print the device information
137 printData(insideThermometer);
138 printData(outsideThermometer);