Possible SD and Ethernet conflicts on SPI

So I've been banging my head all day trying to figure this out, but not having any luck. I have an Arduino Mega 1280 with an Arduino branded Ethernet shield(w/ sd).

My issue is. I'm able to use the Ethernet libraries just fine at the beginning of my code, but once I initialize the sd library, the Ethernet becomes unreliable and the data sent to my server is full of garbage. Sometimes my actual message is buried in the garbage. For example I run this block of code

char mg[] = "GET /restInterface/sensor=11,temp=22,datetime=3333/ HTTP/1.1";

And my server sees something like this

";odsf jpow4ujfpa98asfefGET /restInterface/sensor=11,temp=22,datetime=3333/ HTTP/1.1alsdkjf;iajoi8fj;oakjd;d"

I'm thinking the issue is the sd library or something with the HW is corrupting the SPI bus, but I can't figure it out. Anyone have any suggestions that may point me in the right direction? The full source can be found at http://code.google.com/p/arduinodatacollector/source/browse/#svn%2Ftrunkwith the SD code in the Logging.cpp and the Ethernet code in Networking.cpp

The below might have some info.


Without looking at your source, SPI should work reliably with the proviso that you take care of the SS line (slave select). Only one device should be addressed at once (by taking SS for it low) and then released (by taking SS high). You need a SS for each device (ethernet, SD card).


Having a logic analyser would help, you might see which library is, or is not, handling the SS line properly. You also need to make sure that each library is told the correct SS pin. Of course, you definitely need a different SS pin for each device, don't try to share pin 10 or anything like that.

If I remember correctly, one of the ethernet shields had a problem between the SD card and W5100 regarding the SS lines. Wasn't it?

@Nick Gammon

I forgot I had a Salae analyzer in the back of my desk. So I plugged it and and set the trigger on the SS for the SD card. Then I waited... and waited.. and waited... It appears that the library is never letting go of the bus. I'm going to dig into it a bit and see if I can figure out what the deal is. Thanks for the push in the right direction, I'll post back when I get some results.

You could always push it low yourself. After calling the library, just digitalWrite (Dx, LOW) - whatever Dx pin is (eg. D9).

Well... It turns I set the the wrong pin for the SD controller's SS. An entire day wasted spent learning! I was thinking it was something deep in the code but realized it was right in from of my face. Thanks for the help!