Conflict I2C and SPI ?

Hello, :slight_smile:
I’m doing a bachelor’s project where I store luminosity sensor’s data in a SD card
(Sorry for my english, I’m from switzerland)

I’m using an Adafruit Feather HUZZAH with ESP8266 WiFi (Adafruit Feather HUZZAH with ESP8266 - Loose Headers : ID 2821 : $16.95 : Adafruit Industries, Unique & fun DIY electronics and kits)
a luminosity sensorTSL2561 wich work with I2C (Adafruit TSL2561 Digital Luminosity/Lux/Light Sensor Breakout : ID 439 : $5.95 : Adafruit Industries, Unique & fun DIY electronics and kits)
And a SD shield wich work with SPI (Adalogger FeatherWing - RTC + SD Add-on For All Feather Boards : ID 2922 : $8.95 : Adafruit Industries, Unique & fun DIY electronics and kits)

I wrote 2 programs wich work separately
-one for the luminosity sensor

#include <Adafruit_Sensor.h>

// lumi
#include <Wire.h>
#include "TSL2561.h"
TSL2561 tsl(TSL2561_ADDR_FLOAT); 

void setup() {
  // Initialise I2C communication as MASTER
  Wire.begin();
  
  // start the serial connection
  Serial.begin(115200);

  // test capteur lumi
  if (tsl.begin()) {
    Serial.println("Found luminosity sensor");
  } else {
    Serial.println("No luminosity sensor?");
    while (1);
  }
  tsl.setGain(TSL2561_GAIN_16X);      // set 16x gain (for dim situations)
  tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)

  // wait for serial monitor to open
  while(! Serial);
}

void loop() {

  uint16_t lux = tsl.getLuminosity(TSL2561_VISIBLE); 
  
  
  // Output data to serial monitor
  Serial.print("lux: ");
  Serial.println(lux);
  
  // wait 5 seconds (5000 milliseconds == 5 seconds)
  delay(5000);
}

-The other for writing data on a Sd card

/*
 

 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)

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
 int test=3;
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = test;
    test=test+1;
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
    Serial.println("printed in SD");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    
  }
  delay(2000);
}

The problem is when I want to make them work together, nothing work

//SD
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

#include <Adafruit_Sensor.h>

// lumi
#include <Wire.h>
#include "TSL2561.h"
TSL2561 tsl(TSL2561_ADDR_FLOAT); 

void setup() {
  // Initialise I2C communication as MASTER
  Wire.begin();
  
  // start the serial connection
  Serial.begin(115200);

  // test capteur lumi
  if (tsl.begin()) {
    Serial.println("Found luminosity sensor");
  } else {
    Serial.println("No luminosity sensor?");
    while (1);
  }
  tsl.setGain(TSL2561_GAIN_16X);      // set 16x gain (for dim situations)
  tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)

  // wait for serial monitor to open
  while(! Serial);

  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");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {

  uint16_t lux = tsl.getLuminosity(TSL2561_VISIBLE); 
  
  
  // Output data to serial monitor
  Serial.print("lux: ");
  Serial.println(lux);

  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = lux;
    
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
    Serial.println("printed in SD");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    
  }
  // wait 5 seconds (5000 milliseconds == 5 seconds)
  delay(5000);
}

I’m begging for solutions
Thanks

You have the SPI chip select on pin 4 but on the Huzzah that's the SDA pin of the I2C interface. According to the documentation the FeatherWing you're using has it's CS on D15 and not D4, so I think it's interesting that your SD card sketch is working as it shouldn't.

Why an SD card. The Huzzay has 3Mb of buildin SPIFFS flash. That could hold a lot of 'luminosity data'. See this guide. Leo..