Maker.io main logo

CircuitPython Hardware SD Cards

2017-12-07 | By Adafruit Industries

License: See Original Project

Courtesy of Adafruit

Guide by Tony DiCola

Overview

 

Note the video above was made showing the MicroPython version of this library. Follow the guide to see both CircuitPython and MicroPython versions of using the SD card library.

Secure Digital, or SD, cards and tiny microSD cards are inexpensive and ubiquituous means of adding lots of storage to devices. For a few dollars you can have gigabytes of storage at your fingertips (smaller than your fingertips actually!). With small CircuitPython and MicroPython boards you typically have a very limited amount of flash memory to store code and data. Wouldn't it be nice if you could connect a microSD card to a Python board and expand its storage? It turns you can use microSD cards with CircuitPython and MicroPython! In fact some boards like the pyboard come with microSD card support built-in, and for other boards like the ESP8266 or M0 / SAMD21 family they can easily be connected to a microSD card that expands their storage.

This guide explores how to use a microSD card to store files for a CircuitPython MicroPython board. Specifically adding a microSD card to CircuitPython boards like Feather M0 adalogger, pyboard, WiPy, and ESP8266 will be covered in this guide. You'll learn how to connect a microSD card to the board and mount it as a new filesystem that store code & data.

Hardware

Parts

You'll need the following parts to follow this guide:

CircuitPython board

CircuitPython board

This guide focuses on the ESP8266 and Feather M0/SAMD21-based boards, but any CircuitPython board that supports I2C should work. You'll find a board with a SD card holder built-in, like the Feather M0 Adalogger, the most simple and direct way to use an SD card.

If your board doesn't come with CircuitPython running on it already then check out your board's guide for how to load CircuitPython firmware. For example the Feather M0 express guide is a good reference.

If you're using a Feather board and FeatherWing you probably want a Feather female header set or Feather stacking female header set.

MicroSD card holder

MicroSD card holder

If your board doesn't have one already you'll need to get a microSD card holder that exposes the card as a SPI device. For Feathers the Adalogger FeatherWing is an easy plug-in adapter that adds microSD card (and more) to your board. For other boards a microSD breakout is what you want.

MicroSD

MicroSD card

You'll need a card to use to store data. Any microSD card should work, but be aware the very cheap / generic cards can sometimes be more unreliable or cause problems compared to known-good cards. If a card works well for Raspberry Pi and other boards it should work well for CircuitPython and MicroPython.

Be careful to avoid extremely large (128 gigabyte) cards as they require special filesystems and block sizes that might not be compatible with the code in this guide. Stick with a typical 4 - 16 gigabyte card.

Breadboard, jumper wires, and Soldering tools

Breadboard, jumper wires, and soldering tools

If you aren't using a Feather and FeatherWing you'll need a breadboard and jumper wires to connect the components.

You'll need to solder headers to the boards. Check out the guide to excellent soldering if you're new to soldering.

Wiring

If you're using a Feather and FeatherWing just plug the FeatherWing into your board and be sure to note which pin on the FeatherWing is used for the microSD card chip select line (for the Adalogger FeatherWing this is GPIO 10 on M0 boards and GPIO 15 on the ESP8266).

plug the FeatherWing into your board

If you're wiring up a microSD card breakout you'll need to connect it to your board's hardware SPI bus, for example with the ESP8266 you might wire it as follows:

Fritzing Source

Fritzing Source

  • Board ground (GND) to microSD breakout ground (GND).
  • Board 3.3V power to microSD breakout 3.3V power.
  • Board SPI CLK (ESP8266 GPIO 14) to microSD breakout clock (CLK).
  • Board SPI MOSI (ESP8266 GPIO 13) to microSD breakout data input (DI).
  • Board SPI MISO (ESP8266 GPIO 12) to microSD breakout data output (DO).
  • Board GPIO 15 (or any other free digital IO pin) to microSD breakout chip select (CS).

Finally make sure your microSD card is formatted with the FAT or FAT32 filesystem. Most cards come pre-formatted with this filesystem, but if yours does not or you'd like to erase it look for a disk utility in your operating system. On macOS the Disk Utility program can format cards, and on Windows right-click the drive and select Format.

CircuitPython

Adafruit CircuitPython Module Install

To use a microSD card with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_SD module on your board. Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

