# Write the temperature to the temperature.txt file every 10 seconds.
temp_log.write('{0:.2f}\n'.format(temperature))
temp_log.flush()
# Blink the LED on every write...
led.value = True
time.sleep(1)
led.value = False
time.sleep(9)
except OSError as e:
delay = 0.5
if e.args[0] == 28:
delay = 0.15
while True:
led.value = not led.value
time.sleep(delay)
First you import the necessary modules to make them available to your code, and you
set up the LED.
Next you have a
try
states of the board: read/write, read-only, or filesystem full. The code in the
block will run if the filesystem is writable by CircuitPython. The code in the
block will run if the filesystem is read-only to CircuitPython OR if the filesystem is full.
Under the
, you open a temperature.txt log file. If it is the first time, it will create
try
the file. For all subsequent times, it opens the file and appends data. Inside the loop,
you get the microcontroller temperature value and assign it to a
variable. Then, you write the temperature value to the log file, followed by clearing
the buffer for the next time through the loop. The temperature data is limited to two
decimal points to save space for more data. Finally, you turn the LED on for one
second, and then turn it off for the next nine seconds. Essentially, you blink the LED
for one second every time the temperature is logged to the file which happens every
ten seconds.
Next you
except
create, open or write to a file on a filesystem that is read-only to CircuitPython. If any
other than 28 is raised (e.g. 30), the
OSError
filesystem fills up, CircuitPython raises
is raised, the
delay
the duration of the
blinking the LED at the speed of the
©Adafruit Industries
# ...for one second.
# Then turn it off...
# ...for the other 9 seconds.
# When the filesystem is NOT writable by CircuitPython...
# ...blink the LED every half second.
# If the file system is full...
# ...blink the LED every 0.15 seconds!
/
block, which is used to handle the three potential
except
an
. An
OSError
OSError
is set to 0.15 seconds. Inside the loop, the LED is turned on for
, and turned off for the duration of the
delay
delay
number 30 is raised when trying to
is set to 0.5 seconds. If the
delay
number 28. If
OSError
.
try
except
temperature
number 28
OSError
, effectively
delay
Page 168 of 263
Need help?
Do you have a question about the ESP32-S3 and is the answer not in the manual?