chipKIT® Development Platform

Inspired by Arduino™

What is the use of the pin number in the SD lib ?

Created Mon, 21 Jan 2013 15:26:00 +0000 by PICedh


Mon, 21 Jan 2013 15:26:00 +0000


I use on UNO32 the SD lib to read/write file on the SD card and it works fine but I don't undertsand what is the purpose of the pin used as parameter of the begin class.

I have put 4 like I have seen in several code examples: if (!SD.begin(4)) Serial.println("\n\n\ninitialization failed!")

But why pin 4 ? on UNO32 this pin 4 is RF1 and is not linked to SPI !


Mon, 21 Jan 2013 16:05:40 +0000

Pin 4 is Chip Select(CS). Any output pin can be used.

Look at your mpide/libraries/SD/examples/Datalogger/Datalogger.pde example for an explanation. (Or any of the other examples.)



Mon, 21 Jan 2013 21:32:35 +0000

For UNO32 based on PIC32MX320F, the Slave Select (SS) pin is #10 on chipkit (= pin 8 on PIC32).

Why do we need to use pin 4 instead of the pin 10 already hardcoded for SS/CS ?


Mon, 21 Jan 2013 22:35:15 +0000

You don't specifically NEED to use pin 4 - pin 4 is just what happens to be in use on that specific board. A chip select line can be any pin if you are driving it with software. If, however, you were driving it using the framing mechanism of the SPI peripheral, then you would have to use the proper SS pin.


Mon, 21 Jan 2013 22:44:20 +0000

Ok thanks for the answer

What is the advantage using the SPI software as there is already an hardcoded SPI provided in PIC ?

Is it because the shields are not build specially for PIC?


Mon, 21 Jan 2013 22:45:54 +0000

On the chipkit you use the SPI hardware for the actual communication, but the chip select is handled in software. It provides for greater flexibility and the ability to have multiple peripherals on one SPI channel.