CircuitPython Storage
CircuitPython boards show up as as USB drive, allowing you to edit code directly on the board. You've been doing this
for a while. By now, maybe you've wondered, "Can I write data
from
CircuitPython
to the storage drive to act as a
datalogger?" The answer is yes!
However, it is a little tricky. You need to add some special code to boot.py, not just code.py. That's because you have
to set the filesystem to be read-only when you need to edit code to the disk from your computer, and set it to writeable
when you want the CircuitPython core to be able to write.
The following is your new boot.py. Copy and paste the code into boot.py using your favorite editor. You may need to
create a new file.
For Gemma M0, Trinket M0, Metro M0 Express, Metro M4 Express, ItsyBitsy M0 Express and ItsyBitsy M4 Express,
no changes to the initial code are needed.
For Feather M0 Express and Feather M4 Express, comment out switch = digitalio.DigitalInOut(board.D2) , and
uncomment switch = digitalio.DigitalInOut(board.D5) .
For Circuit Playground Express, comment out switch = digitalio.DigitalInOut(board.D2) , and uncomment switch =
digitalio.DigitalInOut(board.D7) .
The following is your new code.py. Copy and paste the code into code.py using your favorite editor.
You can only have either your computer edit the CIRCUITPY drive files, or CircuitPython. You cannot have
both write to the drive at the same time. (Bad Things Will Happen so we do not allow you to do it!)
import board
import digitalio
import storage
# For Gemma M0, Trinket M0, Metro M0/M4 Express, ItsyBitsy M0/M4 Express
switch = digitalio.DigitalInOut(board.D2)
# switch = digitalio.DigitalInOut(board.D5) # For Feather M0/M4 Express
# switch = digitalio.DigitalInOut(board.D7) # For Circuit Playground Express
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
# If the switch pin is connected to ground CircuitPython can write to the drive
storage.remount("/", switch.value)