Skip to content

Commit 15bace7

Browse files
committed
SD library
1 parent 9c53d73 commit 15bace7

23 files changed

+5927
-0
lines changed

Diff for: libraries/SD/README.adoc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
= SD Library for Arduino =
2+
3+
The SD library allows for reading from and writing to SD cards.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/SD
7+
8+
== License ==
9+
10+
Copyright (C) 2009 by William Greiman
11+
Copyright (c) 2010 SparkFun Electronics
12+
13+
This program is free software: you can redistribute it and/or modify
14+
it under the terms of the GNU General Public License as published by
15+
the Free Software Foundation, either version 3 of the License, or
16+
(at your option) any later version.
17+
18+
This program is distributed in the hope that it will be useful,
19+
but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
GNU General Public License for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with this program. If not, see <http://www.gnu.org/licenses/>.

Diff for: libraries/SD/examples/CardInfo/CardInfo.ino

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
SD card test
3+
4+
This example shows how use the utility libraries on which the'
5+
SD library is based in order to get info about your SD card.
6+
Very useful for testing a card when you're not sure whether its working or not.
7+
8+
The circuit:
9+
* 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
12+
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
13+
** CS - depends on your SD card shield or module.
14+
Pin 4 used here for consistency with other Arduino examples
15+
16+
17+
created 28 Mar 2011
18+
by Limor Fried
19+
modified 9 Apr 2012
20+
by Tom Igoe
21+
*/
22+
// include the SD library:
23+
#include <SPI.h>
24+
#include <SD.h>
25+
26+
// set up variables using the SD utility library functions:
27+
Sd2Card card;
28+
SdVolume volume;
29+
SdFile root;
30+
31+
// change this to match your SD shield or module;
32+
// Arduino Ethernet shield: pin 4
33+
// Adafruit SD shields and modules: pin 10
34+
// Sparkfun SD shield: pin 8
35+
const int chipSelect = 4;
36+
37+
void setup()
38+
{
39+
// Open serial communications and wait for port to open:
40+
Serial.begin(9600);
41+
while (!Serial) {
42+
; // wait for serial port to connect. Needed for Leonardo only
43+
}
44+
45+
46+
Serial.print("\nInitializing SD card...");
47+
48+
// we'll use the initialization code from the utility libraries
49+
// since we're just testing if the card is working!
50+
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
51+
Serial.println("initialization failed. Things to check:");
52+
Serial.println("* is a card inserted?");
53+
Serial.println("* is your wiring correct?");
54+
Serial.println("* did you change the chipSelect pin to match your shield or module?");
55+
return;
56+
} else {
57+
Serial.println("Wiring is correct and a card is present.");
58+
}
59+
60+
// print the type of card
61+
Serial.print("\nCard type: ");
62+
switch (card.type()) {
63+
case SD_CARD_TYPE_SD1:
64+
Serial.println("SD1");
65+
break;
66+
case SD_CARD_TYPE_SD2:
67+
Serial.println("SD2");
68+
break;
69+
case SD_CARD_TYPE_SDHC:
70+
Serial.println("SDHC");
71+
break;
72+
default:
73+
Serial.println("Unknown");
74+
}
75+
76+
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
77+
if (!volume.init(card)) {
78+
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
79+
return;
80+
}
81+
82+
83+
// print the type and size of the first FAT-type volume
84+
uint32_t volumesize;
85+
Serial.print("\nVolume type is FAT");
86+
Serial.println(volume.fatType(), DEC);
87+
Serial.println();
88+
89+
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
90+
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
91+
volumesize *= 512; // SD card blocks are always 512 bytes
92+
Serial.print("Volume size (bytes): ");
93+
Serial.println(volumesize);
94+
Serial.print("Volume size (Kbytes): ");
95+
volumesize /= 1024;
96+
Serial.println(volumesize);
97+
Serial.print("Volume size (Mbytes): ");
98+
volumesize /= 1024;
99+
Serial.println(volumesize);
100+
101+
102+
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
103+
root.openRoot(volume);
104+
105+
// list all files in the card with date and size
106+
root.ls(LS_R | LS_DATE | LS_SIZE);
107+
}
108+
109+
110+
void loop(void) {
111+
112+
}

