breadboard
docs

Example project · Builds & projects

microSD write & read-back

A real 64 MB SD card in SPI mode: the Uno runs the honest init handshake (CMD0, CMD8, ACMD41), writes a message into a raw block through the hardware SPI port, reads it back with CMD17 and prints it on the serial monitor. Watch the activity LED blink on the breakout. The card ships FAT16-formatted, so the SdFat sketch in Code (compile & upload) can write real files to it too.

The microSD write & read-back circuit as rendered by the simulator

How it works

Two programs share this bench. The bundled firmware, example:sd-block, is a small AVR program that talks to the card at the lowest level. It sends CMD0 to put the card into SPI mode, CMD8 to check the voltage range, CMD55 and ACMD41 until the card leaves its idle state, and CMD58 to read the OCR register. It prints SD init OK, writes HELLO FROM SD :) into raw block 40000 with CMD24, reads that block back with CMD17 and prints its first 16 bytes.

The card uses the Uno's hardware SPI port: SCK on D13, MOSI on D11, MISO on D12 and chip select on D10. It runs from the Uno's 3.3 V pin, because microSD cards are 3.3 V devices. Code holds a second program, an SdFat sketch that mounts the card's FAT16 filesystem and writes the same message to a real file, log.txt. That sketch replaces the bundled firmware when you compile and upload it.

What to watch

Serial, at 9600 baud, shows SD init OK and then block 40000: HELLO FROM SD :). The activity LED lights briefly during the transfer. Open Logic analyzer before pressing Restart and look at D10 to see when the card is selected. Serial shows the bytes that were read back.

What's on the bench

  • Arduino Uno R3
  • microSD card

How it's wired

  • Arduino Uno R3 · 3v3→microSD card · vcc
  • Arduino Uno R3 · gnd2→microSD card · gnd
  • Arduino Uno R3 · d13→microSD card · sck
  • Arduino Uno R3 · d11→microSD card · mosi
  • Arduino Uno R3 · d12→microSD card · miso
  • Arduino Uno R3 · d10→microSD card · cs

The code

This Arduino C++ sketch lives in Code; sign in and press Compile & upload to build real firmware for the emulated board.

// The same card through the MIT-licensed SdFat library (the card comes
// FAT16-formatted, so sd.begin just works). CS on pin 10; SdFat drives
// the hardware SPI port (11/12/13) itself.
#include <SdFat.h>

SdFat sd;

void setup() {
  Serial.begin(9600);
  if (!sd.begin(10)) {
    Serial.println("card init failed");
    return;
  }
  Serial.println("SD init OK");
  File f = sd.open("log.txt", FILE_WRITE);
  f.println("HELLO FROM SD :)");
  f.close();
  f = sd.open("log.txt");
  Serial.print("log.txt: ");
  while (f.available()) Serial.write(f.read());
  f.close();
}

void loop() {}

Try this

  • Sign in and press Compile & upload to replace the raw-block firmware with the SdFat sketch. Serial then prints SD init OK and log.txt: followed by the message read back from the file.
  • Change the text in f.println and press Compile & upload. Serial shows your new line. Then press Restart without editing the sketch. The readback shows the line twice because FILE_WRITE appends to the file on the loaded card.
  • Move chip select to D9: change the wire and sd.begin(10) to sd.begin(9). Chip select can be any GPIO, but SCK, MOSI and MISO must stay on the hardware SPI pins.

Related projects