First make sure you are running the latest version of Adafruit CircuitPython for your board. Next you'll need to install the necessary libraries to use the hardware--read below and carefully follow the referenced steps to find and install these libraries from Adafruit's CircuitPython library bundle.

Bundle Install

For express boards that have extra flash storage, like the Feather/Metro M0 express and Circuit Playground express, you can easily install the necessary libraries with Adafruit's CircuitPython bundle. This is an all-in-one package that includes the necessary libraries to use the microSD driver with CircuitPython. To install the bundle follow the steps in your board's guide, like these steps for the Feather M0 express board.

Note: Be sure to use the CircuitPython Bundle version 2.1 and up (from October 19) as it includes an updated version of the SD card module with a few necessary fixes!

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_sdcard.mpy
  • adafruit_bus_device

If your board supports USB mass storage, like the M0-based boards, then simply drag the files to the board's file system. Note on boards without external SPI flash, like a Feather M0 or Trinket/Gemma M0, you might run into issues on Mac OSX with hidden files taking up too much space when drag and drop copying, see this page for a workaround.

If your board doesn't support USB mass storage, like the ESP8266, then use a tool like ampy to copy the file to the board. You can use the latest version of ampy and its new directory copy command to easily move module directories to the board.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_sdcard.mpy and adafruit_bus_device modules copied over.

micropython Screen Shot

Usage

The following section will show how to initialize the SD card and read & write data to it from the board's Python prompt / REPL.

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Initialize & Mount SD Card Filesystem

Before you can use the microSD card you need to initialize its SPI connection and mount its filesystem. First import the necessary modules to initialize the SPI and CS line physical connections:

Copy Code
import board
import busio
import digitalio

Next create the SPI bus and a digital output for the microSD card's chip select line (be sure to select the right pin name or number for your wiring):

Copy Code
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Use board.SD_CS for Feather M0 Adalogger
cs = digitalio.DigitalInOut(board.SD_CS)
# Or use a GPIO pin like 15 for ESP8266 wiring:
#cs = digitalio.DigitalInOut(board.GPIO15)

Now import modules to access the SD card and filesystem:

Copy Code
import adafruit_sdcard
import storage

At this point you're ready to create the microSD card object and the filesystem object:

Copy Code
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)

Notice the adafruit_sdcard module has a SDCard class which contains all the logic for talking to the microSD card at a low level. This class needs to be told the SPI bus and chip select digital IO pin in its initializer.

After a SDCard class is created it can be passed to the storage module's VfsFat class. This class has all the logic for translating CircuitPython filesystem calls into low level microSD card access. Both the SDCard and VfsFat class instances are required to mount the card as a new filesystem.

Finally you can mount the microSD card's filesystem into the CircuitPython filesystem. For example to make the path /sd on the CircuitPython filesystem read and write from the card run this command:

Copy Code
storage.mount(vfs, "/sd")

The first parameter to the storage.mount command is the VfsFat class instance that was created above, and the second parameter is the location within the CircuitPython filesystem that you'd like to 'place' the microSD card. Remember the mount location as you'll need it to read and write files on the card!

Reading & Writing Data

Once the microSD card is mounted inside CircuitPython's filesystem you're ready to read and write data from it. Reading and writing data is simple using Python's file operations like open, close, read, and write. The beauty of CircuitPython and MicroPython is that they try to be as similar to desktop Python as possible, including access to files.

For example to create a file and write a line of text to it you can run:

Copy Code
with open("/sd/test.txt", "w") as f:
f.write("Hello world!\r\n")

Notice the with statement is used to create a context manager that opens and automatically closes the file. This is handy because with file access you Python you must close the file when you're done or else all the data you thought was written might be lost!

The open function is used to open the file by telling it the path to it, and the mode (w for writing). Notice the path is under /sd, /sd/test.txt. This means the file will be created on the microSD card that was mounted as that path.

Inside the context manager you can access the f variable to operate on the file while it's open. The write function is called to write a line of text to the file. Notice that unlike a print statement you need to end the string passed to write with explicit carriage returns and new lines.

You can also open a file and read a line from it with similar code:

Copy Code
with open("/sd/test.txt", "r") as f:
print("Read line from file:")
print(f.readline())

If you wanted to read and print all of the lines from a file you could call readline in a loop. Once readline reaches the end of the file it will return an empty string so you know when to stop:

Copy Code
with open("/sd/test.txt", "r") as f:
print("Printing lines in file:")
line = f.readline()
while line != '':
print(line)
line = f.readline()

