Micro Sd Cards With The M5stamps3

Photo of AJB2K3

Made by AJB2K3 / IoT

About the project

How to use Micro SD cards with the M5Stamp S3

Project info

Items used in this project

Hardware components

M5Stamp S3 M5Stamp S3 x 1

Software apps and online services

Micropython Micropython

Story

Using Micro SD Card Adapters with M5Stack M5Stamp S3


Cheap 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.

Micro SD Card Adapter

Micro SD Card Adapter

Micro SD Card Adapter

The pinout for the micro SD card adapter is as follows:

Micro SD Card Adapter Pinout

Micro SD Card Adapter Pinout

Micro SD Card Adapter Pinout

In order to access the card data we dont need any additional drivers as the functions are built into the core of MicroPython.

In order 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:

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")

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.

Screenshot of OSX's text editor showing the message written to the file.

Screenshot of OSX's text editor showing the message written to the file.

Code

Test code for reading and writing to SD cards.

Credits

Photo of AJB2K3

AJB2K3

Self trained in electronics due to undiagnosed Depression, A.D.D and dyslexia at a young age.

   

Leave your feedback...