I2C multiple devices comunication problem

Hello there,

I'm building a circuit with two I2C controlled sensors: a 3 axis digital compass (HMC5883L) and a clock (DS1307). Both seems to work ok separatedly, but I'm not able to connect both together.

Both can be powered by +5 - GND, so i supposed this circuit:

Then, I initialize them with this code (most part is based on the examples included with sensors libraries):

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include "RTClib.h"

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
RTC_DS1307 RTC;

void Start(){
    Serial.begin(57600);
    
    Wire.begin();
    RTC.begin();

    if(!RTC.isrunning()){
        Serial.println("RTC is NOT running!");
    }

    if(!mag.begin()){
        Serial.println("MAGNETOMETER is NOT running");
    }
}

Let's suppose there are no compilation errors:

The Serial debugs "RTC is NOT running!", while Magnetometer gives correct values.

I also suppose the conections are ok, so it's a code error?

Anyone can help me to find a solution?

EDIT:

I get it work reading without libraries, but code is much larger:

#include <Wire.h>

#define RTSADDRESS 0x68
#define MGNADRESS 0x00 //Not defined


byte cDay, cMonth, cHour;

byte decToBcd(byte val){ //Convertir valores de binario Decimal a Binario BCD
  return( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val){ //Convertir valores de Binario BCD a Decimal
  return( (val/16*10) + (val%16) );
}

void setRtsTime(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(RTSADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}

void ReadRtsTime(){ //Only day, month and hour
  Wire.beginTransmission(RTSADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(RTSADDRESS, 7); //Coger 7 bytes de RTS empezando en el registro 00h (0)
  
  int temp; //Guardamos segundos, minutos, dia de la semana, año -> Datos inutiles
  temp = bcdToDec(Wire.read() & 0x7f); //Segundos (0x7f)
  temp = bcdToDec(Wire.read()); //Minutos
  
  cHour = bcdToDec(Wire.read() & 0x3f); //0x3f -> dirección en hexadecimal de la hora en bcd

  temp = bcdToDec(Wire.read()); //Dia de la semana (1 -7)
  
  cDay = bcdToDec(Wire.read());
  cMonth = bcdToDec(Wire.read());

  temp = bcdToDec(Wire.read());
}

What are the I2C addresses of the two devices? Have you run the I2C scanner sketch to verify that it can see two devices?

PaulS:
What are the I2C addresses of the two devices? Have you run the I2C scanner sketch to verify that it can see two devices?

It seems to find 3 devices:

Scanning...
I2C device found at address 0x1E !
I2C device found at address 0x50 !
I2C device found at address 0x68 !
done

I've disconected magnetometer and still being 2 devices, tryed disconecting clock and only 1 appears:

Clock devices:

Scanning...
I2C device found at address 0x50 !
I2C device found at address 0x68 !
done

Magnetometer:

Scanning...
I2C device found at address 0x1E !
done

Thank you again!

Sounds okay. Most RTC modules have a bit of EEPROM on them as well. So that explains the two addresses when you only connect the RTC module.

All have a dfferent I2C address as well so it can work. So it depends on the code... So post the whole code....

septillion:
Sounds okay. Most RTC modules have a bit of EEPROM on them as well. So that explains the two addresses when you only connect the RTC module.

All have a dfferent I2C address as well so it can work. So it depends on the code... So post the whole code....

Well, the actual code is only a little debug to try sensors. As i don't have very clear how to read data from I2C, i'm going to learn about a bit and rewrite the code in a moment, I'll post if i've been lucky then!

Thank you a lot!

Hi Mireia

With your code from your first post ...

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include "RTClib.h"

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
RTC_DS1307 RTC;

void Start(){
    Serial.begin(57600);
    
    Wire.begin();
    RTC.begin();

    if(!RTC.isrunning()){
        Serial.println("RTC is NOT running!");
    }

    if(!mag.begin()){
        Serial.println("MAGNETOMETER is NOT running");
    }
}

... are you saying that you always get the output "RTC is NOT running!"?

Without changing the code, does it make any difference if you have the compass physically connected or disconnected?

Regards

Ray

Hackscribble:
Hi Mireia

With your code from your first post ...

#include <Wire.h>

#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include "RTClib.h"

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
RTC_DS1307 RTC;

void Start(){
    Serial.begin(57600);
   
    Wire.begin();
    RTC.begin();

if(!RTC.isrunning()){
        Serial.println("RTC is NOT running!");
    }

if(!mag.begin()){
        Serial.println("MAGNETOMETER is NOT running");
    }
}




... are you saying that you always get the output "RTC is NOT running!"?

Without changing the code, does it make any difference if you have the compass physically connected or disconnected?

Regards

Ray

It doesn't make any difference, it stills debuging the same. Now i've tryed to get it work without libraries and, it works fine (except i have hundreds of lines of code...).

I'm going to edit main post with my final solution.

Thank you!

Nooooooooooooo. Just post it over here!