NittyGritty icon

Dallas_18B20_Tester_1.ino

NittyGritty | PRO | 03/01/22 08:24:37 PM UTC | 0 ⭐ | 1319 👁️ | Never ⏰ | []
C++ |

5.07 KB

|

None

|

0 👍

/

0 👎

#include <Arduino.h>
#include <Wire.h>
#include <OneWire.h>
#include <MicroLCD.h>
#include <DallasTemperature.h>
#include <Time.h>
 
 
const char compile_date[] =  __FILE__ " " __DATE__ " " __TIME__; 
 
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10
#define TEMPERATURE_PRECISION 11 // Lower resolution
 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
int numberOfDevices; // Number of temperature devices found
 
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
 
//LCD_SH1106 lcd; /* for SH1106 OLED module */
LCD_SSD1306 lcd; /* for SSD1306 OLED module */
 
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  lcd.begin();
 
  Serial.println(compile_date);
  lcd.print( __FILE__ );
  lcd.setCursor(0, 1);                       // Anfang auf Stelle 0, Zeile 0 setzen
  lcd.print( __DATE__ );
 
  Serial.println("Dallas Temperature IC Control Library Demo");
  
  // Start up the library
  sensors.begin();
 
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  
  // locate devices on the bus
  Serial.print("Locating devices...");
 
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
 
  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  delay(5000);
 
  lcd.clear();
  lcd.setFontSize(FONT_SIZE_SMALL);
  lcd.setCursor(0, 0);
 
  // Loop through each device, print out address
  for (int i = 0; i < numberOfDevices; i++)
  {
    // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i))
    {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
 
      Serial.print("Setting resolution to ");
      Serial.println(TEMPERATURE_PRECISION, DEC);
 
      // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
 
      Serial.print("Resolution actually set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
      Serial.println();
      lcd.print("Device: ");
      lcd.println(i);
 
      lcd.println("Addr ");
      lcd_printAddress(tempDeviceAddress);
      lcd.println();
 
    } else {
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }
  }
  delay(10000);
}
 
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
 
  
  // method 1 - slower
  Serial.print(sensors.getTempC(deviceAddress));
  Serial.println("°C"); 
  lcd.print(sensors.getTempC(deviceAddress),1);
 
  
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
 
  // method 2 - faster
  // float tempC = sensors.getTempC(deviceAddress);
  // Serial.print("Temp C: ");
  // Serial.print(tempC);
  // Serial.print(" Temp F: ");
  // Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
 
void loop(void)
{
  lcd.clear();
  lcd.setFontSize(FONT_SIZE_MEDIUM);
  lcd.setCursor(0, 0);
 
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.println("=================================");
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
 
  // Loop through each device, print out temperature data
  for (int i = 0; i < numberOfDevices; i++)
  {
    // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i))
    {
      // Output the device ID
      Serial.print("Temperature for device: ");
      Serial.print(i, DEC);
      Serial.print(" ");
 
      lcd.print("Device ");
      lcd.print(i);
      lcd.print(" ");
 
      // It responds almost immediately. Let's print out the data
      printTemperature(tempDeviceAddress); // Use a simple function to print out the data
      lcd.println();
 
    }
    //else ghost device! Check your power requirements and cabling
 
  }
  delay(10000);
}
 
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}
 
// function to print a device address
void lcd_printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) lcd.print("0");
    lcd.print(deviceAddress[i], HEX);
  }
}
 

Comments