SD card read writer
Example code of the Raspberry Pi Pico W connected to a Secure Digital (SD) card reader.
Connections
Description | Colour | Pico W | SD card |
---|---|---|---|
Power 3v | Orange | pin36 3V3(OUT) | 3.3v |
Ground | Black | pin3 GND | GND |
SPI | Blue | pin14 GP10 SPI1 SCK | SCK |
SPI | Yellow | pin15 GP11 SPI1 TX | MOSI |
SPI | Green | pin16 GP12 SPI1 RX | MISO |
SPI | White | pin17 GP13 SPI1 CSn | CS |


Photo






Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Raspberry Pi Pico W - with SD card | |
# Copyright (C) 2023 https://www.roboticboat.uk | |
# eeffa77d-1496-4d39-bcd8-66ca9011815e | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
# These Terms shall be governed and construed in accordance with the laws of | |
# England and Wales, without regard to its conflict of law provisions. | |
# | |
# User Interface https://thonny.org/ | |
# | |
# The sdcard library source is here | |
# https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py | |
# The sdcard.py needs to be also copied to the Pico W, so it can find it | |
import machine | |
import time | |
import uos | |
import sdcard | |
# Constants | |
SPI_SCK_PIN = const(10) | |
SPI_MOSI_PIN = const(11) | |
SPI_MISO_PIN = const(12) | |
SPI_CS_PIN = const(13) | |
# Update the User | |
print("Testing the SD card") | |
# Intialize the SPI network | |
spi = machine.SPI(1,baudrate=1000000, polarity=0, phase=0, bits=8, | |
firstbit=machine.SPI.MSB, | |
sck=machine.Pin(SPI_SCK_PIN), | |
mosi=machine.Pin(SPI_MOSI_PIN), | |
miso=machine.Pin(SPI_MISO_PIN)) | |
# SPI chip select pin | |
chipSelectPin = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) | |
# Initialize SD card object | |
sdobj = sdcard.SDCard(spi, chipSelectPin) | |
# Create a filesystem object that uses the FAT filesystem format | |
fsobj = uos.VfsFat(sdobj) | |
# Mount the filesystem object fsobj at the location | |
# in the VFS given by the mount_point string | |
uos.mount(fsobj, "/") | |
# List the top directory contents of the SD card | |
directoryList = uos.listdir("/") | |
# Update the User | |
print(directoryList) | |
# Create the output file (so we know it exists) | |
file = open("testfile.txt", 'w') | |
# Write to the file | |
file.write("First line of output file\r\n") | |
# Close the file | |
file.close() | |
# CTRL + C to stop the program running. | |
# Without the try: except it is very hard to stop the infinite loop | |
try: | |
# Infinite loop, like on the Arduino | |
while (True): | |
# Open a file for appending to the end of the file | |
# The file must already exist | |
file = open("testfile.txt", 'a') | |
# Append to the file | |
file.write("abcdefghijklmnopqrstuvwxyz1234567890\r\n") | |
# Close the file | |
file.close() | |
# Sleep for 1 second | |
time.sleep(1) | |
# Open file for reading (default) | |
file = open("testfile.txt", 'r') | |
# Read the whole file | |
data = file.read() | |
# Close the file | |
file.close() | |
# Update the User | |
print(data) | |
# Sleep for 1 second | |
time.sleep(1) | |
# Interrupt handing | |
# Capture the CTRL + C event | |
except (KeyboardInterrupt, SystemExit): | |
# Unmount the filesystem | |
uos.umount("/") | |
# Update the user | |
print ("Well done. Finished\n"); |