Single.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Include the libraries we need
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. // Data wire is plugged into port 2 on the Arduino
  5. #define ONE_WIRE_BUS 2
  6. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  7. OneWire oneWire(ONE_WIRE_BUS);
  8. // Pass our oneWire reference to Dallas Temperature.
  9. DallasTemperature sensors(&oneWire);
  10. // arrays to hold device address
  11. DeviceAddress insideThermometer;
  12. /*
  13. * Setup function. Here we do the basics
  14. */
  15. void setup(void)
  16. {
  17. // start serial port
  18. Serial.begin(9600);
  19. Serial.println("Dallas Temperature IC Control Library Demo");
  20. // locate devices on the bus
  21. Serial.print("Locating devices...");
  22. sensors.begin();
  23. Serial.print("Found ");
  24. Serial.print(sensors.getDeviceCount(), DEC);
  25. Serial.println(" devices.");
  26. // report parasite power requirements
  27. Serial.print("Parasite power is: ");
  28. if (sensors.isParasitePowerMode()) Serial.println("ON");
  29. else Serial.println("OFF");
  30. // Assign address manually. The addresses below will beed to be changed
  31. // to valid device addresses on your bus. Device address can be retrieved
  32. // by using either oneWire.search(deviceAddress) or individually via
  33. // sensors.getAddress(deviceAddress, index)
  34. // Note that you will need to use your specific address here
  35. //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
  36. // Method 1:
  37. // Search for devices on the bus and assign based on an index. Ideally,
  38. // you would do this to initially discover addresses on the bus and then
  39. // use those addresses and manually assign them (see above) once you know
  40. // the devices on your bus (and assuming they don't change).
  41. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  42. // method 2: search()
  43. // search() looks for the next device. Returns 1 if a new address has been
  44. // returned. A zero might mean that the bus is shorted, there are no devices,
  45. // or you have already retrieved all of them. It might be a good idea to
  46. // check the CRC to make sure you didn't get garbage. The order is
  47. // deterministic. You will always get the same devices in the same order
  48. //
  49. // Must be called before search()
  50. //oneWire.reset_search();
  51. // assigns the first address found to insideThermometer
  52. //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
  53. // show the addresses we found on the bus
  54. Serial.print("Device 0 Address: ");
  55. printAddress(insideThermometer);
  56. Serial.println();
  57. // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  58. sensors.setResolution(insideThermometer, 9);
  59. Serial.print("Device 0 Resolution: ");
  60. Serial.print(sensors.getResolution(insideThermometer), DEC);
  61. Serial.println();
  62. }
  63. // function to print the temperature for a device
  64. void printTemperature(DeviceAddress deviceAddress)
  65. {
  66. // method 1 - slower
  67. //Serial.print("Temp C: ");
  68. //Serial.print(sensors.getTempC(deviceAddress));
  69. //Serial.print(" Temp F: ");
  70. //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  71. // method 2 - faster
  72. float tempC = sensors.getTempC(deviceAddress);
  73. if(tempC == DEVICE_DISCONNECTED_C)
  74. {
  75. Serial.println("Error: Could not read temperature data");
  76. return;
  77. }
  78. Serial.print("Temp C: ");
  79. Serial.print(tempC);
  80. Serial.print(" Temp F: ");
  81. Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  82. }
  83. /*
  84. * Main function. It will request the tempC from the sensors and display on Serial.
  85. */
  86. void loop(void)
  87. {
  88. // call sensors.requestTemperatures() to issue a global temperature
  89. // request to all devices on the bus
  90. Serial.print("Requesting temperatures...");
  91. sensors.requestTemperatures(); // Send the command to get temperatures
  92. Serial.println("DONE");
  93. // It responds almost immediately. Let's print out the data
  94. printTemperature(insideThermometer); // Use a simple function to print out the data
  95. }
  96. // function to print a device address
  97. void printAddress(DeviceAddress deviceAddress)
  98. {
  99. for (uint8_t i = 0; i < 8; i++)
  100. {
  101. if (deviceAddress[i] < 16) Serial.print("0");
  102. Serial.print(deviceAddress[i], HEX);
  103. }
  104. }