Skip to content

Commit 90d617f

Browse files
committed
Adding arduino Metro RP2040 SD card example
Adding Arduino SD card example for Metro RP2040
1 parent 0206a1b commit 90d617f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 SPI bus as follows:
10+
** MISO - pin 20
11+
** MOSI - pin 19
12+
** CS - pin 23
13+
** SCK - pin 18
14+
15+
created Nov 2010
16+
by David A. Mellis
17+
modified 9 Apr 2012
18+
by Tom Igoe
19+
20+
This example code is in the public domain.
21+
22+
*/
23+
24+
// SPI0 pins for Metro RP2040
25+
// Pins connected to onboard SD card slot
26+
const int _MISO = 20;
27+
const int _MOSI = 19;
28+
const int _CS = 23;
29+
const int _SCK = 18;
30+
31+
#include <SPI.h>
32+
#include <SD.h>
33+
34+
File myFile;
35+
36+
void setup() {
37+
// Open serial communications and wait for port to open:
38+
Serial.begin(115200);
39+
40+
Serial.print("Initializing SD card...");
41+
42+
// Ensure the SPI pinout the SD card is connected to is configured properly
43+
SPI.setRX(_MISO);
44+
SPI.setTX(_MOSI);
45+
SPI.setSCK(_SCK);
46+
47+
if (!SD.begin(_CS)) {
48+
Serial.println("initialization failed!");
49+
return;
50+
}
51+
Serial.println("initialization done.");
52+
53+
// open the file. note that only one file can be open at a time,
54+
// so you have to close this one before opening another.
55+
myFile = SD.open("test.txt", FILE_WRITE);
56+
57+
// if the file opened okay, write to it:
58+
if (myFile) {
59+
Serial.print("Writing to test.txt...");
60+
myFile.println("hello metro rp2040!");
61+
// close the file:
62+
myFile.close();
63+
Serial.println("done.");
64+
} else {
65+
// if the file didn't open, print an error:
66+
Serial.println("error opening test.txt");
67+
}
68+
69+
// re-open the file for reading:
70+
myFile = SD.open("test.txt");
71+
if (myFile) {
72+
Serial.println("test.txt:");
73+
74+
// read from the file until there's nothing else in it:
75+
while (myFile.available()) {
76+
Serial.write(myFile.read());
77+
}
78+
// close the file:
79+
myFile.close();
80+
} else {
81+
// if the file didn't open, print an error:
82+
Serial.println("error opening test.txt");
83+
}
84+
}
85+
86+
void loop() {
87+
// nothing happens after setup
88+
}

0 commit comments

Comments
 (0)