SPI with SD?

Hello, I am trying to use the SPI library to talk to a sensor, then use the SD library to write data to an SD card. However, I am having mucho problems. If anyone can shed any light on the topic, I'd appreciate it.

The sensor uses SPI mode 3, and the SD card uses SPI mode 0. I know we can use something like SPI.setDataMode(SPI_MODE3); to change the mode, but can we change back and forth throughout the code? I am running into problems with that (SD card errors and erroneous readings from the sensor).

Also, I tried to just change the chip selects to deselect the SD card


then send some data to the sensor


and switch back


However, after this when I try to write to the SD card, I get an error! Why would sending data on SPI cause problems when the chip select is not selected?

Even stranger, it does not cause an error on the SD card if I transfer all ones... 0xFF. Does this make any sense to anyone?



Later, Me too I will have to control two devices with SPI protocol. It’s because, I’m interesting to understand your problem and I hope help you to resolve it.

So, to be sure, if I understand, your sensor and SD card share the same MISO, MOSI, and CLK lines on your arduino board ?
And you tried to select/deselect SD card and sensor with “digitalWrite” in high/low mode ?

Also, I tried to just change the chip selects to deselect the SD card



Be careful, because if you SDcs is in high value, it means SDcs ignores the master. I think CS (Chip select is complementary). It’s /CS, not CS. But I’m not sure.
When a device’s Slave Select pin is low, it communicates with the master. When it’s high, it ignores the master.
Try to inverse, :slight_smile:

EDIT : I’m sorry I made a mistake, here in your code, it’s correct. Sorry :*

Can you give me reference of your sensor and SD card, please ? :)

Well I hope I/we can figure this out and get it working for both of us! They do share the same digital lines (MISO, MOSI, CLK) and have different chip selects. I am using the ADIS16367 from Analog Devices, and the microSD breakout board from Sparkfun. I want to read several registers from the inertial measurement unit, then write them to the microSD card after a few samples from each sensor. I use the Timer1 library I found in the playground to trigger an interrupt in which I read the sensor registers and put the values in hex into a char array. I will post any important updates!


Did you check CLK rate ?


Page 9, table 7, there are some settings for Master processor SPI.

Hello, I did check the clock rate. I am using the 8MHz Arduino Pro Mini and calling   SPI.setClockDivider(SPI_CLOCK_DIV16); to cut it down to 500 kHz. This is below the maximum 1MHz for calling a burst read. I posted my entire code on the other thread if you are interested http://arduino.cc/forum/index.php/topic,50010.0.html

Two things here:

1. Have a look at the SPI.beginTransaction() command. It sets all the SPI settings in one command for the specific device you need to talk to. You can call the command as many times as you need to in order to accommodate SPI devices that use different SPI modes and clock speeds.

2. Have you got the two SPI devices working separately first? If you have, then the issue is likely to be with your SD card module. See this thread for more info.