Skip to content

Commit d453e1f

Browse files
authored
Merge pull request #2588 from adafruit/metro_rp2040_sd
Adding arduino Metro RP2040 SD card example
2 parents 726c835 + c803573 commit d453e1f

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Adafruit_Metro_RP2040/Arduino/Metro_RP2040_SD_Card_Test/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/*
5+
SD card read/write
6+
7+
This example shows how to read and write data to and from an SD card file
8+
The circuit:
9+
* SD card attached to SPI0 bus as follows:
10+
** MOSI - pin 19
11+
** MISO - pin 20
12+
** CLK - pin 18
13+
14+
created Nov 2010
15+
by David A. Mellis
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
modified 14 Feb 2023
19+
by Liz Clark
20+
21+
This example code is in the public domain.
22+
23+
*/
24+
25+
#include "SdFat.h"
26+
SdFat sd;
27+
28+
#define SD_FAT_TYPE 1
29+
30+
// default CS pin is 23 for Metro RP2040
31+
#define SD_CS_PIN 23
32+
33+
File32 myFile;
34+
35+
void setup() {
36+
// Open serial communications and wait for port to open:
37+
Serial.begin(115200);
38+
while (!Serial) {
39+
; // wait for serial port to connect. Needed for native USB port only
40+
}
41+
42+
43+
Serial.print("Initializing SD card...");
44+
45+
if (!sd.begin(SD_CS_PIN)) {
46+
Serial.println("initialization failed!");
47+
return;
48+
}
49+
Serial.println("initialization done.");
50+
51+
// open the file. note that only one file can be open at a time,
52+
// so you have to close this one before opening another.
53+
myFile.open("test.txt", FILE_WRITE);
54+
55+
// if the file opened okay, write to it:
56+
if (myFile) {
57+
Serial.print("Writing to test.txt...");
58+
myFile.println("testing 1, 2, 3.");
59+
myFile.println("hello metro rp2040!");
60+
// close the file:
61+
myFile.close();
62+
Serial.println("done.");
63+
} else {
64+
// if the file didn't open, print an error:
65+
Serial.println("error opening test.txt");
66+
}
67+
68+
// re-open the file for reading:
69+
myFile.open("test.txt");
70+
if (myFile) {
71+
Serial.println("test.txt:");
72+
73+
// read from the file until there's nothing else in it:
74+
while (myFile.available()) {
75+
Serial.write(myFile.read());
76+
}
77+
// close the file:
78+
myFile.close();
79+
} else {
80+
// if the file didn't open, print an error:
81+
Serial.println("error opening test.txt");
82+
}
83+
}
84+
85+
void loop() {
86+
// nothing happens after setup
87+
}

0 commit comments

Comments
 (0)