There's even a readlines function that will read all of the lines in the file and return them in an array of lines. Be careful though as this means the entire file must be loaded into memory, so if the file is very large you might run out of memory. If you know your file is very small you can use it though:

Copy Code
with open("/sd/test.txt", "r") as f:
lines = f.readlines()
print("Printing lines in file:")
for line in lines:
print(line)

Finally one other very common file scenario is opening a file to add new data at the end, or append data. This works exactly the same as in Python and the open function can be told you'd like to append instead of erase and write new data (what normally happens with the w option for open). For example to add a line to the file:

Copy Code
with open("/sd/test.txt", "a") as f:
f.write("This is another line!\r\n")

Notice the a option in the open function--this tells Python to add data at the end of the file instead of erasing it and starting over at the top. Try reading the file with the code above to see the new line that was added!

That's all there is to manipulating files on microSD cards with CircuitPython!

Here are a few more complete examples of using a SD card from the Trinket M0 CircuitPython guides. These are great as a reference for more SD card usage.

List Files

Load this into main.py:

Copy Code
import adafruit_sdcard
import busio
import digitalio
import board
import storage
import os

# Use any pin that is not taken by SPI
SD_CS = board.D0

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(SD_CS)
sdcard = adafruit_sdcard.SDCard(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) " by"
elif filesize < 1000000:
sizestr = "%0.1f KB" % (filesize/1000)
else:
sizestr = "%0.1f MB" % (filesize/1000000)

prettyprintname = ""
for i 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")

Once it's loaded up, open up the REPL (and restart it with ^D if necessary) to get a printout of all the files included. We recursively print out all files and also the filesize. This is a good demo to start with because you can at least tell if your files exist!

This is a good demo to start with

But you probably want to do a little more, lets log the temperature from the chip to a file.

Here's the new script

Copy Code
import adafruit_sdcard
import microcontroller
import busio
import digitalio
import board
import storage
import os
import time

# Use any pin that is not taken by SPI
SD_CS = board.D0

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(SD_CS)
sdcard = adafruit_sdcard.SDCard(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:
led.value = True # turn on LED to indicate we're writing to the file
t = microcontroller.cpu.temperature
print("Temperature = %0.1f" % t)
f.write("%0.1f\n" % t)
led.value = False # turn off LED to indicate we're done
# file is saved
time.sleep(1)

When saved, the Trinket will start saving the temperature once per second to the SD card under the file temperature.txt

When saved, Trinket will start saving temperature

The key part of this demo is in these lines:

Copy Code
print("Logging temperature to filesystem")
# append to the file!
while True:
# open file for append
with open("/sd/temperature.txt", "a") as f:
led.value = True # turn on LED to indicate we're writing to the file
t = microcontroller.cpu.temperature
print("Temperature = %0.1f" % t)
f.write("%0.1f\n" % t)
led.value = False # turn off LED to indicate we're done
# file is saved
time.sleep(1)

This is a slightly complex demo but it's for a good reason. We use with (a 'context') to open the file for appending, that way the file is only opened for the very short time its written to. This is safer because then if the SD card is removed or the board turned off, all the data will be safe(r).

We use the LED to let the person using this know that the temperature is being written, it turns on just before the write and then off right after.

After the LED is turned off the with ends and the context closes, the file is safely stored.

MicroPython

Note this page describes how to use a MicroPython.org version of this library with MicroPython boards. Skip back to the previous page if you're using a CircuitPython board like the Feather M0 express or Adalogger!

Before you get started you'll want to be familiar with the basics of using MicroPython by reading these guides:

Note that SD card support varies greatly for MicroPython boards. A few boards have good native support built-in:

pyboard

The pyboard is one of the easiest MicroPython boards to use with a microSD card. The board is built with a small microSD card slot and its firmware will automatically load the card as the root filesystem for the board. Plug in the card, power up the pyboard and it should automatically use the card as a filesystem to run scripts, store data, etc. You don't need to buy extra hardware or run any commands!

Check out the pyboard SD card documentation for more details.

WiPy

The WiPy also makes it easy to use a microSD card with its WiPy expansion board. The expansion board includes a microSD card slot so you can easily plug in a card. With a few lines of code you can mount the microSD card on the board's filesystem to read and write data, just like with the pyboard.

Check out the WiPy SD card documentation for more details.

Other MicroPython boards like the ESP8266 need to use a little more code to work with microSD cards. Follow the steps below to use MicroPython ESP8266 with a microSD card.

MicroPython ESP8266 SD Card Setup

Before you can use a microSD card with MicroPython on the ESP8266 you'll need to make sure you're using at least firmware version 1.8.4 or higher. If you aren't familiar with loading MicroPython firmware on the ESP8266 check out this handy guide for all the details. Be sure to get MicroPython ESP8266 stable firmware version 1.8.4 or higher from its download page!

Once your board is running MicroPython 1.8.4 or higher there's one more step to enable microSD card access. You need to manually install a Python module to talk to the SD card. This module is not included in the MicroPython ESP8266 firmware by default, but it's easy to copy to the board's internal filesystem. Be sure to read this guide on loading modules to understand more about how you can copy modules and load them.

The quickest way to load the module is to copy its source to the board. This isn't the most memory efficient way to load the module (it will use a bit more RAM), but it's good to quickly get started. Once you have SD card access working you can freeze the module into a custom build of MicroPython firmware to reduce its memory usage.

First download the SD card driver module from the MicroPython GitHub repository by clicking the button below:

Download MicroPython sdcard.py

Next use a tool like ampy to copy the sdcard.py file to the root of the board's filesystem:

Copy Code
ampy --port /board/serial/port put sdcard.py

At this point the sdcard.py file should be on the root of the MicroPython board's filesystem. You're ready to run code that can import the SD card and make it the new root filesystem.

Mount SD Card

To mount the SD card as the MicroPython ESP8266 board's filesystem you'll need to run a few commands. Connect to the board's serial or other REPL and first run this import command:

Copy Code
import machine, sdcard, os

If you see an error like the sdcard module couldn't be found or imported double check that you copied the module's .py file to the board (or that you froze it into the firmware if you're building a custom MicroPython firmware).

Once the necessary modules are imported you can initialize access to the SD card with this command:

Copy Code
sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))

