import sdcard
The sdcard module reads and writes files on a FAT16/FAT32 microSD card connected over SPI. Use it for data logging, camera images, configuration files, web assets, and offline message storage. ESP32 GPIO is 3.3 V logic. Use a microSD module designed for 3.3 V signals, or one with proper level shifting. SD card operations can block briefly, so avoid running heavy file work inside timing-critical callbacks.
Quick example
import sdcard
card = sdcard.SDCard(cs=5)
card.mount()
card.write_text("/hello.txt", "Hello from MojoScale\n")
print(card.read_text("/hello.txt"))
card.unmount()
import sdcard
import bme280
import system
card = sdcard.SDCard(cs=5)
card.mount()
log = card.open("/environment.csv", "a")
def record():
reading = bme280.read()
line = str(system.millis()) + "," + str(reading["temperature"]) + "\n"
log.write(line)
log.flush()
system.schedule("sd-log", 5000, 0, -1, record)
importsdcard
microSD card storage module.
The sdcard module reads and writes files on a FAT16/FAT32 microSD card connected over SPI. Use it for data logging, camera images, configuration files, web assets, and offline message storage. ESP32 GPIO is 3.3 V logic. Use a microSD module designed for 3.3 V signals, or one with proper level shifting. SD card operations can block briefly, so avoid running heavy file work inside timing-critical callbacks.
sdcard.SDCard(cs: int, spi: int = None, frequency: int = None, mount_point: str = None) -> sdcard_card
sdcard.SDCard(cs: int, spi: int = None, frequency: int = None, mount_point: str = None) -> sdcard_cardCreate a microSD card object.
Creating the object does not mount the card. Call ``mount()`` before file operations. The module uses the configured SPI bus pins when SPI has already been initialized; otherwise it starts the selected SPI bus with board-default pins.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
cs |
int |
positional or keyword | Yes | Chip-select GPIO connected to the SD card module. |
spi |
int |
positional or keyword | No | Optional SPI bus number. Defaults to 2. |
frequency |
int |
positional or keyword | No | Optional SPI clock frequency in hertz. Defaults to 20000000. |
mount_point |
str |
positional or keyword | No | Optional internal mount location. Defaults to "/sd". |
sdcard_card sdcard_card object.
sdcard.SDCard().mount() -> bool
sdcard.SDCard().mount() -> boolMount the FAT filesystem. Returns True when the card is mounted.
sdcard.SDCard().unmount() -> bool
sdcard.SDCard().unmount() -> boolFlush and unmount the card. Close open files first.
sdcard.SDCard().is_mounted() -> bool
sdcard.SDCard().is_mounted() -> boolReturn whether the card is currently mounted.
sdcard.SDCard().info() -> dict
sdcard.SDCard().info() -> dictReturn type, size_bytes, used_bytes, free_bytes, mount_point, mounted, cs, spi, frequency, and open_files.
sdcard.SDCard().exists(path) -> bool
sdcard.SDCard().exists(path) -> boolReturn True when a file or directory exists. Path must start with "/".
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
sdcard.SDCard().stat(path) -> dict
sdcard.SDCard().stat(path) -> dictReturn name, path, type, size, and modified for a file or directory.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
sdcard.SDCard().list(path="/") -> list
sdcard.SDCard().list(path="/") -> listReturn direct child entries for a directory. This is not recursive.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | No | Optional value. Defaults to "/". |
sdcard.SDCard().mkdir(path) -> bool
sdcard.SDCard().mkdir(path) -> boolCreate a directory, including missing parent directories.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
sdcard.SDCard().remove(path) -> bool
sdcard.SDCard().remove(path) -> boolDelete a file or an empty directory. This is not recursive.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
sdcard.SDCard().rename(source, destination, overwrite=False) -> bool
sdcard.SDCard().rename(source, destination, overwrite=False) -> boolRename or move a file or directory on the same card.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
source |
int |
positional or keyword | Yes | Required value. |
destination |
int |
positional or keyword | Yes | Required value. |
overwrite |
bool |
positional or keyword | No | Optional value. Defaults to False. |
sdcard.SDCard().open(path, mode="r") -> sdcard_file: Open a file. Modes
sdcard.SDCard().open(path, mode="r") -> sdcard_file: Open a file. Modes"r", "w", "a", "r+", "w+", "a+", plus binary variants.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
mode |
str |
positional or keyword | No | Optional value. Defaults to "r". |
sdcard.SDCard().read_text(path, max_bytes=65536) -> str
sdcard.SDCard().read_text(path, max_bytes=65536) -> strRead a UTF-8 text file, rejecting files larger than max_bytes.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
max_bytes |
int |
positional or keyword | No | Optional value. Defaults to 65536. |
sdcard.SDCard().write_text(path, content) -> bool
sdcard.SDCard().write_text(path, content) -> boolWrite text, replacing the file contents.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
content |
any |
positional or keyword | Yes | Required value. |
sdcard.SDCard().append_text(path, content) -> bool
sdcard.SDCard().append_text(path, content) -> boolAppend text, creating the file when needed.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Required value. |
content |
any |
positional or keyword | Yes | Required value. |
sdcard.SDCard().close() -> bool
sdcard.SDCard().close() -> boolAlias for unmount().
sdcard.SDCard().File Object Methods
sdcard.SDCard().File Object Methods
sdcard.SDCard().read(size=-1) -> str
sdcard.SDCard().read(size=-1) -> strRead data from the current position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
size |
any |
positional or keyword | No | Optional value. Defaults to -1. |
sdcard.SDCard().readline() -> str
sdcard.SDCard().readline() -> strRead one line, or nil at end of file.
sdcard.SDCard().write(data) -> int
sdcard.SDCard().write(data) -> intWrite a string or bytes value and return bytes written.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
any |
positional or keyword | Yes | Required value. |
sdcard.SDCard().flush() -> bool
sdcard.SDCard().flush() -> boolFlush buffered data to the card.
sdcard.SDCard().seek(offset, origin="start") -> bool
sdcard.SDCard().seek(offset, origin="start") -> boolMove the file position. Origins are "start", "current", and "end".
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
offset |
float |
positional or keyword | Yes | Required value. |
origin |
any |
positional or keyword | No | Optional value. Defaults to "start". |
sdcard.SDCard().tell() -> int
sdcard.SDCard().tell() -> intReturn the current byte position.
sdcard.SDCard().size() -> int
sdcard.SDCard().size() -> intReturn the file size in bytes.
sdcard.SDCard().close() -> bool
sdcard.SDCard().close() -> boolFlush and close the file.