Advertisement
NittyGritty

Dallas_18B20_Tester_1.ino

Mar 1st, 2022
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <OneWire.h>
  4. #include <MicroLCD.h>
  5. #include <DallasTemperature.h>
  6. #include <Time.h>
  7.  
  8.  
  9. const char compile_date[] =  __FILE__ " " __DATE__ " " __TIME__;
  10.  
  11. // Data wire is plugged into port 2 on the Arduino
  12. #define ONE_WIRE_BUS 10
  13. #define TEMPERATURE_PRECISION 11 // Lower resolution
  14.  
  15. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  16. OneWire oneWire(ONE_WIRE_BUS);
  17.  
  18. // Pass our oneWire reference to Dallas Temperature.
  19. DallasTemperature sensors(&oneWire);
  20.  
  21. int numberOfDevices; // Number of temperature devices found
  22.  
  23. DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
  24.  
  25. //LCD_SH1106 lcd; /* for SH1106 OLED module */
  26. LCD_SSD1306 lcd; /* for SSD1306 OLED module */
  27.  
  28.  
  29. void setup(void)
  30. {
  31.   // start serial port
  32.   Serial.begin(9600);
  33.   lcd.begin();
  34.  
  35.   Serial.println(compile_date);
  36.   lcd.print( __FILE__ );
  37.   lcd.setCursor(0, 1);                       // Anfang auf Stelle 0, Zeile 0 setzen
  38.   lcd.print( __DATE__ );
  39.  
  40.   Serial.println("Dallas Temperature IC Control Library Demo");
  41.  
  42.   // Start up the library
  43.   sensors.begin();
  44.  
  45.   // Grab a count of devices on the wire
  46.   numberOfDevices = sensors.getDeviceCount();
  47.  
  48.   // locate devices on the bus
  49.   Serial.print("Locating devices...");
  50.  
  51.   Serial.print("Found ");
  52.   Serial.print(numberOfDevices, DEC);
  53.   Serial.println(" devices.");
  54.  
  55.   // report parasite power requirements
  56.   Serial.print("Parasite power is: ");
  57.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  58.   else Serial.println("OFF");
  59.   delay(5000);
  60.  
  61.   lcd.clear();
  62.   lcd.setFontSize(FONT_SIZE_SMALL);
  63.   lcd.setCursor(0, 0);
  64.  
  65.   // Loop through each device, print out address
  66.   for (int i = 0; i < numberOfDevices; i++)
  67.   {
  68.     // Search the wire for address
  69.     if (sensors.getAddress(tempDeviceAddress, i))
  70.     {
  71.       Serial.print("Found device ");
  72.       Serial.print(i, DEC);
  73.       Serial.print(" with address: ");
  74.       printAddress(tempDeviceAddress);
  75.       Serial.println();
  76.  
  77.       Serial.print("Setting resolution to ");
  78.       Serial.println(TEMPERATURE_PRECISION, DEC);
  79.  
  80.       // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
  81.       sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
  82.  
  83.       Serial.print("Resolution actually set to: ");
  84.       Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
  85.       Serial.println();
  86.       lcd.print("Device: ");
  87.       lcd.println(i);
  88.  
  89.       lcd.println("Addr ");
  90.       lcd_printAddress(tempDeviceAddress);
  91.       lcd.println();
  92.  
  93.     } else {
  94.       Serial.print("Found ghost device at ");
  95.       Serial.print(i, DEC);
  96.       Serial.print(" but could not detect address. Check power and cabling");
  97.     }
  98.   }
  99.   delay(10000);
  100. }
  101.  
  102. // function to print the temperature for a device
  103. void printTemperature(DeviceAddress deviceAddress)
  104. {
  105.  
  106.  
  107.   // method 1 - slower
  108.   Serial.print(sensors.getTempC(deviceAddress));
  109.   Serial.println("°C");
  110.   lcd.print(sensors.getTempC(deviceAddress),1);
  111.  
  112.  
  113.   //Serial.print(" Temp F: ");
  114.   //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  115.  
  116.   // method 2 - faster
  117.   // float tempC = sensors.getTempC(deviceAddress);
  118.   // Serial.print("Temp C: ");
  119.   // Serial.print(tempC);
  120.   // Serial.print(" Temp F: ");
  121.   // Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  122. }
  123.  
  124. void loop(void)
  125. {
  126.   lcd.clear();
  127.   lcd.setFontSize(FONT_SIZE_MEDIUM);
  128.   lcd.setCursor(0, 0);
  129.  
  130.   // call sensors.requestTemperatures() to issue a global temperature
  131.   // request to all devices on the bus
  132.   Serial.println("=================================");
  133.   Serial.print("Requesting temperatures...");
  134.   sensors.requestTemperatures(); // Send the command to get temperatures
  135.   Serial.println("DONE");
  136.  
  137.  
  138.   // Loop through each device, print out temperature data
  139.   for (int i = 0; i < numberOfDevices; i++)
  140.   {
  141.     // Search the wire for address
  142.     if (sensors.getAddress(tempDeviceAddress, i))
  143.     {
  144.       // Output the device ID
  145.       Serial.print("Temperature for device: ");
  146.       Serial.print(i, DEC);
  147.       Serial.print(" ");
  148.  
  149.       lcd.print("Device ");
  150.       lcd.print(i);
  151.       lcd.print(" ");
  152.  
  153.       // It responds almost immediately. Let's print out the data
  154.       printTemperature(tempDeviceAddress); // Use a simple function to print out the data
  155.       lcd.println();
  156.  
  157.     }
  158.     //else ghost device! Check your power requirements and cabling
  159.  
  160.   }
  161.   delay(10000);
  162. }
  163.  
  164. // function to print a device address
  165. void printAddress(DeviceAddress deviceAddress)
  166. {
  167.   for (uint8_t i = 0; i < 8; i++)
  168.   {
  169.     if (deviceAddress[i] < 16) Serial.print("0");
  170.     Serial.print(deviceAddress[i], HEX);
  171.   }
  172. }
  173.  
  174. // function to print a device address
  175. void lcd_printAddress(DeviceAddress deviceAddress)
  176. {
  177.   for (uint8_t i = 0; i < 8; i++)
  178.   {
  179.     if (deviceAddress[i] < 16) lcd.print("0");
  180.     lcd.print(deviceAddress[i], HEX);
  181.   }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement