To get you started with how to program your Pico in CircuitPython, especially for those who may have started out with the official MicroPython setup, we've 'ported' the Getting Started with MicroPython on Pico book examples to CircuitPython. The book is awesome, please download/purchase it to support Raspberry Pi Press!

All of the projects in this guide have used your Raspberry Pi Pico while connected to your computer. However, there are many microcontroller projects that work standalone, powered from the wall or a battery pack, and your Pico is perfectly capable of doing the same.

This section will show you how to use CircuitPython to read the internal temperature data and write it to a file on the filesystem to create a temperature data logger.

For this example, you'll need your Pico board, and if you want to use it separated from your computer, a micro USB wall charger or a USB battery pack and a micro USB cable.

Data Logger Wiring

CircuitPython does not allow your computer to write to the filesystem at the same time as CircuitPython is writing to the filesystem. Therefore, if you simply run storage.remount("/", readonly=False) in boot.py, CIRCUITPY is no longer writable by your computer, which means you cannot write to or delete files from the CIRCUITPY drive. This means you cannot modify boot.py. To return the filesystem to a state writable by your computer, you would need to run some commands in the REPL. More information on this process is available at the end of this page.

Alternatively, you can add a little extra code to boot.py that only runs remount if a particular pin is connected to a ground pin. This means if you start up the board with the specified pin connected to ground, the filesystem will be read-only to your computer (and therefore writable by CircuitPython), and if you start up the board without the pin connected to ground, the filesystem will be writable by your computer. If you include this extra code, you will need to have a jumper wire handy. The boot.py shown below contains this extra code. So, when you want to begin data logging, you'll need to connect a jumper wire as shown below before plugging the Pico into USB.

For this example, you'll need your Pico, a breadboard, and a male-to-male jumper wire.

  • Pico GP0 to Pico GND

Programming the Temperature Data Logger

Start with the jumper wire disconnected.

First create a file called boot.py on your CIRCUITPY drive, and save it with the following contents.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
boot.py file for Pico data logging example. If pin GP0 is connected to GND when
the pico starts up, make the filesystem writeable by CircuitPython.
"""
import board
import digitalio
import storage

write_pin = digitalio.DigitalInOut(board.GP0)
write_pin.direction = digitalio.Direction.INPUT
write_pin.pull = digitalio.Pull.UP

# If write pin is connected to ground on start-up, CircuitPython can write to CIRCUITPY filesystem.
if not write_pin.value:
    storage.remount("/", readonly=False)

It is important to know that the boot.py file is run only when the Pico starts up, e.g. when it is plugged into USB or power. (It is NOT run when the board soft resets, e.g, when you save a file etc.) This code sets up pin GP0 as an input with a pull-up.

Update your code.py to the following and save.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Data logging example for Pico. Logs the temperature to a file on the Pico.
"""
import time
import board
import digitalio
import microcontroller

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

try:
    with open("/temperature.txt", "a") as datalog:
        while True:
            temp = microcontroller.cpu.temperature
            datalog.write('{0:f}\n'.format(temp))
            datalog.flush()
            led.value = not led.value
            time.sleep(1)
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 filesystem is full...
        delay = 0.25  # ...blink the LED faster!
    while True:
        led.value = not led.value
        time.sleep(delay)

The LED should begin blinking every 0.5 seconds. This lets you know the code is running but nothing else is happening. Since you started up the board with the wire disconnected, no data logging is occurring.

Disconnect your Pico from USB, and plug the jumper wire in as shown above. Then plug your board into USB. The LED blinking will slow down to every 1 second. CIRCUITPY will mount, but only as a read-only filesystem. You'll see a temperature.txt file that wasn't there before. If you view this file, you'll see the beginning of a list of temperatures. The temperature is checked every second and added to the file.

You can now let it run to track an ambient temperature in the area. If you use a USB battery pack, you can place it anywhere to track temperature.

If you let it run for long enough, you can fill up the filesystem. At that time, the LED will start blinking quickly, every 0.25 seconds, to let you know.

When you're ready to work with your Pico again, you can unplug it from USB, unplug the jumper wire, and plug the Pico into your computer's USB again. CIRCUITPY will once again be writable by your computer and you can update code as needed.

That's all there is to logging the ambient temperature using CircuitPython and Pico!

Data Logger Without Wiring

If you want to use your Pico without a breadboard for data logging, or simply don't want to connect a wire between a pin and ground, you would use the following boot.py file.

If you add this file to CIRCUITPY, your board will always be read-only to your computer when it is plugged into USB.
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
boot.py file for Pico data logging example. If this file is present when
the pico starts up, make the filesystem writeable by CircuitPython.
"""
import storage

storage.remount("/", readonly=False)

This will make the filesystem writable by CircuitPython for the purposes of data logging every time the board is powered up. This means you can plug your Pico into your computer's USB or a USB battery pack with no additional wiring necessary, and the temperature logging will begin.

However, you'll notice that you cannot edit any of the files on the CIRCUITPY drive. So how do you make the filesystem writable by your computer again? You can use the REPL.

Using the REPL to Rename boot.py

Even when you can't write to CIRCUITPY, you can still get to the REPL. Connect your Pico to your computer via USB, and connect to the serial console. Enter the REPL (>>>) and one of the following commands.

To rename boot.py to something else so your Pico no longer sees it, run the following.

import os
os.rename("boot.py", "boot1.py")

To remove boot.py entirely, run the following.

import os
os.remove("boot.py")

That's all there is to data logging with CircuitPython and Pico without needing a jumper wire!

This guide was first published on Jan 21, 2021. It was last updated on Mar 18, 2024.

This page (Data Logger) was last updated on Mar 18, 2024.

Text editor powered by tinymce.