Storing data on a microSD card using Serial Peripheral Interface (SPI).

Components

  • 1x microSD card module
  • 1x microSD card

Instructions

With the arrow on the silkscreen facing up, the pins from top to bottom are:

  • 3.3v (VCC)
  • Chip Select (CS)
  • Data In (DI)
  • Clock (CLK)
  • Data Out (DO)
  • Ground (GND)

Make the following connections

  • VCC to 3.3v
  • GND to GND
  • CS to GP09
  • DI to GP15
  • CLK to GP14
  • DO to GP12

Look at and run sd-card.cpp. Open the serial console and you will see some information get printed about the SD card.

The code actually writes to a file on the SD card.

Info

SD cards (and by extension microSD cards) can be interfaced over Serial Peripheral Interface (SPI). You can send commands to the card to read and write data. In the example code, a library is used that writes data to the card in a FAT filesystem. In theory, you could then mount the SD card on your computer to read back the data.

Using an SD card is a better alternative to Flash storage, because you can easily read the data off. Also, SD cards generally are more durable and don’t wear out as fast as flash does. SD Cards can have much greater storage capacity than a microcontroller’s built-in flash (SDXC can go up to 2TB).

Exercises

Exercise

Look at the library code for the SD card. Can you figure out how to read the contents of a file on the SD card? How about writing to multiple files.

As an extension, take input from the serial console, then write that to a file on the SD Card.

Exercise

Use a computer to read the SD card.