I will not write the complete SD card documentation but I will explain how to connect the SD card to a microcontroleur. This codes are usable in a XC8 project from Microchip.

This article will focus on the hardware for the electronic connection between the SD card and the microcontroleur and also about the software. A word about the software, it’s an extra light library as it request only 600 octets of RAM.

The Hardware

The SD card works with 3,3V and use the SPI port. But the microcontroler (PIC18F) is supplied with 5V so it’s not really working together. You can use a level shiffter or you can do it in an easier way.


Permet de connecter une carte SD à un micro 5V

Talking to a SD card

what is the SD card version

The procedure

To communicate with the SD card, we have to initiate the card and we also need to know the SD card version. Up to know, we have 3 SD card (MMC, V1 and V2). To know we have to follow the initiation sequence:

SDCard Initialisation
Source :

Note: the complete documentation is here

At the end of this sequence, the SD card is ready to receive writing and reading commands. (Or other commands but’s not the subjet)

Lets go to the code

I will not present the SPI code as wikipedia already done it. But before going in the code we have to explain some functions. To exchange with the SD card, we need to send command and to read the results. To do this we need some functions and parameters.

The commands

They are many command that can be send to the SD card. I use the same description as LUCKY RESISTOR  for the enumeration. Here after the main commands:

They are other commands in the documentations.

Arguments and results

Once the argument is send, we have to send arguments in generaly. The SD card, returns the results to the command. To limit the memory space usage, we will use a 4 octets in RAM eighter for an array and for a 32 bits integer.

The SD card returns also a status Byte. This Byte indicates if an error occurs as address, CRC, or if the card is Idle. This Byte is stored in the SDResponse variable.

Card status register

An 8 bits register is defined to store the SD card status. It contains:

All the flag will not be used in this code.

Enabling and disabling SPI bus

A pre-processor code is used to enable the SPI bus.

Select and Deselect the SD card on the SPI bus.

Before starting to talk, we have to select the card on the bus. The functions are:

in this code, I used a Timeout timer based on an internal timer. You can replace it with a counter.

Sending a command

It’s one of the most important function. The function manage to send the command to the SD card.

it’s very easy. The card is selected on the bus, it send the command, the argument and the CRC. Then, the SD card send the results.


So now we can start to initialize the card. First all the flag are set to 0 and the default status is FAILED. Then is a initialization condition is OK, the status will be updated in READY.

This function is also used to specify to the SD card the way to access to the data. We are asking for 512 Bytes memory bloc size.


Now the SD card is ready to be used.

Connect an SD card to a PIC complete tuto
Tagged on:                                 

Leave a Reply

Your email address will not be published. Required fields are marked *