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 int numberOfDevices; // Number of temperature devices found
16 DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
22 Serial.println("Dallas Temperature IC Control Library Demo");
24 // Start up the library
27 // Grab a count of devices on the wire
28 numberOfDevices = sensors.getDeviceCount();
30 // locate devices on the bus
31 Serial.print("Locating devices...");
33 Serial.print("Found ");
34 Serial.print(numberOfDevices, DEC);
35 Serial.println(" devices.");
37 // report parasite power requirements
38 Serial.print("Parasite power is: ");
39 if (sensors.isParasitePowerMode()) Serial.println("ON");
40 else Serial.println("OFF");
42 // Loop through each device, print out address
43 for(int i=0;i<numberOfDevices; i++)
45 // Search the wire for address
46 if(sensors.getAddress(tempDeviceAddress, i))
48 Serial.print("Found device ");
50 Serial.print(" with address: ");
51 printAddress(tempDeviceAddress);
54 Serial.print("Setting resolution to ");
55 Serial.println(TEMPERATURE_PRECISION,DEC);
57 // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
58 sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
60 Serial.print("Resolution actually set to: ");
61 Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
64 Serial.print("Found ghost device at ");
66 Serial.print(" but could not detect address. Check power and cabling");
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");
98 // Loop through each device, print out temperature data
99 for(int i=0;i<numberOfDevices; i++)
101 // Search the wire for address
102 if(sensors.getAddress(tempDeviceAddress, i))
104 // Output the device ID
105 Serial.print("Temperature for device: ");
106 Serial.println(i,DEC);
108 // It responds almost immediately. Let's print out the data
109 printTemperature(tempDeviceAddress); // Use a simple function to print out the data
111 //else ghost device! Check your power requirements and cabling
116 // function to print a device address
117 void printAddress(DeviceAddress deviceAddress)
119 for (uint8_t i = 0; i < 8; i++)
121 if (deviceAddress[i] < 16) Serial.print("0");
122 Serial.print(deviceAddress[i], HEX);