Code compatibility SD card Uno <> Mega

I don't have a problem doing what I want to do but I could be living in a fool's paradise. I got most of my stuff going on a Uno and was then obliged to move to a Mega.

All I did was plug it all in, upload the Uno sketch, and we were off and running without even thinking about it. Now, after months of reliable datalogging, I read a thread this evening about Uno<>Mega and SDs, and I wonder how it all happened. This particularly in that one post said that the pinmode line had to be changed to call pin 53.

My Ethernet+SD shield uses the 6-pin ICSP cluster which is common to both Uno and Mega, so I guess that is a good start. The pins 11,12,13 thereon appear to be just pass-through, but pin 10 is connected.

So, I still have the line

pinMode(10, OUTPUT);

in my code and pin 53 doesn't get a mention.

I only use the SD for backup, and would remove it read it. Is that what absolves me from editing the pinmode line?

You almost never need the pinMode line with the current SD libraries SD.h and SdFat.

These libraries internally set SS (pin 10 on Uno and 53 on Mega) to output mode and set it high.

The main exception is if you use the Ethernet shield on a Mega. Pin 10 which is chip select for the Ethernet controller must be set high if you initialize the SD before the Ethernet to prevent a clash between the SD and Ethernet on the SPI bus.

In general, when several devices share the SPI bus you should set each device's chip select pin to output mode and high.

This only needs to be done before device initialization since each device's library should handle the state of chip select after initialization.

fat16lib:
The main exception is if you use the Ethernet shield on a Mega. Pin 10 which is chip select for the Ethernet controller must be set high if you initialize the SD before the Ethernet to prevent a clash between the SD and Ethernet on the SPI bus.

Thanks, and karma

I assume the lines below are the initialisation, and they are in this order:

pinMode(10, OUTPUT);

if (!SD.begin(4))

....tralala.....

while (Ethernet.begin(mac) != 1)

so I should retain that.