Pyboard: Using a microSD Card
Contents
Introduction
The Pyboard's filesystem basics are discussed in this article. The internal filesystem /flash is convenient but has the very limited storage capacity of 95 KB. For serious projects such as long term and/or very rapid logging of data from sensors and so on, this storage limitation becomes a show-stopper.
Any current microSD card may be used in the Pyboard's slot; up to a theoretical limit of 2 TB. The storage space available to MicroPython is only limited by the card's capacity.
Booting from the microSD Card
The options for the use of a microSD card with the Pyboard are very flexible. There is the choice to boot from either the card or the internal filesystem.
When the Pyboard is powered-up and the MicroPython system becomes active it will look for a microSD card. If one is found then it is mounted as the /sd filesystem. The files boot.py and main.py if present will be run in that order.
The internal filesystem /flash is also mounted, though /sd remains the current (default).
The internal filesystem can be forced as the boot even if MicroPython detects the presence of a microSD card. When the Pyboard boots, if it detects an empty file named SKIPSD in the root directory of /flash, it will ignore the microSD card and proceed to boot from the internal flash memory.
import os
print(os.listdir('/flash'))
Output:
['boot.py', 'main.py', 'pybcdc.inf', 'README.txt', 'SKIPSD']
The user scripts (boot.py and main.py) will then be searched for and run from /flash. The microSD card will not be mounted as a filesystem. It is still available but must be manually mounted. The process for this is described in the next section.
Manually Mounting the microSD Card
If a microSD card is present but hasn't been mounted automatically by MicroPython then the following script will manually perform this task.
import pyb
import vfs
import os
# Manually mount Pyboard's microSD card.
sd = pyb.SDCard()
vfs.mount(sd, '/sd')
print('microSD card is mounted')
print(os.listdir('/'))
# Manually unmount Pyboard's microSD card.
vfs.umount('/sd')
print('\nmicroSD card is unmounted')
print(os.listdir('/'))
Output:
microSD card is mounted
['flash', 'sd']
microSD card is unmounted
['flash']
For completion this MicroPython script also demonstrates how to unmount the microSD card.