Make sure you're using MicroPython ESP8266 version 1.8.4 or higher! If you're using an older firmware version this command will fail because the machine.SPI object doesn't use interface number 1.

This command will create a SD card object that's initialized using the hardware SPI interface (interface 1 for the ESP8266) and the specified chip select pin (GPIO 15, but you can use other GPIO pins if you wire them accordingly).

Next to make the SD card the new root filesystem run the following command:

Copy Code
vfs = os.VfsFat(sd, "")

This line will create a new virtual FAT filesystem using the SD card as a backing store instead of the internal flash memory on the board. The filesystem is assigned to a global vfs object that MicroPython ESP8266 internally uses for its filesystem.

It actually isn't that important that the vfs object be assigned since the VfsFat object initializer does all the work to mount the filesystem. However as a best practice it's good to assign the new VfsFat object to a variable so it never gets garbage collected.

The second parameter to the VfsFat object initializer is the path on the filesystem to mount the card. For now only the empty string is allowed as it specifies to load the filesystem as the new root (i.e. under / ).

At this point the microSD card should be the new root filesystem for the board. You can interact with the files just like interacting with files on the internal filesystem. See the CircuitPython file usage page for details on file reading and writing with the open, read, and write functions. Remember since the card is mounted as the root filesystem you access files directly under the / path and not /sd or other deeper paths!

制造商零件编号 2390
PYBOARD V1.1 STM32F405 NO HEADER
Adafruit Industries LLC
制造商零件编号 2821
ESP8266 FEATHER HUZZAH LOOSE HDR
Adafruit Industries LLC
制造商零件编号 2772
FEATHER M0 BAS PROTO ATSAMD21G18
Adafruit Industries LLC
制造商零件编号 2796
EVAL BOARD FOR ATSAMD21G18
Adafruit Industries LLC
制造商零件编号 2886
FEATHER HEADER KIT FML
Adafruit Industries LLC
制造商零件编号 2830
FEATHER STACKING HEADERS FML
Adafruit Industries LLC
制造商零件编号 254
MICROSD CARD BREAKOUT 5V OR 3V
Adafruit Industries LLC
制造商零件编号 64
BREADBOARD TERM STRIP 3.40X2.20"
Adafruit Industries LLC
制造商零件编号 153
JUMPER WIRE M TO M VARIOUS
Adafruit Industries LLC
制造商零件编号 136
LADYADAS ELECTRONICS TOOLKIT
Adafruit Industries LLC
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum