Skip to content

Commit 8706ad6

Browse files
authored
Merge pull request #2574 from adafruit/grand_central_sd_card
Adding arduino micro sd card example
2 parents 436fc7a + a74f973 commit 8706ad6

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Grand_Central_M4_Arduino_SD_Card/.none.test.only

Whitespace-only changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
** MOSI - pin 64
11+
** MISO - pin 66
12+
** CLK - pin 65
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 <SPI.h>
26+
//#include <SD.h>
27+
#include "SdFat.h"
28+
SdFat SD;
29+
30+
#define SD_FAT_TYPE 3
31+
32+
// default CS pin is SDCARD_SS_PIN (83) for Grand Central M4
33+
#define SD_CS_PIN SDCARD_SS_PIN
34+
35+
File myFile;
36+
37+
void setup() {
38+
// Open serial communications and wait for port to open:
39+
Serial.begin(115200);
40+
while (!Serial) {
41+
; // wait for serial port to connect. Needed for native USB port only
42+
}
43+
44+
45+
Serial.print("Initializing SD card...");
46+
47+
if (!SD.begin(SD_CS_PIN)) {
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("testing 1, 2, 3.");
61+
myFile.println("hello grand central m4!");
62+
// close the file:
63+
myFile.close();
64+
Serial.println("done.");
65+
} else {
66+
// if the file didn't open, print an error:
67+
Serial.println("error opening test.txt");
68+
}
69+
70+
// re-open the file for reading:
71+
myFile = SD.open("test.txt");
72+
if (myFile) {
73+
Serial.println("test.txt:");
74+
75+
// read from the file until there's nothing else in it:
76+
while (myFile.available()) {
77+
Serial.write(myFile.read());
78+
}
79+
// close the file:
80+
myFile.close();
81+
} else {
82+
// if the file didn't open, print an error:
83+
Serial.println("error opening test.txt");
84+
}
85+
}
86+
87+
void loop() {
88+
// nothing happens after setup
89+
}

0 commit comments

Comments
 (0)