mikroavr

ds18b20

May 2nd, 2021
1,008
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Include the libraries we need
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5. // Data wire is plugged into port 2 on the Arduino
  6. #define ONE_WIRE_BUS 26
  7. #define pin_ctrl 12
  8.  
  9. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  10. OneWire oneWire(ONE_WIRE_BUS);
  11.  
  12. // Pass our oneWire reference to Dallas Temperature.
  13. DallasTemperature sensors(&oneWire);
  14.  
  15. /*
  16.  * The setup function. We only start the sensors here
  17.  */
  18. void setup(void)
  19. {
  20.   // start serial port
  21.   pinMode(pin_ctrl, OUTPUT);
  22.   Serial.begin(115200);
  23.   Serial.println("Dallas Temperature IC Control Library Demo");
  24.   digitalWrite(pin_ctrl, HIGH);
  25.   delay(1000);
  26.   // Start up the library
  27.   sensors.begin();
  28. }
  29.  
  30. /*
  31.  * Main function, get and show the temperature
  32.  */
  33. void loop(void)
  34. {
  35.   // call sensors.requestTemperatures() to issue a global temperature
  36.   // request to all devices on the bus
  37.   Serial.print("Requesting temperatures...");
  38.   sensors.requestTemperatures(); // Send the command to get temperatures
  39.   Serial.println("DONE");
  40.   // After we got the temperatures, we can print them here.
  41.   // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  42.   float tempC = sensors.getTempCByIndex(0);
  43.  
  44.   // Check if reading was successful
  45.   if(tempC != DEVICE_DISCONNECTED_C)
  46.   {
  47.     Serial.print("Temperature for the device 1 (index 0) is: ");
  48.     Serial.println(tempC);
  49.   }
  50.   else
  51.   {
  52.     Serial.println("Error: Could not read temperature data");
  53.   }
  54. }
RAW Paste Data