Problems with SD card and SPI bus

The Setup
I have 4 Arduino Nano. One is the master, the other three are the slaves. The master communicates with the three other slave Arduinos to collect their sensor data. This is done using the SPI bus. The master then logs that data to a SD card, also on the SPI bus.

What Works
Both parts work individually. When the SD library isn’t used (SD.begin), the master can successfully talk to the slaves and retrieve there information. When the slaves aren’t plugged in, the master can successfully log data to the SD card.

The Problem
I can’t get the slaves and the SD card all working at the same time. The SD card doesn’t register and the data that the master receives from the slaves is corrupted. I believe the root of the problem is the SD.h library setting up strange clocks for the SPI bus. It may also be the SPI mode that the library initializes.

Master Code

    #include <SPI.h>
    #include <SD.h>
    
    // Slave select pins
    const int slave1 = 9, slave2 = 8, slave3 = 7;
    const int sd = 10;
    
    // Timer, not 100% accurate due to SPI delays
    int count = 0;
    
    
    
    //-------------------------------------------------------
    //                      setup
    //-------------------------------------------------------
    // Summary: initalizes slave select pins and sets them
    //          to HIGH to deselect. Also begins SPI and SD
    //-------------------------------------------------------
    void setup(){
      pinMode(slave1, OUTPUT);
      pinMode(slave2, OUTPUT);
      pinMode(slave3, OUTPUT);
      digitalWrite(slave1, HIGH);
      digitalWrite(slave2, HIGH);
      digitalWrite(slave3, HIGH);
    
      SD.begin(sd);
      SPI.begin();
      SPI.setDataMode(SPI_MODE0); // Same as SD card?
      SPI.setClockDivider(SPI_CLOCK_DIV8);
    }
    
    
   

    float getTemp (int device){
      // Implementation not show
      // Just transfering and receiving data
      // to/from the arduino slaves
    }
    
    
    
    //-------------------------------------------------------
    //                  getData
    //-------------------------------------------------------
    // Passed: slave select pin and the number of devices
    //             the slave is carrying
    // Return: temperature data in .csv form
    //-------------------------------------------------------
    String getData(int pin, int numDevices){
      String csv = "";
      digitalWrite(pin, LOW);  // Selects the slave
      for(int i = 0; i < numDevices; i++){
        csv += csv + ",";
        csv += getTemp(i);
      }
      digitalWrite(pin, HIGH); // Deselects the slave
      return csv;
    }
    
    
    
    //-------------------------------------------------------
    //                       toF
    //-------------------------------------------------------
    float toF(int raw){
      return ((raw * .0625) * 1.8) + 32;
    }
    
    
    
    //-------------------------------------------------------
    //                      loop
    //-------------------------------------------------------
    void loop()
    {
      delay (1000);
      String csv = "";
      count++;
      
      // Constructing data to send to SD card
      csv += count;
      csv += getData(slave1, 1);
      csv += getData(slave2, 2);
      csv += getData(slave3, 3);

      File file = SD.open("file.csv", FILE_WRITE);
      if(file){
        Serial.println("Success");
        file.println(csv);
        file.close();
      }
    }

Arduino Slave Code

    #include <Wire.h>

    int temp;
    
    // SPI Setup
    void setup (void){
      Serial.begin(9600);
      pinMode(MISO, OUTPUT);
      SPCR |= _BV(SPE);
      SPCR |= _BV(SPIE);
      Wire.begin();
    }
    
    // Interupt
    ISR (SPI_STC_vect){
      byte data = SPDR;
      byte c = data % 10;
      byte i = data / 10;
      if(c == 1)
        SPDR = 11; // Location
      if(c == 2)
        SPDR = temps[i] / 10;
      if(c == 3)
        SPDR = temps[i] % 10;
      if(c == 0)
        SPDR = 0;
    }
    
    void getTemp(int add){
      Wire.requestFrom(add, 2);
      byte MSB = Wire.read();
      byte LSB = Wire.read();
      temp = ((MSB << 8) | LSB) >> 4;
    }
    
    void loop(){  
      getTemp(0x48);
      delay(1000);
    }

I have 4 Arduino Nano

Yeah, we know, you told us in your other identical post, now deleted.

DO NOT CROSS-POST CROSS-POSTING WASTES TIME.

Sorry, I'm new to this forum and I wasn't sure where it fit better

stmc225:
Sorry, I'm new to this forum and I wasn't sure where it fit better

So why didn't you ask?

You're so cranky

stmc225:
You're so cranky

Wanna share life experiences?

Yeah! You start :slight_smile:

I'll start. Why are you using the wire library with the SPI bus? Why not shiftIn and shiftOut?

I think the Wire library is only for the slaves that have I2C temperature sensors on them.

What SD card adaptor? The Nano is 5V and SD cards are exclusively 3.3V. Depending on the design choices of the adaptor, it may block the SPI bus from other communication.

Be careful that you aren't running out of SRAM due to the rather large amounts required by the SPI, SD, and Wire libraries (not to mention your own code).

Link