Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit fdc48fc

Browse files
fpistmalfran
authored andcommitted
SD: Update/Add examples
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent fe029a4 commit fdc48fc

File tree

7 files changed

+610
-62
lines changed

7 files changed

+610
-62
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
* SD card attached
9+
10+
*/
11+
// include the SD library:
12+
#include <SD.h>
13+
14+
Sd2Card card;
15+
SdFatFs fatFs;
16+
17+
void setup()
18+
{
19+
bool disp = false;
20+
// Open serial communications and wait for port to open:
21+
Serial.begin(9600);
22+
23+
while (!Serial);
24+
Serial.print("\nInitializing SD card...");
25+
while(!card.init(SD_DETECT_PIN)) {
26+
if (!disp) {
27+
Serial.println("initialization failed. Is a card inserted?");
28+
disp = true;
29+
}
30+
delay(10);
31+
}
32+
33+
Serial.println("A card is present.");
34+
35+
// print the type of card
36+
Serial.print("\nCard type: ");
37+
switch (card.type()) {
38+
case STD_CAPACITY_SD_CARD_V1_1:
39+
Serial.println("SD1");
40+
break;
41+
case STD_CAPACITY_SD_CARD_V2_0:
42+
Serial.println("SD2");
43+
break;
44+
case HIGH_CAPACITY_SD_CARD:
45+
Serial.println("SDHC");
46+
break;
47+
default:
48+
Serial.println("Unknown");
49+
}
50+
51+
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
52+
if (!fatFs.init()) {
53+
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
54+
return;
55+
}
56+
57+
// print the type and size of the first FAT-type volume
58+
uint32_t volumesize;
59+
Serial.print("\nVolume type is FAT");
60+
Serial.println(fatFs.fatType(), DEC);
61+
Serial.println();
62+
63+
volumesize = fatFs.blocksPerCluster(); // clusters are collections of blocks
64+
volumesize *= fatFs.clusterCount(); // we'll have a lot of clusters
65+
volumesize *= 512; // SD card blocks are always 512 bytes
66+
Serial.print("Volume size (bytes): ");
67+
Serial.println(volumesize);
68+
Serial.print("Volume size (Kbytes): ");
69+
volumesize /= 1024;
70+
Serial.println(volumesize);
71+
Serial.print("Volume size (Mbytes): ");
72+
volumesize /= 1024;
73+
Serial.println(volumesize);
74+
75+
76+
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
77+
File root = SD.openRoot();
78+
79+
// list all files in the card with date and size
80+
root.ls(LS_R | LS_DATE | LS_SIZE);
81+
}
82+
83+
void loop(void) {
84+
// do nothing
85+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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
10+
11+
*/
12+
13+
#include <SD.h>
14+
15+
void setup()
16+
{
17+
// Open serial communications and wait for port to open:
18+
Serial.begin(9600);
19+
while (!Serial) {
20+
; // wait for serial port to connect. Needed for Leonardo only
21+
}
22+
23+
Serial.print("Initializing SD card...");
24+
// see if the card is present and can be initialized:
25+
while (SD.begin(SD_DETECT_PIN) != TRUE)
26+
{
27+
delay(10);
28+
}
29+
delay(100);
30+
Serial.println("card initialized.");
31+
}
32+
33+
void loop()
34+
{
35+
// make a string for assembling the data to log:
36+
String dataString = "";
37+
38+
// read three sensors and append to the string:
39+
for (int analogPin = 0; analogPin < 3; analogPin++) {
40+
int sensor = analogRead(analogPin);
41+
dataString += String(sensor);
42+
if (analogPin < 2) {
43+
dataString += ",";
44+
}
45+
}
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", FILE_WRITE);
50+
51+
// if the file is available, write to it:
52+
if (dataFile) {
53+
dataFile.seek(dataFile.size());
54+
dataFile.println(dataString);
55+
dataFile.close();
56+
// print to the serial port too:
57+
Serial.println(dataString);
58+
}
59+
// if the file isn't open, pop up an error:
60+
else {
61+
Serial.println("error opening datalog.txt");
62+
}
63+
delay(100);
64+
}
65+
66+
67+
68+
69+
70+
71+
72+
73+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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
9+
10+
This example code is in the public domain.
11+
12+
*/
13+
14+
#include <SD.h>
15+
16+
void setup()
17+
{
18+
// Open serial communications and wait for port to open:
19+
Serial.begin(9600);
20+
while (!Serial) {
21+
; // wait for serial port to connect. Needed for Leonardo only
22+
}
23+
24+
25+
Serial.print("Initializing SD card...");
26+
// see if the card is present and can be initialized:
27+
while (SD.begin(SD_DETECT_PIN) != TRUE)
28+
{
29+
delay(10);
30+
}
31+
delay(100);
32+
Serial.println("card initialized.");
33+
34+
// open the file. note that only one file can be open at a time,
35+
// so you have to close this one before opening another.
36+
File dataFile = SD.open("datalog.txt");
37+
38+
// if the file is available, write to it:
39+
if (dataFile) {
40+
while (dataFile.available()) {
41+
Serial.write(dataFile.read());
42+
}
43+
dataFile.close();
44+
}
45+
// if the file isn't open, pop up an error:
46+
else {
47+
Serial.println("error opening datalog.txt");
48+
}
49+
}
50+
51+
void loop()
52+
{
53+
}
54+

libraries/SD/examples/Files/Files.ino

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
7+
8+
This example code is in the public domain.
9+
10+
*/
11+
#include <SD.h>
12+
13+
File myFile;
14+
15+
void setup()
16+
{
17+
// Open serial communications and wait for port to open:
18+
Serial.begin(9600);
19+
while (!Serial) {
20+
; // wait for serial port to connect. Needed for Leonardo only
21+
}
22+
23+
24+
Serial.print("Initializing SD card...");
25+
26+
while (SD.begin(SD_DETECT_PIN) != TRUE)
27+
{
28+
delay(10);
29+
}
30+
Serial.println("initialization done.");
31+
32+
if (SD.exists("example.txt")) {
33+
Serial.println("example.txt exists.");
34+
}
35+
else {
36+
Serial.println("example.txt doesn't exist.");
37+
}
38+
39+
// open a new file and immediately close it:
40+
Serial.println("Creating example.txt...");
41+
myFile = SD.open("example.txt", FILE_WRITE);
42+
myFile.close();
43+
44+
// Check to see if the file exists:
45+
if (SD.exists("example.txt")) {
46+
Serial.println("example.txt exists.");
47+
}
48+
else {
49+
Serial.println("example.txt doesn't exist.");
50+
}
51+
52+
// delete the file:
53+
Serial.println("Removing example.txt...");
54+
SD.remove("example.txt");
55+
56+
if (SD.exists("example.txt")) {
57+
Serial.println("example.txt exists.");
58+
}
59+
else {
60+
Serial.println("example.txt doesn't exist.");
61+
}
62+
}
63+
64+
void loop()
65+
{
66+
// nothing happens after setup finishes.
67+
}
68+
69+
70+

0 commit comments

Comments
 (0)