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.
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
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 Feather RP2040 Adalogger """ import os import busio import digitalio import board import storage import adafruit_sdcard # Connect to the card and mount the filesystem. cs = digitalio.DigitalInOut(board.SD_CS) sd_spi = busio.SPI(board.SD_CLK, board.SD_MOSI, board.SD_MISO) sdcard = adafruit_sdcard.SDCard(sd_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.
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT """ CircuitPython Essentials SD Card Write Demo Feather RP2040 Adalogger """ import time import busio import board import digitalio import microcontroller import storage import adafruit_sdcard # Connect to the card and mount the filesystem. cs = digitalio.DigitalInOut(board.SD_CS) sd_spi = busio.SPI(board.SD_CLK, board.SD_MOSI, board.SD_MISO) sdcard = adafruit_sdcard.SDCard(sd_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.
Text editor powered by tinymce.