Trouble with SD card shield Arduino Mega

I am working on a project that logs sensor data to an SD card using a SD card shield stacked on an arduino mega. At first, I was able to use the shield on the arduino mega without modifications to the SD library example code. Later however, the shield stopped working after attaching another power source and an rtc module. The same shield works on a separate arduino mega without sensors attached. Could sensors I have attached to the board (on spi or card select pins) be causing the problem? How should I troubleshoot this problem?

Could sensors I have attached to the board (on spi or card select pins) be causing the problem?

Yes.

How should I troubleshoot this problem?

The simplest way is to NOT use the same pins that the SD shield uses.

If that IS necessary, posting your code AND links to the other stuff you are attaching to the SPI pins would be useful.

So would describing IN DETAIL what problems you are having.

You can NOT use the card select pin for any other purpose, though you do not have to use any specific pin as the card select pin.

I am using the CardInfo example from the sd card library. The examples fails every time - "initialization failed". I made sure to attach nothing to the pin select pin (53). I have noticed that an led on a moisture sensor I have connected to pin 51, an spi pin, receives power when I run the example code with no modifications. This is the only sensor I have attached to an spi pin. Do I need to change the pin this sensor is connected to even though it is not on the pin select pin.
When using the shield on the other arduino mega I was able to run the CardInfo code successfully without changing the pin select pin from 4. Should I try removing the moisture sensor off pin 51? Are all of the spi pins off limits even though my shield is stacked on the mega and not wired separately.

Do I need to change the pin this sensor is connected to even though it is not on the pin select pin.

Absolutely.

So I can use any digital pin as chip select?

I changed the pin used from 51 to 7 and the shield now works. Thank you for your help!