Go Down

Topic: How to use both DS1307 RTC and ahtx0 - two i2C devices  (Read 216 times) previous topic - next topic


I'm building a sketch to use two i2C adafruit devices:  the DS1307 RTC and the AHTx0 humidity sensor. I realize that when you have two or more slave devices (Arduino Master) on i2C that you must use the "Wire.h" library and read and write the device using the specific address.

I've learned how to do that successfully for the DS1307 - for example:

Code: [Select]
#include "Wire.h"
#define DS1307_ADDRESS 0x68

and then

Code: [Select]

  byte zero = 0x00;

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

But I'm not sure how to do that with the Adafruit AHTX0. I can successfully run it with the example sketch but not along with the DS1307 because they are both connected to SDA and SCL.  I added the following line:

#define AHTX0_I2CADDR_DEFAULT 0x38

and then when getting information from it I need to:


but not sure where to go past this to get the temperature and humidity readings from the sensor?



but not sure where to go past this to get the temperature and humidity readings from the sensor?
there are a few libraries out there for this sensor like here:


Try looking at the demo code and the library .cpp file to see how they do it! ;)


Hi Sherzaad,

Yes, I have tried to do an extensive search for others that are trying to do what I am doing. The link below is just a standard example of running the AHT0X which is not much different from the example sketches and which I've already done.

The point of my question is that I need some help with how to run this device by accessing its specific address using the wire.h library and accessing its commands via the Wire.beginTransmission(AHTX0_I2CADDR_DEFAULT);


I found a solution to my problem by way of a library I can across AHT10 written by  enjoyneering79 which is for the Humidity sensor that I'm trying access per specific address and since this library was written for ATTiny which must use wire.h I can use this library along with my DS1307 RTC. Here's a snipit:

#include <AHT10.h>                     // library for access AHT20 via wire and specific address
#include "Wire.h"

AHT10 myAHT20(AHT10_ADDRESS_0X38, AHT20_SENSOR); // set specific I2c address for humidity sensor

Serial.print(F("Temperature: "));
    Serial.print(F(" +-0.3C")); //by default "AHT10_FORCE_READ_DATA"
    Serial.print("    ");
    Serial.print(F("Humidity...: "));
    curr_humidity =  myAHT20.readHumidity();                                     //pass value on to humid_color function
    Serial.println(F(" +-2%"));   //by default "AHT10_FORCE_READ_DATA"

Go Up