Using two IC's which both use I2C Communication

Hi

I do hope someone can help me. I have two IC's and an arduino mega, the IC's I have are 24LC256 (non volatile memory) and I have just got a RTC DS1307. I have connected the 24LC256 to pin 20 and pin 21 of the arduino mega and used the wire.h library (worked great), I do know I have to use the wire library for the RTC and use the library RTClib, however my issues is how do I wire it. Do I connect the SDA and the SCL pins from the 24LC256 to the same SDA and SCL pin of the RTC, or do I have to use the pins SDA1 and SCL1 pins of the arduino mega.

Thank you

I2C is a bus architecture, so you can have multiple elements connected to the same SDA/SCL lines. Devices have different addresses and that's how they are being addressed. Pullup resistors might or might not be needed depending on your hardware.

(image from first hit on google)

1 Like

You can use the two pins next to the Aref pin for the second I2C device.
Not that it makes any difference though. They are internally connected to pin 20/21 anyway.

And yes, I2C is a bus that can theoretically support up to about 127 devices on the same two wires.
As long as each device has a unique address.
Leo..

how would i set each device to a unquie address i got it to work but not sure if it will case a issue i connected one 10Kohm to 5V to the SDA from the memory and i connected another 10kohm from 5V to the sda of the RTC and a 5V to 10Kohm to the SDL of the RTC

The 24LC256 has three address pins which you need to connect either to ground or Vcc. There are eight possible combinations of the three pins' settings. The DS1307 already has a different address to any of those eight.

The RTC does not have a "SDL" pin.

You connect both SDA pins together and to the Arduino, also both SCL pins together and to the Arduino, you have one 10k from 5 V to each of those two common connections, just two resistors.

Thanks for the reply i have attached two pictures one showing how i have connected it the hardware side and another showing an issue i am getting, at random points i am getting a weird reading as shown in the file output.png, iam unaware if this is an issue with software or hardware
output

So what is "R1" supposed to be doing, since it is in parallel with "R3"?

Since we have no idea whatsoever of your code or its purpose, it is clearly not possible to comment on your output.

Hi i do apologies that i forgot to paste my code when asking if there is a software issue, i have removed R1 and below is my code. my code uploads led sequences to 24ClL256 and stores them in memory and when a number is sent to the serial it runs the sequence and also gets the date and time.

#include <RTClib.h>
#include <Wire.h>
#include <Tlc5940.h>

/*
 * 
 * 
 * Outputs:
 * ________________________
 * 
  Red : 1
  Green : 2
  Blue : 3
  Red And Green : 4
  Red And Blue : 5
  Green And Blue : 6
  All On : 7
  All Off : 8
 *
 * 
 * 
 * 
*/

#define eeprom 0x50

String readString;

int speed_threshold_1;
int speed_threshold_2;
int speed_threshold_3;

int speed_radar;

RTC_DS1307 RTC;

void setup() {
  // put your setup code here, to run once:

   Wire.begin();
 RTC.begin();
 Serial.begin(115200);
 
   Tlc.init();
  Tlc.clear();
  Tlc.update();

  Read();
 date_time();
}

void loop() {
  // put your main code here, to run repeatedly:
while (Serial.available() > 0) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(5);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() > 0) {

//Serial.println(readString);

 if (readString == "Delete") {
      Delete();
    }

    else if (readString == "Upload") {
      Upload();
    }

     else if (readString == "Read") {
      Read();
    }
    else if (readString == "DateTime") {
      date_time();
    }
    else{
      speed_radar = readString.toInt();


DateTime now = RTC.now();
 Serial.print(speed_radar);
 Serial.print("MPH");
  Serial.print(",");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(",");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();



 if(speed_radar > 3 && speed_radar <= speed_threshold_1){
       //Serial.println("Sequence 1");
       sequence_1();
      }
 if(speed_radar > speed_threshold_1 && speed_radar <= speed_threshold_2){
      // Serial.println("Sequence 2");
      sequence_2();
      }
       if(speed_radar > speed_threshold_2 && speed_radar <= speed_threshold_3){
       //Serial.println("Sequence 3");
       sequence_3();
      }
       if(speed_radar > speed_threshold_3){
       Serial.println("Sequence 4");
      }
      
    }
  }
   readString = "";
}


