// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas
temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress termo[5];
// Assign address manually. The addresses below will beed to be changed
// to valid device addresses on your bus. Device address can be retrieved
// by using either [Link](deviceAddress) or individually via
// [Link](deviceAddress, index)
// DeviceAddress insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// DeviceAddress outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
void setup(void)
// start serial port
[Link](9600);
[Link]("Dallas Temperature IC Control Library Demo");
// Start up the library
[Link]();
// locate devices on the bus
[Link]("Locating devices...");
[Link]("Found ");
[Link]([Link](), DEC);
[Link](" devices.");
// report parasite power requirements
[Link]("Parasite power is: ");
if ([Link]()) [Link]("ON");
else [Link]("OFF");
// Search for devices on the bus and assign based on an index. Ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
if () [Link]("Unable to find address for Device 0");
if () [Link]("Unable to find address for Device 1");
if () [Link]("Unable to find address for Device 2");
if () [Link]("Unable to find address for Device 3");
if () [Link]("Unable to find address for Device 4");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if () [Link]("Unable to find address for
insideThermometer");
// assigns the seconds address found to outsideThermometer
//if () [Link]("Unable to find address for
outsideThermometer");
// show the addresses we found on the bus
for(int i = 0; i < 5; i++){
[Link]("Device");
[Link](i);
[Link]("Address: ");
printAddress(termo[i]);
[Link]();
// set the resolution to 9 bit per device
for(int i = 0; i < 5; i++){
[Link](termo[i], TEMPERATURE_PRECISION);
for(int i = 0; i < 5; i++){
[Link]("Device");
[Link](i);
[Link]("Resolution: ");
[Link]([Link](termo[i]), DEC);
[Link]();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
for (uint8_t i = 0; i < 8; i++)
// zero pad the address if necessary
if (deviceAddress[i] < 16) [Link]("0");
[Link](deviceAddress[i], HEX);
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
float tempC = [Link](deviceAddress);
[Link]("Temp C: ");
[Link](tempC);
[Link](" Temp F: ");
[Link](DallasTemperature::toFahrenheit(tempC));
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
[Link]("Resolution: ");
[Link]([Link](deviceAddress));
[Link]();
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
[Link]("Device Address: ");
printAddress(deviceAddress);
[Link](" ");
printTemperature(deviceAddress);
[Link]();
/*
Main function, calls the temperatures in a loop.
*/
void loop(void)
// call [Link]() to issue a global temperature
// request to all devices on the bus
[Link]("Requesting temperatures...");
[Link]();
[Link]("DONE");
// print the device information
for(int i = 0; i < 5; i++){
printData(termo[i]);
delay(500);
analogWrite(50);