- Why do we need this RP2040_SD library
- Changelog
- Prerequisites
- Installation
- Examples
- Example ReadWrite
- Debug Terminal Output Samples
- 1. CardInfo on MBED RaspberryPi Pico
- 2. DumpFile on MBED RaspberryPi Pico
- 3. ListFiles on MBED RaspberryPi Pico
- 4. ReadWrite on MBED RaspberryPi Pico
- 5. Files on MBED RaspberryPi Pico
- 6. CardInfo on RASPBERRY_PI_PICO
- 7. DumpFile on RASPBERRY_PI_PICO
- 8. ListFiles on RASPBERRY_PI_PICO
- 9. ReadWrite on RASPBERRY_PI_PICO
- 10. Files on RASPBERRY_PI_PICO
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this RP2040_SD library
This is an Arduino library for RP2040-based SPI SD Cards.
It's based on and modified from :
Have a look at SdFat's README
SdFat Version 2 supports FAT16/FAT32 and exFAT SD cards. It is mostly backward compatible with SdFat Version 1 for FAT16/FAT32 cards.
exFAT supports files larger than 4GB so files sizes and positions are type uint64_t for classes that support exFAT.
exFAT has many features not available in FAT16/FAT32. exFAT has excellent support for contiguous files on flash devices and supports preallocation.
If the SD card is the only SPI device, use dedicated SPI mode. This can greatly improve performance.
This library was created as an effort to use SD Card while the arduino-pico
core still has issue SD card FILE_WRITE issue #214, which has been fixed from core v1.8.6.
It's better to use the built-in SD library for Earle Philhower's arduino-pico core v1.8.6+.
But if you need to write codes to be used in both Arduino-mbed RP2040 core and Earle Philhower's arduino-pico core without having to modify the code, you can have the option to use this library.
- RP2040-based boards such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. using either Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
Arduino IDE 1.8.19+
for Arduino.Arduino mbed_rp2040 core 3.4.1+
for Arduino (Use Arduino Board Manager) RP2040-based boards, such as Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc..Earle Philhower's arduino-pico core v2.6.3+
for RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc.
The best and easiest way is to use Arduino Library Manager
. Search for RP2040_SD, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to RP2040_SD page.
- Download the latest release
RP2040_SD-main.zip
. - Extract the zip file to
RP2040_SD-main
directory - Copy whole
RP2040_SD-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install RP2040_SD library by using Library Manager. Search for RP2040_SD in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
Example ReadWrite
1. File ReadWrite.ino
RP2040_SD/examples/ReadWrite/ReadWrite.ino
Lines 26 to 167 in c1c62cc
The following is the sample terminal output when running example CardInfo on MBED RaspberryPi Pico using Arduino-mbed RP2040 core
Starting SD Card CardInfo on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Wiring is correct and a card is present.
Card type: SDHC
Clusters: 982896
Blocks x Cluster: 64
Total Blocks: 62905344
Volume type is: FAT32
Volume size (Kb): 31452672
Volume size (Mb): 30715
Volume size (Gb): 30.00
Initialization done.
Files found on the card (name, date and size in bytes):
DATALOG.TXT 144
TEST.TXT 36
NEWTEST.TXT 36
NEWTEST1.TXT 18
MYDATA~1.TXT 0
NEWTEST2.TXT 54
NEWTEST3.TXT 54
NEWTEST4.TXT 72
NEWTEST0.TXT 297
NEWDATA.TXT 18
The following is the sample terminal output when running example DumpFile on MBED RaspberryPi Pico using Arduino-mbed RP2040 core
Starting SD Card DumpFile on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
====== Data ======
Testing RP2040 SD
====== Data ======
[SD] Closed " newdata.txt ", nfilecount = 0
The following is the sample terminal output when running example ListFiles on MBED RaspberryPi Pico using Arduino-mbed RP2040 core
Starting SD Card ListFiles on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
DATALOG.TXT 126
TEST.TXT 36
NEWTEST.TXT 36
NEWTEST1.TXT 18
MYDATA~1.TXT 0
NEWTEST2.TXT 54
NEWTEST3.TXT 54
NEWTEST4.TXT 72
NEWTEST0.TXT 297
Print Directory done!
writeData = Testing RP2040 SD
readData = Testing RP2040 SD
DATALOG.TXT 144
TEST.TXT 36
NEWTEST.TXT 36
NEWTEST1.TXT 18
MYDATA~1.TXT 0
NEWTEST2.TXT 54
NEWTEST3.TXT 54
NEWTEST4.TXT 72
NEWTEST0.TXT 297
NEWDATA.TXT 18
Print Directory done!
The following is the sample terminal output when running example ReadWrite on MBED RaspberryPi Pico using Arduino-mbed RP2040 core
Starting SD Card ReadWrite on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
initialization done.
Writing to newtest0.txt ==> Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount = 0
done.
Reading from newtest0.txt
===============
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount = 0
===============
The following is the sample terminal output when running example Files on MBED RaspberryPi Pico using Arduino-mbed RP2040 core
Starting SD Card Files on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
example.txt doesn't exist.
Creating example.txt
writeData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount = 0
readData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount = 0
example.txt exists.
Removing example.txt...
example.txt doesn't exist.
The following is the sample terminal output when running example CardInfo on RASPBERRY_PI_PICO using arduino-pico core
Starting SD Card CardInfo on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Wiring is correct and a card is present.
Card type: SDHC
Clusters: 1965597
Blocks x Cluster: 32
Total Blocks: 62899104
Volume type is: FAT32
Volume size (Kb): 31449552
Volume size (Mb): 30712
Volume size (Gb): 29.99
Initialization done.
Files found on the card (name, date and size in bytes):
DATALOG.TXT 267
DEMO.TXT 360960
CANADA~1.JPG 11156
TEST.TXT 72
NEWTEST4.TXT 132
NEWTEST0.TXT 330
NEWDATA.TXT 54
The following is the sample terminal output when running example DumpFile on RASPBERRY_PI_PICO using arduino-pico core
Starting SD Card DumpFile on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
====== Data ======
Testing RP2040 SD
====== Data ======
[SD] Closed " newdata.txt ", nfilecount = 0
The following is the sample terminal output when running example ListFiles on RASPBERRY_PI_PICO using arduino-pico core
Starting SD Card ListFiles on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
DATALOG.TXT 249
DEMO.TXT 82432
CANADA~1.JPG 11156
TEST.TXT 72
NEWTEST4.TXT 132
NEWTEST0.TXT 330
NEWDATA.TXT 36
Print Directory done!
writeData = Testing RP2040 SD
readData = Testing RP2040 SD
DATALOG.TXT 267
DEMO.TXT 82432
CANADA~1.JPG 11156
TEST.TXT 72
NEWTEST4.TXT 132
NEWTEST0.TXT 330
NEWDATA.TXT 54
Print Directory done!
The following is the sample terminal output when running example ReadWrite on RASPBERRY_PI_PICO using arduino-pico core
Starting SD Card ReadWrite on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
initialization done.
Writing to newtest0.txt ==> Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount = 0
done.
Reading from newtest0.txt
===============
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount = 0
===============
The following is the sample terminal output when running example Files on RASPBERRY_PI_PICO using arduino-pico core
Starting SD Card Files on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
example.txt doesn't exist.
Creating example.txt
writeData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount = 0
readData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount = 0
example.txt exists.
Removing example.txt...
example.txt doesn't exist.
Debug is enabled by default on Serial.
You can also change the debugging level (RP2040_SD_LOGLEVEL) from 0 to 4
// These define's must be placed at the beginning before #include "RP2040_SD.h"
// _RP2040_SD_LOGLEVEL_ from 0 to 4
#define _RP2040_SD_LOGLEVEL_ 0
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: RP2040_SD issues
- Search for bug and improvement.
- Basic SD-wrapper library for RP2040-based boards, such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
- Add Version String
- Add Table of Contents
- Add astyle using
allman
style. Restyle the library
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
- Based on and modified from the Bill Greiman's SdFat library to add support to RP2040-based boards using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
- Based on and modified from the Adafruit SD library to add support to RP2040-based boards using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
⭐️ Bill Greiman |
⭐️ Adafruit |
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
- (C) Copyright 2009 by William Greiman
- (C) Copyright 2010 SparkFun Electronics
- (C) Copyright 2021 by Khoi Hoang