PicoSeries PR

How to read and write files on RaspberryPi Pico series ~ MicroPython ~

記事内に商品プロモーションを含む場合があります

Introduction

This blog post explains how to use MicroPython to read and write files on the Raspberry Pi Pico series.

The MicroPython firmware allows you to use a virtual file system and powerful Python functions to read and write files just like you would on a regular computer (CPython).

No special configuration or equipment is required, so we hope you will find it useful for your projects, including log output and configuration files.

Not necessarily pico2

The Pico2 is used in this blog post, but the exact same wiring and programme can be used with the Pico and Pico W.
If you don’t have a Pico2 yet, please try it out with your Pico series.

Environment

The environment used in this blog post is the following.

environment Version etc. Comments
Developing PC Windows11 Windows 10’s fine.
Language MicroPython  Ver.1.24.0
Development Environment Thonny  Ver.4.1.4
board RaspberryPi Pico2 Can also use Pico, PicoW

Connection with RaspberryPi Pico series

The Pico series can be used by itself.

Parts in use

Ready-to-use kits are also available.

Ready to use kits with soldered headers, USB cable and pin layout chart are also available.

Programme overview

  • Write and save files
  • Loading Files

Results of running the program

The following is the result of running the program described below.

A text file named “pico.txt” is created inside Pico, and its contents are displayed as read into Thonny’s shell.

ファイル書き込みをするプログラムの実行結果を説明する画像
Result

If the file does not appear when you run it,
Try clicking the “Refresh” button in the settings.

プログラムを実行しても、ファイルが表示されない場合の対応方法を説明する画像
If a file is not displayed

Entire code

The full text of the Code is as follows. The details are explained in the section “Key Points of the Code” below.

import os

# Specify a filename.
fileName = "pico.txt"

# Open the file in write mode
f = open( fileName,  "w")

# Write content
f.write("Raspberry\n")
f.write("Pi\n")
f.write("Pico 2\n")

#  Closes the file when writing is complete.
f.close()

# Reopen the file in read mode
f = open(fileName, "r")

# Line by line reading and display.
print( f.readline() )
print( f.readline() )
print( f.readline() )

# Closes file after writing.
f.close()

To run the program with the Pico series alone (without Thonny), save the program as “main.py” in the Pico series main unit.

Key Points of the Code

Write and save files

The open function is used to write and save a file.

By specifying “w” for write and a file name as arguments, a text file is opened in “write mode” (if the file does not exist, a new file is created).

After opening, specify the string to write as an argument to the write function.
Since line feeds are not inserted, specify the string with “\n” at the end of the sentence or other places where you want to insert line feeds.

Calling the close function finishes writing and saves the file.

# Open the file in write mode
f = open( fileName,  "w")

# Write content
f.write("Raspberry\n")
f.write("Pi\n")
f.write("Pico 2\n")

#  Closes the file when writing is complete.
f.close()

Loading Files

The open function is also used to read a file.

Specifying “r” for read and the name of the file will open a text file in read mode.

Once opened, the text in the file can be read line by line using the readline function.

As with writing, when you are done reading, close the file with the close function.

# Reopen the file in read mode
f = open(fileName, "r")

# Line by line reading and display.
print( f.readline() )
print( f.readline() )
print( f.readline() )

# Closes file after writing.
f.close()

Finally

This blog post explains how to use MicroPython to read and write files on the Raspberry Pi Pico series.

As mentioned above, MicroPython formware allows you to read and write files on the Pico just like you would on a regular computer.

Once you learn file operations, you can apply them to a wide range of applications, such as logging data and creating configuration files.

It can also improve the completeness and debuggability of your products, so please give it a try.

I would be happy to help.

質問・要望 大歓迎です

「こんな解説記事作って」「こんなことがしたいけど、〇〇で困ってる」など、コメント欄で教えてください。 質問・要望に、中の人ができる限り対応します。

使えたよ・設定できたよの一言コメントも大歓迎。気軽に足跡を残してみてください。記事を紹介したい方はブログ、SNSにバシバシ貼ってもらってOKです。

ABOUT ME
えす
現役のソフトウェアエンジニアです。 C++ C# Python を使ってます。10年ちょい設計/開発部門にいましたが、今はQAエンジニアっぽいことをしています。
Index