Micro-SD cards with the M5StampS3
2024-07-15 | By M5Stack
License: General Public License Bluetooth / BLE Programmers Wifi Arduino ESP32 M5Stack
Thanks for the source code and project information provided by @Adam Bryant
Micro SD Card Adapter
Low-cost micro-SD card adapters are available online for adding simple SD card data access. For this guide, I will be using this adapter connected over S.P.I.
The pinout for the micro SD card adapter is as follows:
In order to access the card data, we don't need any additional drivers as the functions are built into the core of MicroPython.
To use the card adapter, we first need to import the following functions:
import machine
from machine import Pin, SPI, SDCard
import os
Next we configure the pins that the SD card adapter is connected to with:
sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(6), miso=Pin(4), mosi=Pin(5), cs=Pin(7), freq=10000000)
And then we try mounting the card adapter with a check:
try:
os.mount(sd, "/sd")
except:
print("Failed to mount SD card”)
Using the following sample code from the MicroPython documentation pages:
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")
We get a long list of files or which most are hidden in the OS but at the end of the list we find the files we want:
number.txt Size: 6 by
._number.txt Size: 4.1 KB
letters.txt Size: 16 by
._letters.txt Size: 4.1 KB
To access the files, we use the following code:
with open('/sd/letters.txt', 'r') as file:
data = file.read()
print(data)
Which will show the contents of the file.
In order to write to the card, we can use the following code:
with open('/sd/letters.txt', 'w') as file:
file.write("Hello, MicroPython!”)
Before we can remove the SD Card from the adapter, the card has to be unmounted with the following code or the files on the card will be corrupted.
os.umount(“/sd")
If we put the card back into a computer, we can view the contents of the file we just wrote the message to.
Screenshot of OSX's text editor showing the message written to the file
Code
Test code for reading and writing to SD cards. MicroPython
import machine
from machine import Pin, SPI, SDCard
import os
sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(6), miso=Pin(4), mosi=Pin(5), cs=Pin(7), freq=10000000)
try:
os.mount(sd, "/sd")
except:
print("Failed to mount SD card”)
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")
with open('/sd/letters.txt', 'r') as file:
data = file.read()
print(data)
with open('/sd/letters.txt', 'w') as file:
file.write("Hello, MicroPython!”)
os.umount(“/sd")
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum