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.

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

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.

import digitalio
import board
import storage

# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express
switch = digitalio.DigitalInOut(board.D2)
# switch = digitalio.DigitalInOut(board.D5)  # For Feather M0 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)

For Gemma M0, Trinket M0, Metro M0 Express, and ItsyBitsy M0 Express, no changes to the initial code are needed.

For Feather M0 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).

Remember: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # + space from the beginning of the line.

The following is your new code.py. Copy and paste the code into code.py using your favorite editor.

import board
import digitalio
import microcontroller
import time

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

try:
    with open("/temperature.txt", "a") as fp:
        while True:
            temp = microcontroller.cpu.temperature
            # do the C-to-F conversion here if you would like
            fp.write('{0:f}\n'.format(temp))
            fp.flush()
            led.value = not led.value
            time.sleep(1)
except OSError as e:
    delay = 0.5
    if e.args[0] == 28:
        delay = 0.25
    while True:
        led.value = not led.value
        time.sleep(delay)

Logging the Temperature

The way boot.py works is by checking to see if the specified pin is connected to a ground pin. If it is, it changes the read-write state of the file system, so the CircuitPython core can begin logging the temperature to the board.

For help finding the correct pins, see the wiring diagrams and information in the Find the Pins section of the CircuitPython Digital In & Out guide. Instead of wiring up a switch, however, you'll be connecting the pin directly to ground with alligator clips or jumper wires.

boot.py only runs on first boot of the device, not if you re-load the serial console with ctrl+D or if you save a file. You must EJECT the USB drive, then physically press the reset button!

Once you copied the files to your board, eject it and unplug it from your computer. If you're using your Circuit Playground Express, all you have to do is make sure the switch is to the right. Otherwise, use alligator clips or jumper wires to connect the chosen pin to ground. Then, plug your board back into your computer.

You will not be able to edit code on your CIRCUITPY drive anymore!

The red LED should blink once a second and you will see a new temperature.txt file on CIRCUITPY.

This file gets updated once per second, but you won't see data come in live. Instead, when you're ready to grab the data, eject and unplug your board. For CPX, move the switch to the left, otherwise remove the wire connecting the pin to ground. Now it will be possible for you to write to the filesystem from your computer again, but it will not be logging data.

We have a more detailed guide on this project available here: CPU Temperature Logging with CircuitPython. If you'd like more details, check it out!

Last updated on 2018-04-19 at 12.13.32 PM Published on 2017-08-23 at 05.57.02 PM