Raspberry Pi Pico - Micro SD Card

This tutorial explains how to connect and use a Micro SD Card with the Raspberry Pi Pico running MicroPython. The topics covered include:

Raspberry Pi Pico Micro SD Card

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×Optionally, MicroSD to SD Memory Card Adapter
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Micro SD Card Module

The Micro SD Card Module is a small breakout board that lets the Raspberry Pi Pico access a Micro SD Card through the SPI bus. It handles the electrical level shifting between the Pico and the card.

Pinout

Micro SD Card Module Pinout

This module provides 6 connection pins:

  • VCC: supply voltage — wire to the VBUS (5V) output on the Raspberry Pi Pico.
  • GND: ground reference — wire to any GND pin on the Pico.
  • MISO: data output from the SD card — wire to the Pico's MISO pin.
  • MOSI: data input to the SD card — wire to the Pico's MOSI pin.
  • SCK: SPI clock signal — wire to the Pico's SCK pin.
  • SS (CS): chip select — wire to the GPIO pin specified in your MicroPython code.

Preparation

  • Use a USB 3.0 SD Card Reader to plug the Micro SD Card into your computer.
  • Confirm the card is formatted as FAT16 or FAT32 (search online for formatting instructions if necessary).

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

The wiring table below shows how to connect the Micro SD Card Module to the Raspberry Pi Pico:

Micro SD Card Module Raspberry Pi Pico
VCC VBUS (5V)
GND GND
MISO GP4
MOSI GP3
SCK GP2
CS GP5

※ NOTE THAT:

When using a shield that already includes a Micro SD Card slot (for example, an Ethernet shield), no additional SD Card Module is required — just slide the card straight into the shield's built-in slot.

Raspberry Pi Pico - Creating or Opening a File on Micro SD Card

MicroPython Code

Detailed Instructions

Follow the steps below to set up and run the code:

  • Verify that Thonny IDE is installed on your PC.
  • Make sure MicroPython firmware has been flashed onto your Raspberry Pi Pico.
  • First-time users should consult the Raspberry Pi Pico - Getting Started guide for setup details.
  • Assemble the circuit following the wiring diagram above.
  • Plug the Raspberry Pi Pico into your computer via USB cable.
  • Open Thonny IDE.
  • Go to Tools Options, then in the Interpreter tab pick MicroPython (Raspberry Pi Pico).
  • Check that the correct serial port is selected — Thonny usually finds it automatically, but manual selection may be needed (for example COM3 on Windows or /dev/ttyACM0 on Linux).
  • Paste the code above into the Thonny editor window.
  • Save the file to the Raspberry Pi Pico:
    • Press Ctrl+S or click Save.
    • When prompted, select the Raspberry Pi Pico location.
    • Name the file main.py.
  • Hit the green Run button (or press F5). The script will start executing.
  • Review the output in the Shell panel at the bottom of Thonny.

Saving your script as main.py in the Pico's root directory means it will launch automatically whenever the board powers up or resets — ideal for standalone projects. Scripts saved under a different name must be started manually through the Thonny Shell.

  • Slot the Micro SD Card into the SD Card module.
  • Wire the SD Card module to the Raspberry Pi Pico following the diagram.
  • Open the Shell panel in Thonny IDE.
  • Install the sdcard library on the Raspberry Pi Pico via Thonny IDE:
    • Go to Tools Manage packages in Thonny IDE.
    • Type sdcard in the search box and install the micropython-sdcard package.
    Raspberry Pi Pico SD Card library
    • Paste the code below into the Thonny editor.
    """ This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card """ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit # Check if the file exists file_path = "/sd/pico.txt" if file_path.replace("/sd/", "") not in os.listdir("/sd"): print("pico.txt doesn't exist. Creating pico.txt file...") # Create a new file by opening it and immediately closing it f = open(file_path, "w") f.close() # Recheck if file is created or not if file_path.replace("/sd/", "") in os.listdir("/sd"): print("pico.txt exists on SD Card.") else: print("pico.txt doesn't exist on SD Card.") os.umount("/sd")
    • Click Run to upload and execute the code on the Raspberry Pi Pico.
    • The Shell output after the first execution:
    Shell x
    >>> %Run -c $EDITOR_CONTENT
    MPY: soft reboot SD Card is ready pico.txt doesn't exist. Creating pico.txt file... pico.txt exists on SD Card.
    MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
    • On subsequent runs, the output will be:
    Shell x
    >>> %Run -c $EDITOR_CONTENT
    MPY: soft reboot SD Card is ready pico.txt exists on SD Card.
    MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

    ※ NOTE THAT:

    The Shell might not display anything if you open it after the script has already finished.

    • Eject the Micro SD Card from the module.
    • Insert the card into a USB SD Card reader.
    • Connect the reader to your computer.
    • Verify that the file appears on the card.

Raspberry Pi Pico - Writing Data and Reading It Back from Micro SD Card

The MicroPython code below performs two tasks:

  • Appends text into a file on the SD card
  • Opens the file and prints each character to the Shell
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card """ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD CARD INITIALIZED.") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Open file for writing (append mode) try: f = open(file_path, "a") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading try: f = open(file_path, "r") while True: ch = f.read(1) # read characters one by one from Micro SD Card if not ch: break print(ch, end="") # print the character to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) os.umount("/sd")
  • The Shell displays the file contents:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Created by newbiely.com Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ NOTE THAT:

Each time the script runs, new text is added at the end of the file. After resetting the Pico and running again, the Shell will show duplicated lines like:

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Created by newbiely.com Learn Raspberry Pi Pico and SD Card Created by newbiely.com Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

To inspect the file content externally, remove the Micro SD Card from the module and check it on your PC using a USB SD Card reader.

Raspberry Pi Pico - Processing a File Line by Line from Micro SD Card

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card """ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Open file for writing (append mode) try: f = open(file_path, "a") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading line-by-line try: f = open(file_path, "r") line_count = 0 while True: line = f.readline() # read line-by-line from Micro SD Card if not line: break line_count += 1 print("Line {}: {}".format(line_count, line.strip())) # print the line to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e)
  • Shell output:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready Line 1: Created by newbiely.com Line 2: Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ NOTE THAT:

You may see additional lines if earlier data was not cleared from the file.

Raspberry Pi Pico - Replacing File Content on Micro SD Card

Because data is appended by default, the simplest way to replace a file's content is to delete the file first and then write a new one under the same name.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card """ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Delete the file if it exists (to overwrite) try: os.remove(file_path) except OSError: pass # Create new file by opening file for writing try: f = open(file_path, "w") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading try: f = open(file_path, "r") while True: ch = f.read(1) # read characters one by one from Micro SD Card if not ch: break print(ch, end="") # print the character to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) os.umount("/sd")
  • Shell output:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready Created by newbiely.com Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
  • Reset the Raspberry Pi Pico.
  • Observe the Shell to confirm whether the content was replaced rather than appended.

Alternatively, eject the card and inspect the file on your computer with a USB SD Card reader.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!