Skip to content

Commit 85dbcca

Browse files
committed
Updated SD card examples for clarity and to reflect OSHWA recommendations on pin names
Updated examples to make examples more uniform and clearer, and to reflect [OSHWA recommendation on pin naming](https://www.oshwa.org/2020/06/29/a-resolution-to-redefine-spi-pin-names/)
1 parent 0a0aaeb commit 85dbcca

File tree

7 files changed

+152
-131
lines changed

7 files changed

+152
-131
lines changed

examples/CardInfo/CardInfo.ino

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
This example shows how use the utility libraries on which the'
55
SD library is based in order to get info about your SD card.
66
Very useful for testing a card when you're not sure whether its working or not.
7-
7+
Pin numbers reflect the default SPI pins for Uno and Nano models
88
The circuit:
99
SD card attached to SPI bus as follows:
10-
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
11-
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
10+
** SDO - pin 11 on Arduino Uno/Duemilanove/Diecimila
11+
** SDI - pin 12 on Arduino Uno/Duemilanove/Diecimila
1212
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
1313
** CS - depends on your SD card shield or module.
14-
Pin 4 used here for consistency with other Arduino examples
15-
14+
Pin 10 used here for consistency with other Arduino examples
1615
1716
created 28 Mar 2011
1817
by Limor Fried
19-
modified 9 Apr 2012
18+
modified 24 July 2020
2019
by Tom Igoe
2120
*/
2221
// include the SD library:
@@ -29,11 +28,12 @@ SdVolume volume;
2928
SdFile root;
3029

3130
// change this to match your SD shield or module;
31+
// Default SPI on Uno and Nano: pin 10
3232
// Arduino Ethernet shield: pin 4
3333
// Adafruit SD shields and modules: pin 10
3434
// Sparkfun SD shield: pin 8
3535
// MKRZero SD: SDCARD_SS_PIN
36-
const int chipSelect = 4;
36+
const int chipSelect = 10;
3737

3838
void setup() {
3939
// Open serial communications and wait for port to open:

examples/Datalogger/Datalogger.ino

+19-24
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
SD card datalogger
33
44
This example shows how to log data from three analog sensors
5-
to an SD card using the SD library.
5+
to an SD card using the SD library. Pin numbers reflect the default
6+
SPI pins for Uno and Nano models
67
78
The circuit:
89
analog sensors on analog ins 0, 1, and 2
910
SD card attached to SPI bus as follows:
10-
** MOSI - pin 11
11-
** MISO - pin 12
11+
** SDO - pin 11
12+
** SDI - pin 12
1213
** CLK - pin 13
13-
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
14+
** CS - depends on your SD card shield or module.
15+
Pin 10 used here for consistency with other Arduino examples
16+
(for MKRZero SD: SDCARD_SS_PIN)
1417
1518
created 24 Nov 2010
16-
modified 9 Apr 2012
19+
modified 24 July 2020
1720
by Tom Igoe
1821
1922
This example code is in the public domain.
@@ -23,25 +26,26 @@
2326
#include <SPI.h>
2427
#include <SD.h>
2528

26-
const int chipSelect = 4;
29+
const int chipSelect = 10;
2730

2831
void setup() {
2932
// Open serial communications and wait for port to open:
3033
Serial.begin(9600);
31-
while (!Serial) {
32-
; // wait for serial port to connect. Needed for native USB port only
33-
}
34-
34+
// wait for Serial Monitor to connect. Needed for native USB port boards only:
35+
while (!Serial);
3536

3637
Serial.print("Initializing SD card...");
3738

38-
// see if the card is present and can be initialized:
3939
if (!SD.begin(chipSelect)) {
40-
Serial.println("Card failed, or not present");
41-
// don't do anything more:
42-
while (1);
40+
Serial.println("initialization failed. Things to check:");
41+
Serial.println("1. is a card inserted?");
42+
Serial.println("2. is your wiring correct?");
43+
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
44+
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
45+
while (true);
4346
}
44-
Serial.println("card initialized.");
47+
48+
Serial.println("initialization done.");
4549
}
4650

4751
void loop() {
@@ -73,12 +77,3 @@ void loop() {
7377
Serial.println("error opening datalog.txt");
7478
}
7579
}
76-
77-
78-
79-
80-
81-
82-
83-
84-

examples/DumpFile/DumpFile.ino

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,46 @@
33
44
This example shows how to read a file from the SD card using the
55
SD library and send it over the serial port.
6+
Pin numbers reflect the default SPI pins for Uno and Nano models.
67
78
The circuit:
89
SD card attached to SPI bus as follows:
9-
** MOSI - pin 11
10-
** MISO - pin 12
10+
** SDO - pin 11
11+
** SDI - pin 12
1112
** CLK - pin 13
12-
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
13+
** CS - depends on your SD card shield or module.
14+
Pin 10 used here for consistency with other Arduino examples
15+
(for MKRZero SD: SDCARD_SS_PIN)
1316
1417
created 22 December 2010
1518
by Limor Fried
1619
modified 9 Apr 2012
1720
by Tom Igoe
1821
1922
This example code is in the public domain.
20-
2123
*/
22-
23-
#include <SPI.h>
2424
#include <SD.h>
2525

26-
const int chipSelect = 4;
26+
const int chipSelect = 10;
2727

2828
void setup() {
2929
// Open serial communications and wait for port to open:
3030
Serial.begin(9600);
31-
while (!Serial) {
32-
; // wait for serial port to connect. Needed for native USB port only
33-
}
34-
31+
// wait for Serial Monitor to connect. Needed for native USB port boards only:
32+
while (!Serial);
3533

3634
Serial.print("Initializing SD card...");
3735

38-
// see if the card is present and can be initialized:
3936
if (!SD.begin(chipSelect)) {
40-
Serial.println("Card failed, or not present");
41-
// don't do anything more:
42-
while (1);
37+
Serial.println("initialization failed. Things to check:");
38+
Serial.println("1. is a card inserted?");
39+
Serial.println("2. is your wiring correct?");
40+
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
41+
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
42+
while (true);
4343
}
44-
Serial.println("card initialized.");
44+
45+
Serial.println("initialization done.");
4546

4647
// open the file. note that only one file can be open at a time,
4748
// so you have to close this one before opening another.
@@ -62,4 +63,3 @@ void setup() {
6263

6364
void loop() {
6465
}
65-

examples/Files/Files.ino

+12-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22
SD card basic file example
33
44
This example shows how to create and destroy an SD card file
5-
The circuit:
5+
The circuit. Pin numbers reflect the default
6+
SPI pins for Uno and Nano models:
67
SD card attached to SPI bus as follows:
7-
** MOSI - pin 11
8-
** MISO - pin 12
8+
** SDO - pin 11
9+
** SDI - pin 12
910
** CLK - pin 13
10-
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
11+
** CS - depends on your SD card shield or module.
12+
Pin 10 used here for consistency with other Arduino examples
13+
(for MKRZero SD: SDCARD_SS_PIN)
1114
1215
created Nov 2010
1316
by David A. Mellis
14-
modified 9 Apr 2012
17+
modified 24 July 2020
1518
by Tom Igoe
1619
1720
This example code is in the public domain.
18-
1921
*/
20-
#include <SPI.h>
2122
#include <SD.h>
2223

24+
const int chipSelect = 10;
2325
File myFile;
2426

2527
void setup() {
2628
// Open serial communications and wait for port to open:
2729
Serial.begin(9600);
28-
while (!Serial) {
29-
; // wait for serial port to connect. Needed for native USB port only
30-
}
31-
30+
// wait for Serial Monitor to connect. Needed for native USB port boards only:
31+
while (!Serial);
3232

3333
Serial.print("Initializing SD card...");
3434

35-
if (!SD.begin(4)) {
35+
if (!SD.begin(10)) {
3636
Serial.println("initialization failed!");
3737
while (1);
3838
}
@@ -70,6 +70,3 @@ void setup() {
7070
void loop() {
7171
// nothing happens after setup finishes.
7272
}
73-
74-
75-

examples/NonBlockingWrite/NonBlockingWrite.ino

+64-37
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,116 @@
33
44
This example demonstrates how to perform non-blocking writes
55
to a file on a SD card. The file will contain the current millis()
6-
value every 10ms. If the SD card is busy, the data will be buffered
6+
value every 10ms. If the SD card is busy, the data will be dataBuffered
77
in order to not block the sketch.
88
9+
If data is successfully written, the built in LED will flash. After a few
10+
seconds, check the card for a file called datalog.txt
11+
912
NOTE: myFile.availableForWrite() will automatically sync the
1013
file contents as needed. You may lose some unsynced data
1114
still if myFile.sync() or myFile.close() is not called.
1215
16+
Pin numbers reflect the default SPI pins for Uno and Nano models
17+
Updated for clarity and uniformity with other examples
18+
1319
The circuit:
14-
- Arduino MKR Zero board
15-
- micro SD card attached
20+
analog sensors on analog ins 0, 1, and 2
21+
SD card attached to SPI bus as follows:
22+
** SDO - pin 11
23+
** SDI - pin 12
24+
** CLK - pin 13
25+
** CS - depends on your SD card shield or module.
26+
Pin 10 used here for consistency with other Arduino examples
27+
(for MKRZero SD: SDCARD_SS_PIN)
28+
29+
modified 24 July 2020
30+
by Tom Igoe
1631
1732
This example code is in the public domain.
1833
*/
19-
2034
#include <SD.h>
2135

36+
const int chipSelect = 10;
37+
2238
// file name to use for writing
23-
const char filename[] = "demo.txt";
39+
const char filename[] = "datalog.txt";
2440

2541
// File object to represent file
26-
File txtFile;
27-
42+
File myFile;
2843
// string to buffer output
29-
String buffer;
30-
44+
String dataBuffer;
45+
// last time data was written to card:
3146
unsigned long lastMillis = 0;
3247

3348
void setup() {
49+
// Open serial communications and wait for port to open:
3450
Serial.begin(9600);
35-
while (!Serial);
36-
37-
// reserve 1kB for String used as a buffer
38-
buffer.reserve(1024);
51+
// reserve 1kB for String used as a dataBuffer
52+
dataBuffer.reserve(1024);
3953

4054
// set LED pin to output, used to blink when writing
4155
pinMode(LED_BUILTIN, OUTPUT);
4256

43-
// init the SD card
44-
if (!SD.begin()) {
45-
Serial.println("Card failed, or not present");
46-
// don't do anything more:
47-
while (1);
57+
// wait for Serial Monitor to connect. Needed for native USB port boards only:
58+
while (!Serial);
59+
60+
Serial.print("Initializing SD card...");
61+
62+
if (!SD.begin(chipSelect)) {
63+
Serial.println("initialization failed. Things to check:");
64+
Serial.println("1. is a card inserted?");
65+
Serial.println("2. is your wiring correct?");
66+
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
67+
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
68+
while (true);
4869
}
4970

71+
Serial.println("initialization done.");
72+
5073
// If you want to start from an empty file,
5174
// uncomment the next line:
52-
// SD.remove(filename);
53-
75+
// SD.remove(filename);
5476
// try to open the file for writing
55-
txtFile = SD.open(filename, FILE_WRITE);
56-
if (!txtFile) {
77+
78+
myFile = SD.open(filename, FILE_WRITE);
79+
if (!myFile) {
5780
Serial.print("error opening ");
5881
Serial.println(filename);
59-
while (1);
82+
while (true);
6083
}
6184

6285
// add some new lines to start
63-
txtFile.println();
64-
txtFile.println("Hello World!");
86+
myFile.println();
87+
myFile.println("Hello World!");
88+
Serial.println("Starting to write to file...");
6589
}
6690

6791
void loop() {
6892
// check if it's been over 10 ms since the last line added
6993
unsigned long now = millis();
7094
if ((now - lastMillis) >= 10) {
71-
// add a new line to the buffer
72-
buffer += "Hello ";
73-
buffer += now;
74-
buffer += "\r\n";
75-
95+
// add a new line to the dataBuffer
96+
dataBuffer += "Hello ";
97+
dataBuffer += now;
98+
dataBuffer += "\r\n";
99+
// print the buffer length. This will change depending on when
100+
// data is actually written to the SD card file:
101+
Serial.print("Unsaved data buffer length (in bytes): ");
102+
Serial.println(dataBuffer.length());
103+
// note the time that the last line was added to the string
76104
lastMillis = now;
77105
}
78106

79107
// check if the SD card is available to write data without blocking
80-
// and if the buffered data is enough for the full chunk size
81-
unsigned int chunkSize = txtFile.availableForWrite();
82-
if (chunkSize && buffer.length() >= chunkSize) {
108+
// and if the dataBuffered data is enough for the full chunk size
109+
unsigned int chunkSize = myFile.availableForWrite();
110+
if (chunkSize && dataBuffer.length() >= chunkSize) {
83111
// write to file and blink LED
84112
digitalWrite(LED_BUILTIN, HIGH);
85-
txtFile.write(buffer.c_str(), chunkSize);
113+
myFile.write(dataBuffer.c_str(), chunkSize);
86114
digitalWrite(LED_BUILTIN, LOW);
87-
88-
// remove written data from buffer
89-
buffer.remove(0, chunkSize);
115+
// remove written data from dataBuffer
116+
dataBuffer.remove(0, chunkSize);
90117
}
91118
}

0 commit comments

Comments
 (0)