///////////////////////////Delete//////////////////////////////
void Delete() {
  
}
///////////////////Upload//////////////////////////
void Upload() {


///////////////////Upload Speed beign//////////////////////////
int threshold_speed_upload;

while (Serial.available() == 0)
{

}
threshold_speed_upload = Serial.parseInt(); 
  //Serial.println(data);
Wire.beginTransmission(eeprom);
  Wire.write((int)(0 >> 8));   // MSB
  Wire.write((int)(0 & 0xFF)); // LSB
  Wire.write(threshold_speed_upload);
  Wire.endTransmission();
  delay(5);
    
  Serial.println("OK");

while (Serial.available() == 0)
{

}
threshold_speed_upload = Serial.parseInt(); 
  Wire.beginTransmission(eeprom);
  Wire.write((int)(1 >> 8));   // MSB
  Wire.write((int)(1 & 0xFF)); // LSB
  Wire.write(threshold_speed_upload);
  Wire.endTransmission();
  delay(5);

  Serial.println("OK");

while (Serial.available() == 0)
{

}

threshold_speed_upload = Serial.parseInt(); 
  Wire.beginTransmission(eeprom);
  Wire.write((int)(2 >> 8));   // MSB
  Wire.write((int)(2 & 0xFF)); // LSB
  Wire.write(threshold_speed_upload);
  Wire.endTransmission();
  delay(5);

  Serial.println("OK");

  ///////////////////Upload Speed End//////////////////////////

  ///////////////////Upload Squence beign//////////////////////
  
    ///////////////////Upload Squence 1//////////////////////
int sequence_upload;
  int i = 3;
   while(i < 11){
while (Serial.available() == 0)
{

}
sequence_upload = Serial.parseInt(); 
 Wire.beginTransmission(eeprom);
  Wire.write((int)(i >> 8));   // MSB
  Wire.write((int)(i & 0xFF)); // LSB
  Wire.write(sequence_upload);
  Wire.endTransmission();
  delay(5);

  Serial.println("OK");
i++;
   }
   ///////////////////Upload Squence 2//////////////////////

   int j = 11;
   while(j < 19){
while (Serial.available() == 0)
{

}
sequence_upload = Serial.parseInt(); 
 Wire.beginTransmission(eeprom);
  Wire.write((int)(j >> 8));   // MSB
  Wire.write((int)(j & 0xFF)); // LSB
  Wire.write(sequence_upload);
  Wire.endTransmission();
  delay(5);

  Serial.println("OK");
j++;
   }

    ///////////////////Upload Squence 3//////////////////////

   int k = 19;
   while(k < 27){
while (Serial.available() == 0)
{

}
sequence_upload = Serial.parseInt(); 
 Wire.beginTransmission(eeprom);
  Wire.write((int)(k >> 8));   // MSB
  Wire.write((int)(k & 0xFF)); // LSB
  Wire.write(sequence_upload);
  Wire.endTransmission();
  delay(5);

  Serial.println("OK");
k++;
   }

  ///////////////////Upload Squence End///////////////////////

  Read();
}






