Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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
- uint8_t insideThermometer[8], outsideThermometer[8];
- void setup(void)
- {
- // start serial port
- Serial.begin(9600);
- Serial.println("Dallas Temperature IC Control Library Demo");
- // Start up the library
- sensors.begin();
- // locate devices on the bus
- Serial.print("Locating devices...");
- Serial.print("Found ");
- Serial.print(sensors.getDeviceCount(), DEC);
- Serial.println(" devices.");
- // report parasite power requirements
- Serial.print("Parasite power is: ");
- if (sensors.isParasitePowerMode()) Serial.println("ON");
- else Serial.println("OFF");
- // 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 sensors.search(deviceAddress) or individually via
- // sensors.getAddress(deviceAddress, index)
- //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
- //outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
- // 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).
- if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
- if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
- // show the addresses we found on the bus
- Serial.print("Device 0 Address: ");
- printAddress(insideThermometer);
- Serial.println();
- Serial.print("Device 1 Address: ");
- printAddress(outsideThermometer);
- Serial.println();
- // set the resolution to 9 bit
- sensors.setResolution(insideThermometer, 9);
- sensors.setResolution(outsideThermometer, 9);
- Serial.print("Device 0 Resolution: ");
- Serial.print(sensors.getResolution(insideThermometer), DEC);
- Serial.println();
- Serial.print("Device 1 Resolution: ");
- Serial.print(sensors.getResolution(outsideThermometer), DEC);
- Serial.println();
- }
- // function to print a device address
- void printAddress(uint8_t deviceAddress[])
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- Serial.print(deviceAddress[i], HEX);
- if (i < 7) Serial.print(" ");
- }
- }
- // function to print the temperature for a device
- void printTemperature(uint8_t deviceAddress[])
- {
- // method 1 - slower
- //Serial.print("Temp C: ");
- //Serial.print(sensors.getTempC(deviceAddress));
- //Serial.print(" Temp F: ");
- //Serial.print(sensors.getTempF(deviceAddress));
- // method 2 - faster
- float tempC = sensors.getTempC(deviceAddress);
- Serial.print("Temp C: ");
- Serial.print(tempC);
- Serial.print(" Temp F: ");
- Serial.print(DallasTemperature::toFahrenheit(tempC));
- }
- // function to print a device's resolution
- void printResolution(uint8_t deviceAddress[])
- {
- Serial.print("Resolution: ");
- Serial.print(sensors.getResolution(deviceAddress));
- Serial.println();
- }
- // main function to print information about a device
- void printData(uint8_t deviceAddress[])
- {
- Serial.print("Device Address: ");
- printAddress(deviceAddress);
- Serial.print(" ");
- printTemperature(deviceAddress);
- Serial.println();
- }
- void loop(void)
- {
- // call sensors.requestTemperatures() to issue a global temperature
- // request to all devices on the bus
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures();
- Serial.println("DONE");
- // print the device information
- printData(insideThermometer);
- printData(outsideThermometer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement