Advertisement
rossoreed

Original Dallas Temp Sensor

Sep 30th, 2011
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3.  
  4. // Data wire is plugged into port 2 on the Arduino
  5. #define ONE_WIRE_BUS 2
  6. #define TEMPERATURE_PRECISION 9
  7.  
  8. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  9. OneWire oneWire(ONE_WIRE_BUS);
  10.  
  11. // Pass our oneWire reference to Dallas Temperature.
  12. DallasTemperature sensors(&oneWire);
  13.  
  14. // arrays to hold device addresses
  15. uint8_t insideThermometer[8], outsideThermometer[8];
  16.  
  17. void setup(void)
  18. {
  19.   // start serial port
  20.   Serial.begin(9600);
  21.   Serial.println("Dallas Temperature IC Control Library Demo");
  22.  
  23.   // Start up the library
  24.   sensors.begin();
  25.  
  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.");
  31.  
  32.   // report parasite power requirements
  33.   Serial.print("Parasite power is: ");
  34.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  35.   else Serial.println("OFF");
  36.  
  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 sensors.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 };
  43.  
  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).
  48.   if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  49.   if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
  50.  
  51.   // show the addresses we found on the bus
  52.   Serial.print("Device 0 Address: ");
  53.   printAddress(insideThermometer);
  54.   Serial.println();
  55.  
  56.   Serial.print("Device 1 Address: ");
  57.   printAddress(outsideThermometer);
  58.   Serial.println();
  59.  
  60.   // set the resolution to 9 bit
  61.   sensors.setResolution(insideThermometer, 9);
  62.   sensors.setResolution(outsideThermometer, 9);
  63.  
  64.   Serial.print("Device 0 Resolution: ");
  65.   Serial.print(sensors.getResolution(insideThermometer), DEC);
  66.   Serial.println();
  67.  
  68.   Serial.print("Device 1 Resolution: ");
  69.   Serial.print(sensors.getResolution(outsideThermometer), DEC);
  70.   Serial.println();
  71. }
  72.  
  73. // function to print a device address
  74. void printAddress(uint8_t deviceAddress[])
  75. {
  76.   for (uint8_t i = 0; i < 8; i++)
  77.   {
  78.     Serial.print(deviceAddress[i], HEX);
  79.     if (i < 7) Serial.print(" ");
  80.   }
  81. }
  82.  
  83. // function to print the temperature for a device
  84. void printTemperature(uint8_t deviceAddress[])
  85. {
  86.   // method 1 - slower
  87.   //Serial.print("Temp C: ");
  88.   //Serial.print(sensors.getTempC(deviceAddress));
  89.   //Serial.print(" Temp F: ");
  90.   //Serial.print(sensors.getTempF(deviceAddress));
  91.  
  92.   // method 2 - faster
  93.   float tempC = sensors.getTempC(deviceAddress);
  94.   Serial.print("Temp C: ");
  95.   Serial.print(tempC);
  96.   Serial.print(" Temp F: ");
  97.   Serial.print(DallasTemperature::toFahrenheit(tempC));
  98. }
  99.  
  100. // function to print a device's resolution
  101. void printResolution(uint8_t deviceAddress[])
  102. {
  103.   Serial.print("Resolution: ");
  104.   Serial.print(sensors.getResolution(deviceAddress));
  105.   Serial.println();    
  106. }
  107.  
  108. // main function to print information about a device
  109. void printData(uint8_t deviceAddress[])
  110. {
  111.   Serial.print("Device Address: ");
  112.   printAddress(deviceAddress);
  113.   Serial.print(" ");
  114.   printTemperature(deviceAddress);
  115.   Serial.println();
  116. }
  117.  
  118. void loop(void)
  119. {
  120.   // call sensors.requestTemperatures() to issue a global temperature
  121.   // request to all devices on the bus
  122.   Serial.print("Requesting temperatures...");
  123.   sensors.requestTemperatures();
  124.   Serial.println("DONE");
  125.  
  126.   // print the device information
  127.   printData(insideThermometer);
  128.   printData(outsideThermometer);
  129. }
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement