Simple.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /*
  11. * The setup function. We only start the sensors here
  12. */
  13. void setup(void)
  14. {
  15. // start serial port
  16. Serial.begin(9600);
  17. Serial.println("Dallas Temperature IC Control Library Demo");
  18. // Start up the library
  19. sensors.begin();
  20. }
  21. /*
  22. * Main function, get and show the temperature
  23. */
  24. void loop(void)
  25. {
  26. // call sensors.requestTemperatures() to issue a global temperature
  27. // request to all devices on the bus
  28. Serial.print("Requesting temperatures...");
  29. sensors.requestTemperatures(); // Send the command to get temperatures
  30. Serial.println("DONE");
  31. // After we got the temperatures, we can print them here.
  32. // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  33. float tempC = sensors.getTempCByIndex(0);
  34. // Check if reading was successful
  35. if(tempC != DEVICE_DISCONNECTED_C)
  36. {
  37. Serial.print("Temperature for the device 1 (index 0) is: ");
  38. Serial.println(tempC);
  39. }
  40. else
  41. {
  42. Serial.println("Error: Could not read temperature data");
  43. }
  44. }