2 I2C devices

Hi,
I’m trying to use “DS1307 Real Time Clock breakout board kit” with an i2c temperature sensor. The serial monitor returns floating/too high temperature values when I add a line of code to read date/time from RTC. This is the line that i causing the problem:
DateTime now = rtc.now(); // there is a problem in this line of code

I’m using libraries for both devices which work well individually. How do I need to transition from reading RTC to reading temperature somehow?

#include <SoftWire.h>
#include <HIH61XX.h>
#include <AsyncDelay.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

HIH61xx hih;
AsyncDelay samplingInterval;




void setup(void)
{
  Serial.begin(9600);
  hih.initialise(20, 21);
  samplingInterval.start(3000, AsyncDelay::MILLIS);

  while (!Serial){
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2016, 10, 13, 11, 23, 0));
  }
}
}
  bool printed = true;





void loop()
{
  DateTime now = rtc.now(); // there is a problem in this line of code
  
  if (samplingInterval.isExpired() && !hih.isSampling()) {
    hih.start();
    printed = false;
    samplingInterval.repeat();
    Serial.println("Sampling started");
  }


    hih.process();
    if (hih.isFinished() && !printed) {
    printed = true;
    // Print saved values
    Serial.print("RH: ");
    Serial.print(hih.getRelHumidity() / 100.0);
    Serial.println(" %");
    Serial.print("Ambient: ");
    Serial.print(hih.getAmbientTemp() / 100.0);
    Serial.println(" deg C");
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }
  delay(3000);
}

Please use code tags and show full code... what is hih and which library are you using?

Updated, please see above. I'm using HIH61XX.h
Any pointers to the problem would be highly appreciated

What sort of Arduino are you using?

Why is the RTC initializations stuff inside the while (!Serial)

Delta_G, that's just to check status of RTC. The actual query of data from RTC is further down in this line:
DateTime now = rtc.now();

When this line is commented, data from temp sensor seem accurate, other wise, it's 326 F!

B.

But why is the part that initialized the RTC inside the while(!Serial). The RTC must be initialized and depending on what type of Arduino you have that code may not be run inside there.

This part:

while (!Serial){
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {

Delta_G, RTC now works! problem half solved. Thank you! you're right that line is only for certain arduino models. Now only if I can figure out why temp readings are constant (seem to be floating). The output is like this:

RH: 655.35 %
Ambient: 327.67 deg C
2016/10/17 (Monday) 10:58:59

Good luck with that. When you decide you need more help then post the new version of the code so someone else can see it.

New code:

#include <SoftWire.h>
#include <HIH61XX.h>
#include <AsyncDelay.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

HIH61xx hih;
AsyncDelay samplingInterval;

void setup()
{
  Serial.begin(9600);
  hih.initialise(20, 21);
  samplingInterval.start(3000, AsyncDelay::MILLIS);
  if (! rtc.begin()) {
  Serial.println("Couldn't find RTC");
  while (1);
  }
  
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2016, 10, 13, 11, 23, 0));
}
}
bool printed = true;


void loop(void)
{
    if (samplingInterval.isExpired() && !hih.isSampling()) {
    hih.start();
    printed = false;
    samplingInterval.repeat();
}
    hih.process();
    if (hih.isFinished() && !printed) {
    printed = true;
    // Print saved values
    Serial.print("RH: ");
    Serial.print(hih.getRelHumidity() / 100.0);
    Serial.println(" %");
    Serial.print("Ambient: ");
    Serial.print(hih.getAmbientTemp() / 100.0);
    Serial.println(" deg C");
    
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(2000);
}
}

Output is:
RH: 655.35 %
Ambient: 327.67 deg C
2016/10/18 (Tuesday) 10:7:21

If you have any tips to fix this error please share your thoughts.
Thanks :slight_smile:

The numbers you get back are a clue. 65535 is the max value for unsigned int and 32767 is max value for a signed int. I don't know that sensor but those particular values look like a sensor that isn't working or isn't connected properly.

The sensor works fine using the sketch below, which is what i used. In the sensor library it states that status5, which is what i’m getting, is statusTimeout error.

Temp sensor sketch:

#include <SoftWire.h>
#include <HIH61XX.h>
#include <AsyncDelay.h>

HIH61xx hih;
AsyncDelay samplingInterval;

void setup(void)
{
  Serial.begin(9600);
  hih.initialise(20, 21);
  samplingInterval.start(3000, AsyncDelay::MILLIS);
}

bool printed = true;
void loop(void)
{
  if (samplingInterval.isExpired() && !hih.isSampling()) {
    hih.start();
    printed = false;
    samplingInterval.repeat();
    Serial.println("Sampling started");
  }

  hih.process();
  
  if (hih.isFinished() && !printed) {
    printed = true;
    // Print saved values
    Serial.print("RH: ");
    Serial.print(hih.getRelHumidity() / 100.0);
    Serial.println(" %");
    Serial.print("Ambient: ");
    Serial.print(hih.getAmbientTemp() / 100.0);
    Serial.println(" deg C");
    Serial.print("Status: ");
    Serial.println(hih.getStatus());
  }
  
}

Output is:
RH: 38.49 %
Ambient: 24.87 deg C