Using SD library with analog pins

I’m running out of pins, so I want to use the analog pins as digital for the SD library

Normal My choice
pin 10 pin 15, A1 for CS
pin 11 pin 16, A2 for MOSI
pin 13 pin 17, A3 for SCK
pin 12 pin 18, A4 for MISO

I changed these pin assignments in the pins_arduino.h file which appears to set them, but my sample code does not initialize the SD library.

Any suggestions? I suspect I need other changes deep in the library. Here’s my code.

Thanks, Theron Wierenga

#include <SD.h>

File myFile;

void setup()
// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only

Serial.print(“Initializing SD card…”);
// On the Ethernet Shield, CS is pin 4. It’s set as an output by default.
// Note that even if it’s not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
pinMode(15, OUTPUT); // CS or SS
pinMode(16, OUTPUT); // MOSI
pinMode(17, OUTPUT); // SCK clock
pinMode(18, INPUT); // MISO

if (!SD.begin(15)) {
Serial.println(“initialization failed!”);
Serial.println(“initialization done.”);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile =“test_new.txt”, FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print(“Writing to test.txt…”);
myFile.println(“testing 1, 2, 3.”);
// close the file:
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening test.txt”);

// re-open the file for reading:
myFile =“test_new.txt”);
if (myFile) {

// read from the file until there’s nothing else in it:
while (myFile.available()) {
// close the file:
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening test.txt”);

void loop()
// nothing happens after setup

those functions are dedicated to those pins in hardware, you might be able to use software SPI but its going to be slow, and may be a pain to get the lib to work. I would just move whatever you have on the hardware SPI pins to the analog port

SD pin assignments in the Sd2PinMap.h this is path but check according your library path \arduino-1\libraries\SD\utility

1 Like