i2c & sd card reader

Hi please help me

The problem is that when using the i2c port and the sd card reader at the same time, the Arduino has

.problems and it doesn't work at all.

I think the hardware will interfere. Because if you cut i2c wires, the problem disappears.

I tried three with arduino uno and three had the same problem

/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.
 */

 // First define the library :
 
 #include <SPI.h>
 #include <SD.h>
 #include <Wire.h>
 #include <OneWire.h>
 #include <DallasTemperature.h>
 #include <BH1750FVI.h> // Sensor Library
 #include <SimpleDHT.h>
 #include <RTC_DS3231_DST.h>
RTC_DS3231_DST rtc;
 char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 #define ONE_WIRE_BUS 4
 const int chipSelect = 8;

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

byte Celsius = 0;
String data2sd="";
//float Fahrenheit = 0;

// Define Slave I2C Address
uint16_t Light_Intensity=0;
 // Call the function 

 BH1750FVI LightSensor;
#define SLAVE_ADDR 9
 
// Define Slave answer size
#define ANSWERSIZE 5

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 =2;
SimpleDHT11 dht11(pinDHT11);
//heater relay number is : 1,3,5,7,9
byte relay1 =6 ;
byte relay3_5 =7  ;
byte relay7_9 =9 ;
//pump relay number is : 10,11,12
byte relay10 =A2 ;
byte relay11 =A1 ;
byte relay12 =A0 ;
//fan relay number is : 2,4
//byte relay2 =4 ;
byte relay4 =5 ;
//motor relay number is : 6
//byte relay6 =11 ;
//light relay number is : 8
byte relay8 =3 ;

void setup() {
  // Initialize the rtc object
  delay(3000);
  rtc.begin();
  Serial.begin(9600); 
  // Initialize I2C communications as Slave
  Wire.begin(SLAVE_ADDR);
  
  // Function to run when data requested from master
  Wire.onRequest(requestEvent); 
  
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
  
  sensors.begin(); //turn on ds18b20

    //  call begin Function so turn the sensor On .
  LightSensor.begin();
  LightSensor.SetAddress(Device_Address_H); //Address 0x5C
  LightSensor.SetMode(Continuous_H_resolution_Mode);
   Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) 
    Serial.println("Card failed, or not present");
    Serial.println("card initialized.");
  
  pinMode(relay1,OUTPUT);
  //pinMode(relay2,OUTPUT);
  pinMode(relay3_5,OUTPUT);
  pinMode(relay4,OUTPUT);
  //pinMode(relay6,OUTPUT);
  pinMode(relay7_9,OUTPUT);
  pinMode(relay8,OUTPUT);
  pinMode(relay11,OUTPUT);
  pinMode(relay12,OUTPUT);
  digitalWrite(relay1,HIGH);
  //digitalWrite(relay2,HIGH);
  digitalWrite(relay3_5,HIGH);
  digitalWrite(relay4,HIGH);
//  digitalWrite(relay6,HIGH);
  digitalWrite(relay7_9,HIGH);
  digitalWrite(relay8,HIGH);
  digitalWrite(relay10,HIGH);
  digitalWrite(relay11,HIGH);
  digitalWrite(relay12,HIGH);
  
}
byte temprature = 0;
byte humidity = 0;
byte x;
byte tinput=0;
void receiveEvent() {
 
  // Read while data received
  while (0 < Wire.available()) {
    x = Wire.read();
    
  }
  
  // Print to Serial Monitor
  Serial.println("Receive event");
}
 
void loop() {
  // put your main code here, to run repeatedly:
}
void requestEvent() 
{
  if(x=='0')Wire.write(tinput);
  if(x=='1')Wire.write(humidity);
  if(x=='2')Wire.write(Light_Intensity);
  if(x=='3')Wire.write(Celsius);
  // Print to Serial Monitor
  Serial.println("Request event");
}

i2c_sd.ino (3.6 KB)

Please define what "problems and it doesn't work at all" means.

// Per.

Try cleaning up the code to make the names consistent with comments, then check that the wiring is consistent with the code. Bet you find something doing that.