Library conflict OneWire & RTClib

I am attempting to integrate code for temperature sensors using OneWire 2.3.7 with
PFC8523 RTC using RTClib 2.0.3 on the Adafruit Datalogger Shield. Individual test programs work fine. Adding the RTC code into the datalogger I am writing caused the program to hang when I call rtc.start().

RTClib says that it uses Wire.lib
from the timestamp example:
// Build a Date and time using a PCF8523 RTC connected via I2C and Wire lib
#include "RTClib.h"

DallasTemperature references OneWire.h

Are these libs incompatable? Suggestions?

1 Like

may be try to stick to Adafruit's world?

OK, I guess that I need to hunt for an RTC lib that used OneWire...

Though the timestamp example has exactly what I need... :frowning:

Thanks for the response!!

it's very old, so not sure it won't make a mess due to their OneWire stuff

As you might guess, I am new to the Arduino universe.

One question which might be relevant:
What is the difference between "" and <> in the #include directive?

I don't see how it could possibly use the wire lib, given that clocks between 100K and 400K which is I2C.

The one wire bus clocks at around 16K, so I would imaging the wire lib is incompatible.

You can always find useful stuff like this to help:

Found the answer regarding the #include directive. I should have searched first....

As far as I know the libraries are compatible. Can you please post the code with the issue.

Certainly:

// Include the libraries needed
#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>


RTC_PCF8523 rtc;

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// 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);

// arrays to hold device address, arbitrary limit of 10 devices
DeviceAddress sensorAddress[10];

uint8_t DeviceCount = 0;  // Number of devices found
bool tCent = false;       // select temperature scale to report

// create strings for assembling the data to log:
String dataRecord = "";   // assembled data record to write to CSV file
String retStr = "";
String dt_buffer = "";    // Date-time buffer

/*
   Setup function. Here we do the basics
*/
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  //    if (! rtc.initialized() || rtc.lostPower()) {
  //      Serial.println("RTC is NOT initialized, let's set the time!");
  //      // When time needs to be set on a new device, or after a power loss, the
  //      // following line sets the RTC to the date & time this sketch was compiled
  //      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //      }

  /* When the RTC was stopped and stays connected to the battery, it has
    to be restarted by clearing the STOP bit. Let's do this to ensure
    the RTC is running. */
  //    rtc.start();   // UNCOMMENTING THIS LINE CAUSES THE PROGRAM TO HANG

  dataRecord = "Dallas OneWire Temperature Sensor Data Logger";
  Serial.println(dataRecord);

  // locate devices on the bus, report the number found
  Serial.print("Locating devices... ");
  sensors.begin();
  DeviceCount = sensors.getDeviceCount();
  dataRecord = String(DeviceCount, DEC) + " devices found";
  Serial.println(dataRecord);

  // report parasite power requirements
  dataRecord = "Parasite power is: ";
  if (sensors.isParasitePowerMode()) {
    dataRecord += "ON";
  }
  else {
    dataRecord += "OFF";
  }
  Serial.println(dataRecord);

  // Search for devices on the bus and assign based on an index. Ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them once you know
  // the devices on your bus (and assuming they don't change).

  for (uint8_t i = 0; i < DeviceCount; i++)
  {
    if (!sensors.getAddress(sensorAddress[i], i)) Serial.println("Unable to find address for Device");
  }

  // show the addresses we found on the bus
  for (uint8_t i = 0; i < DeviceCount; i++)
  {
    strAddress(sensorAddress[i]);
    dataRecord = "Device ";
    dataRecord += String(i, DEC);
    dataRecord += ", Address: ";
    dataRecord += retStr;

    // set the resolution to 10 bit (Each Dallas/Maxim device is capable of several different resolutions)
    sensors.setResolution(sensorAddress[i], 10);
    dataRecord += ", Resolution: ";
    delay(100);
    dataRecord += String(sensors.getResolution(sensorAddress[i]), DEC);
    dataRecord += " bits";
    Serial.println(dataRecord);
  }
  // report temperature scale
  if (tCent == true)
  {
    dataRecord = "Degrees C";
  }
  else
  {
    dataRecord = "Degrees F";
  }
  Serial.println(dataRecord);

  // print device indexes for found devices, comma delimited list
  dataRecord = "0";
  for (uint8_t i = 1; i < DeviceCount; i++)
  {
    dataRecord += ",";
    dataRecord += String(i, DEC);
  }
  Serial.println(dataRecord);

  // print device addresses for found devices, comma delimited list
  strAddress(sensorAddress[0]);
  dataRecord = retStr;
  for (uint8_t i = 1; i < DeviceCount; i++)
  {
    dataRecord += ",";
    strAddress(sensorAddress[i]);
    dataRecord += retStr;
  }
  Serial.println(dataRecord);
}

/*
   Main function. It will request the tempC from the sensors and display on Serial.
*/
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus, builds CSV record of temperatures
  sensors.requestTemperatures();        // Send the command to get temperatures
  dataRecord = "";
  getTemperature(sensorAddress[0]);     // Use a simple function to add temperature to the data recoed string
  for (uint8_t i = 1; i < DeviceCount; i++)
  {
    dataRecord += ",";
    getTemperature(sensorAddress[i]);  // temps 2 thru n devices
  }
  Serial.println(dataRecord);
  delay(5000);
}


// function to format a device address
void strAddress(DeviceAddress deviceAddress)
{
  retStr = "";
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) {
      // add leading '0' to addresses less than 10h
      retStr += "0";
    }
    retStr += String(deviceAddress[i], HEX);
  }
}

// function to format the temperature for a device
void getTemperature(DeviceAddress deviceAddress )
{
  float tempC = sensors.getTempC(deviceAddress);
  float tempF = DallasTemperature::toFahrenheit(tempC);  // Converts tempC to Fahrenheit

  if (tempC == DEVICE_DISCONNECTED_C)   // returns -127C for a disconnected device
  {
    dataRecord += "Error";
    return;
  }
  if (tCent == true)
  {
    dataRecord += String(tempC, 2);
  }
  else
  {
    dataRecord += String(tempF, 2);
  }
}

Serial Monitor output with offending function call commented out:

Dallas OneWire Temperature Sensor Data Logger
Locating devices... 4 devices found
Parasite power is: OFF
Device 0, Address: 28729248f67e3c0b, Resolution: 10 bits
Device 1, Address: 289aab48f64d3c8c, Resolution: 10 bits
Device 2, Address: 28a7e548f6623cb9, Resolution: 10 bits
Device 3, Address: 285f1a48f6243c92, Resolution: 10 bits
Degrees F
0,1,2,3
28729248f67e3c0b,289aab48f64d3c8c,28a7e548f6623cb9,285f1a48f6243c92
77.00,76.55,77.00,76.55
77.00,76.55,77.00,76.55
77.00,76.55,77.00,77.00
77.00,76.55,77.00,76.55

Try calling rtc.begin() in setup() before you call .start().

That was the missing key. Thanks so much!

I guess its because wire library includes all serial type functions, be that Seria,l TWI or I2C.

Duly Noted

Actually, I missed a function call. My bad. They are working together just fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.