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, not just on your CIRCUITPY drive. 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 Copy and paste the code into using your favorite editor. You may need to create a new file.

"""CircuitPython Essentials Storage logging file"""
import board
import digitalio
import storage

# For Gemma M0, Trinket M0, Metro M0/M4 Express, ItsyBitsy M0/M4 Express
switch = digitalio.DigitalInOut(board.D2)

# For Feather M0/M4 Express
# switch = digitalio.DigitalInOut(board.D5)

# For Circuit Playground Express, Circuit Playground Bluefruit
# switch = digitalio.DigitalInOut(board.D7)

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, 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 and Circuit Playground Bluefruit, comment out switch = digitalio.DigitalInOut(board.D2), and uncomment switch = digitalio.DigitalInOut(board.D7). Remember, D7 is the onboard slide switch, so there's no extra wires or alligator clips needed.

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 Copy and paste the code into using your favorite editor.

"""CircuitPython Essentials Storage logging example"""
import time
import board
import digitalio
import microcontroller

# For most CircuitPython boards:
led = digitalio.DigitalInOut(board.D13)
# For QT Py M0:
# led = digitalio.DigitalInOut(board.SCK)

    with open("/temperature.txt", "a") as fp:
        while True:
            temp = microcontroller.cpu.temperature
            # do the C-to-F conversion here if you would like
            led.value = not led.value
except OSError as e:  # Typically when the filesystem isn't writeable...
    delay = 0.5  # ...blink the LED every half second.
    if e.args[0] == 28:  # If the file system is full...
        delay = 0.25  # ...blink the LED faster!
    while True:
        led.value = not led.value

Logging the Temperature

The way works is by checking to see if the pin you specified in the switch setup in your code 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. 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!

This guide was first published on Jun 17, 2018. It was last updated on Jun 17, 2018.

This page (CircuitPython Storage) was last updated on Apr 10, 2021.

Text editor powered by tinymce.