As of Nov 8, 2023 - We've fixed this hardware to be revision B. If you have a rev A hardware board, contact support to get a replacement rev B.
As of CircuitPython 9, a mount point (folder) named /sd is required on the CIRCUITPY drive. Make sure to create that directory after upgrading CircuitPython.

Your board comes with a built in MicroSD card slot! Adding a MicroSD card allows you to include more files, music, images, videos etc. than the flash on the board can hold. You can use a MicroSD card with CircuitPython and the Adafruit_CircuitPython_SD module. This module allows you to easily write Python code that lets you read and write to an attached MicroSD card.

MicroSD Card Slot

The MicroSD card slot (highlighted in red), labeled MicroSD on the board silk, is located above the analog pins in the lower right corner of the board.

For these demos to work, you must insert a MicroSD card into the slot.
Angel shot of Small microSD card 512mb
Add storage in a jiffy using this 512MB microSD card. Preformatted to FAT32, so it works out of the packaging with our projects. Works great with any device in the...
$4.95
In Stock

CircuitPython Usage

To use with CircuitPython, you need to first install the adafruit_sdcard library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

Your CIRCUITPY/lib folder should contain the following folders and files:

  • /adafruit_bus_device
  • adafruit_sdcard.mpy
CIRCUITPY

SD Card Read Test

Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials SD Card Read Demo"""

import os
import digitalio
import board
import storage
import adafruit_sdcard

# The SD_CS pin is the chip select line.
SD_CS = board.SD_CS

# Connect to the card and mount the filesystem.
cs = digitalio.DigitalInOut(SD_CS)
sdcard = adafruit_sdcard.SDCard(board.SPI(), cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

# Use the filesystem as normal! Our files are under /sd

# This helper function will print the contents of the SD
def print_directory(path, tabs=0):
    for file in os.listdir(path):
        stats = os.stat(path + "/" + file)
        filesize = stats[6]
        isdir = stats[0] & 0x4000

        if filesize < 1000:
            sizestr = str(filesize) + " bytes"
        elif filesize < 1000000:
            sizestr = "%0.1f KB" % (filesize / 1000)
        else:
            sizestr = "%0.1f MB" % (filesize / 1000000)

        prettyprintname = ""
        for _ in range(tabs):
            prettyprintname += "   "
        prettyprintname += file
        if isdir:
            prettyprintname += "/"
        print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))

        # recursively print directory contents
        if isdir:
            print_directory(path + "/" + file, tabs + 1)


print("Files on filesystem:")
print("====================")
print_directory("/sd")

In this read test for the SD card, the filesystem on the SD card is mounted and read. Then, the contents of the filesystem are printed to the REPL.

SD Card Write Test

# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
CircuitPython Essentials SD Card Write Demo
"""

import time
import adafruit_sdcard
import board
import digitalio
import microcontroller
import storage

# The SD_CS pin is the chip select line.
SD_CS = board.SD_CS

# Connect to the card and mount the filesystem.
cs = digitalio.DigitalInOut(SD_CS)
sdcard = adafruit_sdcard.SDCard(board.SPI(), cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

# Use the filesystem as normal! Our files are under /sd

print("Logging temperature to filesystem")
# append to the file!
while True:
    # open file for append
    with open("/sd/temperature.txt", "a") as f:
        t = microcontroller.cpu.temperature
        print("Temperature = %0.1f" % t)
        f.write("%0.1f\n" % t)
    # file is saved
    time.sleep(1)

In this example, the code is writing data to the SD card. The microcontroller CPU temperature is printed to the REPL and logged to a text file on the SD card. After running the code, you can read the text file from the SD card. You'll see that the text file matches what was printed to the REPL. 

This guide was first published on Aug 25, 2023. It was last updated on Jul 18, 2024.

This page (SD Card) was last updated on Jul 18, 2024.

Text editor powered by tinymce.