CircuitPython-compatible microcontrollers show up as a CIRCUITPY drive when plugged into your computer, allowing you to edit code directly on the board. Perhaps you've wondered whether or not you can write data from CircuitPython directly to the board to act as a data logger. The answer is yes!

The storage module in CircuitPython enables you to write code that allows CircuitPython to write data to the CIRCUITPY drive. This process requires you to include a file on your CIRCUITPY drive, along side your file.

The file is special - the code within it is executed when CircuitPython starts up, either from a hard reset or powering up the board. It is not run on soft reset, for example, if you reload the board from the serial console or the REPL. This is in contrast to the code within, which is executed after CircuitPython is already running.

The CIRCUITPY drive is typically writable by your computer; this is what allows you to edit your code directly on the board. The reason you need a file is that you have to set the filesystem to be read-only by your computer to allow it to be writable by CircuitPython. This is because CircuitPython cannot write to the filesystem at the same time as your computer. Doing so can lead to filesystem corruption and loss of all content on the drive, so CircuitPython is designed to only allow one at at time.

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

# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
# SPDX-License-Identifier: MIT

"""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("/", readonly=switch.value)

The storage.remount() command has a readonly keyword argument. This argument refers to the read/write state of CircuitPython. It does NOT refer to the read/write state of your computer.

When the physical pin is connected to ground, it returns False. The readonly argument in is set to the value of the pin. When the value=True, the CIRCUITPY drive is read-only to CircuitPython (and writable by your computer). When the value=False, the CIRCUITPY drive is writable by CircuitPython (and read-only by your computer).

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.

On the Circuit Playground Express or Circuit Playground Bluefruit, the switch is in the right position (closer to the ear icon on the silkscreen) it returns False, and the CIRCUITPY drive will be writable by CircuitPython. If the switch is in the left position (closer to the music icon on the silkscreen), it returns True, and the CIRCUITPY drive will be writable by your computer.

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.

Installing Project Code

In the example below, click the Download Project Bundle button below to download the necessary libraries and the file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Essentials/CircuitPython_Logger/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
# SPDX-License-Identifier: MIT

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

# For most CircuitPython boards:
led = digitalio.DigitalInOut(board.LED)
# 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
The filesystem will NOT automatically be set to read-only after you copy these files over! You'll still be able to edit files on CIRCUITPY after saving this

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 Aug 23, 2017. It was last updated on Jul 14, 2024.

This page (CircuitPython Storage) was last updated on Jul 14, 2024.

Text editor powered by tinymce.