Diff for: libraries/SD/examples/Datalogger/Datalogger.ino

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
SD card datalogger
3+
4+
This example shows how to log data from three analog sensors
5+
to an SD card using the SD library.
6+
7+
The circuit:
8+
* analog sensors on analog ins 0, 1, and 2
9+
* SD card attached to SPI bus as follows:
10+
** MOSI - pin 11
11+
** MISO - pin 12
12+
** CLK - pin 13
13+
** CS - pin 4
14+
15+
created 24 Nov 2010
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
19+
This example code is in the public domain.
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <SD.h>
25+
26+
const int chipSelect = 4;
27+
28+
void setup()
29+
{
30+
// Open serial communications and wait for port to open:
31+
Serial.begin(9600);
32+
while (!Serial) {
33+
; // wait for serial port to connect. Needed for Leonardo only
34+
}
35+
36+
37+
Serial.print("Initializing SD card...");
38+
39+
// see if the card is present and can be initialized:
40+
if (!SD.begin(chipSelect)) {
41+
Serial.println("Card failed, or not present");
42+
// don't do anything more:
43+
return;
44+
}
45+
Serial.println("card initialized.");
46+
}
47+
48+
void loop()
49+
{
50+
// make a string for assembling the data to log:
51+
String dataString = "";
52+
53+
// read three sensors and append to the string:
54+
for (int analogPin = 0; analogPin < 3; analogPin++) {
55+
int sensor = analogRead(analogPin);
56+
dataString += String(sensor);
57+
if (analogPin < 2) {
58+
dataString += ",";
59+
}
60+
}
61+
62+
// open the file. note that only one file can be open at a time,
63+
// so you have to close this one before opening another.
64+
File dataFile = SD.open("datalog.txt", FILE_WRITE);
65+
66+
// if the file is available, write to it:
67+
if (dataFile) {
68+
dataFile.println(dataString);
69+
dataFile.close();
70+
// print to the serial port too:
71+
Serial.println(dataString);
72+
}
73+
// if the file isn't open, pop up an error:
74+
else {
75+
Serial.println("error opening datalog.txt");
76+
}
77+
}
78+
79+
80+
81+
82+
83+
84+
85+
86+

Diff for: libraries/SD/examples/DumpFile/DumpFile.ino

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
SD card file dump
3+
4+
This example shows how to read a file from the SD card using the
5+
SD library and send it over the serial port.
6+
7+
The circuit:
8+
* SD card attached to SPI bus as follows:
9+
** MOSI - pin 11
10+
** MISO - pin 12
11+
** CLK - pin 13
12+
** CS - pin 4
13+
14+
created 22 December 2010
15+
by Limor Fried
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
19+
This example code is in the public domain.
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <SD.h>
25+
26+
const int chipSelect = 4;
27+
28+
void setup()
29+
{
30+
// Open serial communications and wait for port to open:
31+
Serial.begin(9600);
32+
while (!Serial) {
33+
; // wait for serial port to connect. Needed for Leonardo only
34+
}
35+
36+
37+
Serial.print("Initializing SD card...");
38+
39+
// see if the card is present and can be initialized:
40+
if (!SD.begin(chipSelect)) {
41+
Serial.println("Card failed, or not present");
42+
// don't do anything more:
43+
return;
44+
}
45+
Serial.println("card initialized.");
46+
47+
// open the file. note that only one file can be open at a time,
48+
// so you have to close this one before opening another.
49+
File dataFile = SD.open("datalog.txt");
50+
51+
// if the file is available, write to it:
52+
if (dataFile) {
53+
while (dataFile.available()) {
54+
Serial.write(dataFile.read());
55+
}
56+
dataFile.close();
57+
}
58+
// if the file isn't open, pop up an error:
59+
else {
60+
Serial.println("error opening datalog.txt");
61+
}
62+
}
63+
64+
void loop()
65+
{
66+
}
67+

Diff for: libraries/SD/examples/Files/Files.ino

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
SD card basic file example
3+
4+
This example shows how to create and destroy an SD card file
5+
The circuit:
6+
* SD card attached to SPI bus as follows:
7+
** MOSI - pin 11
8+
** MISO - pin 12
9+
** CLK - pin 13
10+
** CS - pin 4
11+
12+
created Nov 2010
13+
by David A. Mellis
14+
modified 9 Apr 2012
15+
by Tom Igoe
16+
17+
This example code is in the public domain.
18+
19+
*/
20+
#include <SPI.h>
21+
#include <SD.h>
22+
23+
File myFile;
24+
25+
void setup()
26+
{
27+
// Open serial communications and wait for port to open:
28+
Serial.begin(9600);
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for Leonardo only
31+
}
32+
33+
34+
Serial.print("Initializing SD card...");
35+
36+
if (!SD.begin(4)) {
37+
Serial.println("initialization failed!");
38+
return;
39+
}
40+
Serial.println("initialization done.");
41+
42+
if (SD.exists("example.txt")) {
43+
Serial.println("example.txt exists.");
44+
}
45+
else {
46+
Serial.println("example.txt doesn't exist.");
47+
}
48+
49+
// open a new file and immediately close it:
50+
Serial.println("Creating example.txt...");
51+
myFile = SD.open("example.txt", FILE_WRITE);
52+
myFile.close();
53+
54+
// Check to see if the file exists:
55+
if (SD.exists("example.txt")) {
56+
Serial.println("example.txt exists.");
57+
}
58+
else {
59+
Serial.println("example.txt doesn't exist.");
60+
}
61+
62+
// delete the file:
63+
Serial.println("Removing example.txt...");
64+
SD.remove("example.txt");
65+
66+
if (SD.exists("example.txt")) {
67+
Serial.println("example.txt exists.");
68+
}
69+
else {
70+
Serial.println("example.txt doesn't exist.");
71+
}
72+
}
73+
74+
void loop()
75+
{
76+
// nothing happens after setup finishes.
77+
}
78+
79+
80+

0 commit comments

Comments
 (0)