Development Board: SD/MMC Cards

SD Card, 2GB

Note: Please ensure you are using the [emphasisbold]latest version of SDFileSystem.bas[/emphasisbold] (at time of writing: 4.1.4). If you are updating (or do not have the library), then ensure to remove the old "C:\ProgramData\Mecanique\Swordfish\Library\SDMMC" files (if you haven't already).

The capability of being able to store GB's of data will make some microcontroller programmers get a little excited. SD Cards are very easy to use and only have two downsides - the PCB footprint and they operate on 3.3V. 


  • Use a 3.3V compatible PIC (all of the LF PIC's are designed for low voltage) & a LD1117V33 (3.3V regulator).
    • OR use voltage level switching resistors to interface with a 5v PIC (see the SF help file - great example).
  • Design a footprint with your circuit design program of choice. (Eagle is free)

The first option is easy enough, you may need some more info for the footprint design. With the SD Card connectors visible, here's the pinout;

sdcard_9p1 Note that pin 9 is before pin 1.

Pin SPI Mode
Name Type Description
1 CS I Chip selection in low status
2 DI I Data input
3 VSS S Supply voltage
4 VDD S Power supply
5 SCLK I Clock
6 VSS2 S Supply voltage
7 DO O/PP Data output


Programming with SD Cards

Swordfish ships with a library for SD Cards, and there are some advanced user modules available. For this article, I'll focus on the standard SD Card library "SDFileSystem.bas" as it is both covered in SF Help and there are three sample programs in the SF "Samples\SDMMC" folder. Here's the SDReadWrite.bas program;

// device and clock...
Device = 18F2620
Clock =  20
// import SD file system, usart and conversion modules...
#option SD_SPI = MSSP
#option SD_SPI_SPEED = spiOscDiv4
Include "SDFileSystem.bas"
Include "usart.bas"
Include "Convert.bas"
// variables...
Dim Index As Byte
// program start...
USART.Write("Initialising card..."1310)   
If SD.Init() Then
   // format SD card...
   USART.Write("Formatting, please wait..."1310)   
   // write data to SD card...
   USART.Write("Writing data, please wait..."1310)
   If SD.NewFile("test.txt") = errOK Then
      For Index = 0 To 255
         SD.Write("Line ",DecToStr(Index,3)1310)
   // read data back...
   USART.Write("Reading data..."1310)
   If SD.OpenFile("test.txt") = errOK Then
      Until SD.EOF


Note that I have made three changes to the program;

  • Changed the first line from Device = 18F452 to Device = 18F2620.
  • Added #option SD_SPI = SW as the 18F2620 has hardware SPI errata.
  • Changed SD.Init(spiOscDiv64) to SD.Init() as we are now using the software SPI module.

There are some points to consider when working with the SD Card library, one is that the card must be formatted to FAT16 or FAT32 (can be done QuickFormat procedure). Another point is that one file can be open at any time. This does not stop you from opening a file, appending/writing, closing, then doing the same for others. When working with files, always ensure that you are working in the right directory, as all file specific commands will function in the current directory.

The above program covers the basics to interfacing with SD Cards, now its time to build the hardware to support it.


Development Board

The SF help file has a circuit diagram for how to interface with SD Cards, however, keep in mind that it uses level shifting circuitry for 5V applications - many of the resistors are not required if a low voltage compatible PIC and 3.3V regulator is used.

This single layer development board is purpose built to interface with SD Cards, and provides headers to work with external circuits/projects. There's also a 3.3V output to power external circuits or connect common grounds. Here's the layout of the board, and schematic below (download the bottom copper layout and make your own board!);


The SD Card socket used for this project was brought from sparkfun electronics for $3.95. Download the bottom copper layout here and make your own development board.


The Schematic


(Note: The PIC shown is a 18F2520, although in real life I used the low voltage variant 18LF2520). 

To program the PIC; simply connect your PICKit to the PICKIT2 PRG 6 pin header and set Vdd to target in the PK2 software (a safe option for in-circuit programming).

When complete, plug the PK2 into the other 6 pin header to receive the UART output.



Here's an animated GIF image of the PICKit 2 UART Tool receiving from the PIC.

The final product (top view);


And the bottom view;



Forum Activity

Member Access