Problem using both SPI library and SD library in Arduino UNO program

I was not sure about this part, but I think you were showing an example of how to fix the SS by calling a function that sets it HIGH.

Yes. If you see "SD SS is LOW" in the serial monitor, you will know the SD.begin() function from adafruit does not disable the slave select, and will correct that IF the slave select was LOW.

The serial does not output "SD SS is LOW". Or, it will output "SD SS is HIGH, good!" if you use the code I modified in my last post.

So we know that the initialization for the SPI and the SD are correct, only in terms of the SS being set HIGH after each device is initialized. Is it safe to say the problem lies inside the begin calls for SD and SPI? (I based this on our recent findings and that the SD.begin() will fail and print "failed!" to the serial output).

What are your thoughts?

I use the standard SD library included with the Arduino IDE, so I do not have experience with the adafruit library. I know the IDE SD library does not interfere with other SPI devices. I use it on a regular basis with the w5100 without fail. The w5100 library uses SPI.begin() to set up the SPI.

The standard SD library included with the Arduino IDE does allow the SD.begin() to return true, but we can still see the same data problems over the SPI with the IMU device. Here is the initialization code:

/* Initialization */
void setup() {
  Serial.begin(9600);
  // start the SPI library:
  SPI.begin();
  // initalize the chip select pin:
  pinMode(chipSelectPin, OUTPUT);
  
  // Code from Arduino Forum, by SurferTim
  if (digitalRead(chipSelectPin) == LOW) {
    Serial.println(F("SPI SS is LOW"));
    digitalWrite(chipSelectPin, HIGH);
  } else {
    Serial.println(F("SPI SS is HIGH, good!"));
  }
  
  // Code from Arduino Forum, by SurferTim
  Serial.print(F("Initializing SD card..."));
  // Set the chip select pin for the SDcard board:
  pinMode(chipSelectPinSDcard, OUTPUT);
  // Initialize the SDcard and library
//  if (!SD.begin(chipSelectPinSDcard, 11, 12, 13)) {
  if (!SD.begin(chipSelectPinSDcard)) {
    Serial.println(F("failed!"));
  } else {
    Serial.println(F("done."));
  }
  // add this to check and correct the status of the SD slave select
  if (digitalRead(chipSelectPinSDcard) == LOW) {
    Serial.println(F("SD SS is LOW"));
    digitalWrite(chipSelectPinSDcard, HIGH);
  } else {
    Serial.println(F("SD SS is HIGH, good!"));
  }
  // End code by SurferTim
  
  // Give the sensor time to set up:
  delay(100);
}

We are desperate for a solution to this problem, do you have any concrete examples of the two working together? I do not know what a w5100 is or know anything about the 5100 library, but would assume the code is similar in these circumstances.

Thank you again, Dave

Hi Dave. I only have one more suggestion for you. One of the things the SD.begin() function apparently does is double the speed of the SPI bus. After the SD.begin call, add this as a test:

SPI.setClockDivider(SPI_CLOCK_DIV8);

I added your line of code after the SD.Begin() call, but we still see the same problem with interference of the SPI data being returned from the IMU.

OK, one more thing. Are you sure you are not running out of memory? I use this freeRam() function to check available SRAM. If you run out, it does weird things. Check your IMU sketch without the SD.begin() call, and see how much memory it shows. Then use SD.begin() and check.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

void setup() {
  Serial.begin(9600);
  Serial.print(F("Free SRAM = "));
  Serial.println(freeRam());
}

void loop() {
}

It will not show zero if out of memory. It will show either a negative number, or an unrealistic positive amount.

I did the check throughout the code with the SD include and SD.begin() and got a value around 1050. I took out the SD library and the SD.begin() lines and got around 1660. These do fit within the available SRAM for the Arduino and I did not see anything negative or funky.

After speaking with Mr. Greiman, the creator of the SdFat library which the SD is based on, I am going to scrap using the SD library from Arduino and focus on using the SdFat library instead. We are still open to any suggestions or questions, but time is short and the decision had to be made in which direction to head to complete the project within the time frame.

[u]For those looking for answers on this topic, we found a solution to the problem![/u]

Using the SdFat library solved all of our problems, and it only took a few modifications to the original programs. The SdFat library is the original basis of the SD library, but the SD library is based on an old/outdated version of the SdFat library. I want to thank Bill Greiman for pointing me in the right direction!

Here is a link to the SdFat download http://code.google.com/p/sdfatlib/downloads/list

Thank you very much for your time and help SurferTime, we are just happy to have a solution. (We learned a lot during your suggestions/help too!)

Best, Dave

Hi Dave2Satellite - I read your thread with great interest as I am in the same position. In my case I’m using the SD library with an A/D on the SPI line - an LTC1859CG#PBF to be precise. The SD card works fine but the A/D returns all 1’s. I placed a data analyzer on the lines and it appears like the SD card is sending garbage on the MISO line after the file is closed and the A/D interprets the garbage as 1’s. The A/D still doesn’t work properly when I upload a sketch that contains only the A/D and SPI - without the SD stuff. I have to remove power to the arduino to get the A/D working properly by itself with no SD references. Also - if I remove the SD card while the A/D is reading garbage then the A/D starts working magically (the MISO line settles back to normal operation). I went back and tried to implement the SdFat library but it didn’t work - same results. You mentioned you only made a few modifications - were any of them to the SdFat library by chance? Any help is greatly appreciated! - -Leah

I made no changes to the SdFat library, I only had to modify the programs we had written. They were the includes, definitions and the function calls, which was nothing of significance in respect to the actual code for SPI and SD/SdFat. We did not look at any of the SPI lines during use, sorry.

