Let’s put it all together: make this your code.py:

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

import time

import board
import digitalio
import microcontroller

led = digitalio.DigitalInOut(board.D13)

    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:
    delay = 0.5
    if e.args[0] == 28:
        delay = 0.25
    while True:
        led.value = not led.value

This code creates a led variable, sets it to the D13 pin and configures it for output (this is the built-in LED pin). Then it opens the temperature.txt file for appending (so future reboots add to the end of the file instead of overwriting it), gets the temperature and writes it to the file with a line break after each reading (on Windows, some editors like Notepad won’t recognize the line ending). The LED blinks on and off in a two second loop: each change indicates a value has been written to the file.

There could be an error opening the file for writing, or for writing the file: maybe your board doesn’t have D0 pulled low to enable writing. Maybe your internal storage is out of space. The except block handles an OSError exception. If the error code is 28 that means the device is out of space. The LED will blink four times a second to indicate this. Otherwise the “issue” is probably that the board set to read-only (which is probably by design!) and will blink twice a second.

This guide was first published on Sep 27, 2017. It was last updated on Dec 08, 2023.

This page (Logging the temperature) was last updated on Nov 28, 2023.

Text editor powered by tinymce.