///////////////////////////Read Store Variables/////////////////////////////////
void Read() {
byte rdata = 0xFF;
byte rdata2 = 0xFF;
byte rdata3 = 0xFF;
// uint8_t _byte;
  Wire.beginTransmission(eeprom);
  Wire.write((int)(0 >> 8));   // MSB
  Wire.write((int)(0 & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);
 
  if (Wire.available()){
    rdata = Wire.read();
  }
//Serial.println(rdata);
speed_threshold_1 = rdata;

Wire.beginTransmission(eeprom);
  Wire.write((int)(1 >> 8));   // MSB
  Wire.write((int)(1 & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);
 
  if (Wire.available()){
    rdata2 = Wire.read();
  }
//Serial.println(rdata2);
speed_threshold_2 = rdata2;

Wire.beginTransmission(eeprom);
  Wire.write((int)(2 >> 8));   // MSB
  Wire.write((int)(2 & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);
 
  if (Wire.available()){
    rdata3 = Wire.read();
  }
//Serial.println(rdata3);
speed_threshold_3 = rdata3;

}



///////////////////////////Loop squence 1/////////////////////////////////
void sequence_1(){
   uint8_t _byte;
  int d = 0;
  for (int i = 3; i < 11; i++) {
Wire.beginTransmission(eeprom);
  Wire.write((int)(i >> 8));   // MSB
  Wire.write((int)(i & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);

  if (Wire.available()){
    _byte = Wire.read();
  }
  if(_byte == 1){
     Tlc.set(d, 3095);
     //Serial.println("RED");
     }
     //Green//
     if(_byte == 2){
     Tlc.set(d+1, 3095);
     //Serial.println("Green");
     }
     //Blue//
     if(_byte == 3){
     Tlc.set(d+2, 3095);
    // Serial.println("Blue");
     }

     //Yellow//
     if(_byte == 4){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 3095);
       //Serial.println("Yellow");
     }
      //Magenta//
      if(_byte == 5){
      Tlc.set(d, 3095);
      Tlc.set(d+2, 3095);
       //Serial.println("Magenta");
     }

     //Cyan//
      if(_byte == 6){
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("Cyan");
     }

     //White//
      if(_byte == 7){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("White");
     }
     d = d + 3;
  }
  Tlc.update();
delay(5000);
Tlc.clear();
Tlc.update();
}

///////////////////////////Loop squence 2/////////////////////////////////
void sequence_2(){
   uint8_t _byte;
  int d = 0;
  for (int i = 11; i < 19; i++) {
Wire.beginTransmission(eeprom);
  Wire.write((int)(i >> 8));   // MSB
  Wire.write((int)(i & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);

  if (Wire.available()){
    _byte = Wire.read();
  }
  if(_byte == 1){
     Tlc.set(d, 3095);
     //Serial.println("RED");
     }
     //Green//
     if(_byte == 2){
     Tlc.set(d+1, 3095);
     //Serial.println("Green");
     }
     //Blue//
     if(_byte == 3){
     Tlc.set(d+2, 3095);
     //Serial.println("Blue");
     }

     //Yellow//
     if(_byte == 4){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 309);
      // Serial.println("Yellow");
     }
      //Magenta//
      if(_byte == 5){
      Tlc.set(d, 3095);
      Tlc.set(d+2, 3095);
       //Serial.println("Magenta");
     }

     //Cyan//
      if(_byte == 6){
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("Cyan");
     }

     //White//
      if(_byte == 7){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("White");
     }
     d = d + 3;
  }
  Tlc.update();
delay(5000);
Tlc.clear();
Tlc.update();
}

///////////////////////////Loop squence 3/////////////////////////////////
void sequence_3(){
   uint8_t _byte;
  int d = 0;
  for (int i = 19; i < 27; i++) {
Wire.beginTransmission(eeprom);
  Wire.write((int)(i >> 8));   // MSB
  Wire.write((int)(i & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(eeprom,1);

  if (Wire.available()){
    _byte = Wire.read();
  }
  if(_byte == 1){
     Tlc.set(d, 3095);
     //Serial.println("RED");
     }
     //Green//
     if(_byte == 2){
     Tlc.set(d+1, 3095);
     //Serial.println("Green");
     }
     //Blue//
     if(_byte == 3){
     Tlc.set(d+2, 3095);
     //Serial.println("Blue");
     }

     //Yellow//
     if(_byte == 4){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 3095);
      // Serial.println("Yellow");
     }
      //Magenta//
      if(_byte == 5){
      Tlc.set(d, 3095);
      Tlc.set(d+2, 3095);
       //Serial.println("Magenta");
     }

     //Cyan//
      if(_byte == 6){
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("Cyan");
     }

     //White//
      if(_byte == 7){
      Tlc.set(d, 3095);
      Tlc.set(d+1, 3095);
      Tlc.set(d+2, 3095);
      //Serial.println("White");
     }
     d = d + 3;
  }
  Tlc.update();
delay(5000);
Tlc.clear();
Tlc.update();
}

///////////////////////////Adjust Date And Time/////////////////////////////////
void date_time(){
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
   RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

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