It sounds a bit like you are having a different problem than we had, I would recommend starting a new topic with a detailed explanation of your setup. You have a few funky things going on that are new to us, as we narrowed our problem to the SD/SPI library and ruled out the hardware. However, we did come to this conclusion: The Arduino SD library should not be used with more than one device on a SPI bus, you should use the SdFat library to get multiple devices to work on a SPI bus.

The Arduino SD library should not be used with more than one device on a SPI bus, you should use the SdFat library to get multiple devices to work on a SPI bus.

...unless the other device is the w5100 ethernet IC, then the standard SD library will work fine.

@LeahM: Try disabling the SD SPI in setup. If D4 is not the slave select, change that.

pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// then do your AD stuff

You must disable it or initialize it. Otherwise, it will cause garbage on the SPI bus.

If you initialize it, insure you disable your AD device SPI first. Otherwise, it could cause garbage on the SPI and mess up the SD startup.

Hi SurferTim - I tried you suggestion and it didn't work. I thought since the MISO line is going wonky that I'd try isolating the MISO on the SD card when it's not in use - but that didn't work either. I'll go ahead and start a new thread. Thanks!

Hi,
I am using the arduino mega 2560 with the yei 3 space sensor embedded module. I was planning on using the tx1(pin 18) and rx1(pin 19) to communicate asynchronously with the mega. However I dont seem to be getting any output. I then tried to use the SPI communication protocol from the code pasted in the first question but I am getting 0 outputs for all 3. Can someone provide some code with for communication with the arduino mega. thanks
#include<string.h>

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(18,OUTPUT);
pinMode(19,INPUT);
}

char inData[30]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

char a[32];
float b;
float c;
float d;
char str1[15];

void loop()
{
Serial1.print(":0\n"); // over here the face is a “: 0” without the space
while (Serial1.available() > 0)
{
int red = Serial1.read();
if (Serial1.read() == ‘\n’)
{
Serial.print(red, HEX);
}
}
}

This is what I tried for async communication

Hello,

I solved the problem this way.

I had the exact same problem using the Adafruit micro Sd breakout board and Her Max 31855 thermocouple breakout board.

After I had initialized the SD card the thermocouples no longer worked. I had noticed that when it was on my breadboard if I disconnected the clock line the thermocouple boards started working again.

The thermocouples were not on the SPI bus they were on their own buss.

After much headbanging, I tried setting the SPI clock pin as an input after the card was initalized. Then before writing to the card again I set the card back to an output then back to input after writing.

Works like a champ now.

Hope this helps someone else in the future.

I can confirm that I had the same strange results as Dave2Satellite when trying to dump data from an RFID reader to an SD card (both devices sharing SPI). This was resolved for me as well by using the SdFat library he posted a link to above. Setting the slave select (SS) pins HIGH and LOW depending on the module you want to work with just started working after that.

Dear Dave, I have the same problem than you. How do you solve it? I download the SDFat library, but don't win use the Ethernet Module and the SD Card Module together. I think in use a pin to turn off and turn on the Ethernet Module. I do almost all, but nothing yet.

Thanks in advance.

Márcio Araújo.

I ran into same problem of cannot read temp sensor after initializing SD card. I am using the Adafruit Max31855 (PID: 269) and Adafruit Assembled Data Logger For Arduino (PID: 1141). I am controlling them with the Adafruit HUZZAH ESP8266 Breakout (PID: 2471).

I resolved the problem by changing the SPI configuration each time before I read temperature or write data to SD.

define THERM02_CS 2

define THERM0_CLK 14 // Fake SPI CLK signal Yellow

define THERM0_D0 12 // Fake SPI MISO signal Orange

define DATALOG_CS 16 // for SD on datalogger

define DATALOG_MOSI 13

define DATALOG_MISO 12

define DATALOG_SCK 14

void setupSPIForTempReading() { pinMode(THERM0_CLK, OUTPUT); pinMode(THERM0_D0, INPUT); }

void setupSPIForDataLogger() { pinMode(DATALOG_MOSI, SPECIAL); pinMode(DATALOG_SCK, SPECIAL); pinMode(DATALOG_MISO, SPECIAL); }

Works great. I read and log temperature.

I do not know if this exact setup will work for Arduino because a different SD library is used with ESP8266:

Used: C:\Users\Mud\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\SD Not used: C:\Program Files (x86)\Arduino\libraries\SD-master

Digging through the Arduino SD-master would allow you to identify what pinMode to use for Arduino.

Hello all! Dave, I'm really struggling with a datalogger i'm making for 3 temperature probes (2x PT100 with Max31865 + 1 thermocouple with Max6675.), using an Arduino Uno board.

The three sensors work great together on the SPI bus: - PT100_1 - Chip Select on pin 10 - PT100_2 - Chip Select on pin 9 - Thermocouple_K - Chip Select pin 8

Also connected the system to Nextion HMI and everything is ok. The problem is similar to what you experienced with your project, i also want to record on a SD card the measurements from the sensors and i used a SD card module with SPI also: - SD Card - Chip Select on pin 7

The card itself i tested and worked fine by itself, but connecting it to the same CLK, SDO, SDI, in paraller with the sensors, i loose the data and the values of the temperatures are all scrambled.

You finished your dialog telling you used the sdFat and it worked with minor changes. I already tried a lot and i'm not getting there... The sdFat literature info tells that the library supports multiple SPI:

«Support for multiple SPI ports now uses a pointer to a SPIClass object.

See the STM32Test example. ``` explicit SdFat(SPIClass* spiPort); \ or explicit SdFatEX(SPIClass* spiPort); »

I cheked the STM32Test example but that seems to be made for two SD cards on the same SPI bus... Am i right?

Would you be so kind to help me? How did you do it on your situation? If anyone can help me i'm really desperate... Rafael ```

is it possible for me to connect these sensors using i2c protocol? could anyone help me regarding this?