In this video lesson, We show you how to log sensor data on your Raspberry Pi Pico W. We will start with simple read and write functions, and then show you how to write and read files line by line, and keep a log of your sensor data.
Our first program is very simple, and just shows how to write a line of data, and read a line of data the the Pico’s flash memory. Here is the code we used in the video for the first simple example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # This code writes three sensor readings as a comma-separated string to log.txt # on the Pico W, then reads the file back and splits the string into a list. # Purpose: Demonstrate basic file writing and reading in MicroPython using log.txt. # Writing comma-delimited data to a file sensorData = "25.5,26.0,24.8" # Example temperature readings from three sensors with open('log.txt', 'w') as file: # Open log.txt in write mode ('w') file.write(sensorData) # Write the comma-separated string to the file print("Data written to log.txt") # Confirm data was written # Reading comma-delimited data with open('log.txt', 'r') as file: # Open log.txt in read mode ('r') content = file.read() # Read the entire file as a string print("Raw data:", content) # Print the raw string # Parse into a list values = content.split(',') # Split the string at commas to create a list print("Parsed values:", values) # Print the resulting list |
In the second example, we show greater complexity by writing and reading a file, line by line. Each line contains comma delimited sensor data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # This code writes three sets of sensor readings to log.txt on the Pico W, # each set as a comma-separated line, overwriting previous content, then reads # the file back line by line, splitting each line into a list. # Purpose: Show how to log multiple data sets to log.txt and read them one line at a time. # Writing one line at a time readingsOne = ["25.5", "26.0", "24.8"] # First set of sensor readings readingsTwo = ["12.6", "62.5", "33.4"] # Second set of sensor readings readingsThree = ["19.8", "20.1", "21.3"] # Third set of sensor readings allReadings = [readingsOne, readingsTwo, readingsThree] # Combine into a list of lists with open('log.txt', 'w') as file: # Open log.txt in write mode ('w') for readingSet in allReadings: # Loop through each set of readings # ','.join(readingSet) converts the list (e.g., ["25.5", "26.0", "24.8"]) # into a single string "25.5,26.0,24.8" with commas between values. # We need this because file.write() requires a string, not a list. line = ','.join(readingSet) file.write(line + '\n') # Write the line to the file with a newline print("Data written to log.txt") # Confirm data was written # Reading one line at a time with open('log.txt', 'r') as file: # Open log.txt in read mode ('r') print("Reading line by line:") for line in file: # Loop through each line in the file # line.strip() removes leading/trailing whitespace, including '\n' # .split(',') splits the line at commas to create a list of values values = line.strip().split(',') print("Sensor readings:", values) # Print the list of readings |
In these first two examples, when we open the file as ‘w’, the existing data is erased and a new file is created. In the example below, we open an existing file, and show how to append new data to the existing file without erasing the old data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # This code appends a new set of sensor readings to log.txt on the Pico W # as a comma-separated line, checks if the file exists, and reads the file # line by line to verify the append. # Purpose: Demonstrate appending to log.txt, verifying file existence, and checking contents. import os # Import os module for file system operations # Appending a new set of readings to the file newReadings = ["30.1", "29.8", "31.0"] # New set of sensor readings try: with open('log.txt', 'a') as file: # Open log.txt in append mode ('a') # ','.join(newReadings) converts the list (e.g., ["30.1", "29.8", "31.0"]) # into a single string "30.1,29.8,31.0" with commas between values. # We need this because file.write() requires a string, not a list. line = ','.join(newReadings) file.write(line + '\n') # Append the line with a newline print("Appended new readings to log.txt") # Confirm data was appended except OSError as e: print("Error appending to file:", e) # Handle errors like full flash # Read and print file to verify append try: with open('log.txt', 'r') as file: # Open log.txt in read mode ('r') print("Verifying log.txt contents line by line:") for line in file: # Loop through each line in the file # line.strip() removes leading/trailing whitespace, including '\n' # .split(',') splits the line at commas to create a list of values values = line.strip().split(',') print("Sensor readings:", values) # Print the list of readings except OSError as e: print("Error reading file:", e) # Handle errors like file not found |