Rev 2201 Rev 2241
1 /* 1 /*
2 SD card datalogger 2 SD card datalogger
3 3
4 This example shows how to log data from three analog sensors 4 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.
6 6
7 The circuit: 7 The circuit:
8 * analog sensors on analog ins PC0, PC1, and PC2 8 * analog sensors on analog ins PC0, PC1, and PC2
9 * SD card attached to SPI bus as follows: 9 * SD card attached to SPI bus as follows:
10 ** MOSI - PB3 10 ** MOSI/CMD - PB3
11 ** MISO - PB4 11 ** MISO/DAT0 - PB4
12 ** CLK - PB5 12 ** CLK - PB5
13 ** CS - PD4 13 ** CD/DAT3 - PD4
14 -  
15 created 24 Nov 2010 -  
16 updated 2 Dec 2010 -  
17 by Tom Igoe -  
18 -  
19 This example code is in the public domain. -  
20 14
21 */ 15 */
22   16  
23 #include <SD.h> 17 #include <SD.h>
24 const int chipSelect = 4; 18 #define chipSelect 4
25   19  
26 void setup() 20 void setup()
27 { 21 {
28 Serial.begin(9600); 22 Serial.begin(9600);
29 Serial.print("Initializing SD card..."); 23 Serial.print("Initializing SD card...");
30 // make sure that the default chip select pin is set to 24 // make sure that the default chip select pin is set to
31 // output, even if you don't use it: 25 // output, even if you don't use it:
32 pinMode(10, OUTPUT); 26 pinMode(10, OUTPUT);
33 27
34 // see if the card is present and can be initialized: 28 // see if the card is present and can be initialized:
35 if (!SD.begin(chipSelect)) { 29 if (!SD.begin(chipSelect)) {
36 Serial.println("Card failed, or not present"); 30 Serial.println("Card failed, or not present");
37 // don't do anything more: 31 // don't do anything more:
38 return; 32 return;
39 } 33 }
40 Serial.println("card initialized."); 34 Serial.println("card initialized.");
41 } 35 }
42   36  
43 int count; 37 int count;
44   38  
45 void loop() 39 void loop()
46 { 40 {
47   41  
48 // make a string for assembling the data to log: 42 // make a string for assembling the data to log:
49 String dataString = "$"; 43 String dataString = "$LOG";
50 delay(100); 44 delay(100);
51 dataString += String(count); // print number of measurement 45 dataString += String(count); // print number of measurement
52 dataString += ","; 46 dataString += ",";
53   47  
54   48  
55 // read three sensors and append to the string: 49 // read three sensors and append to the string:
56 for (int analogPin = 0; analogPin < 3; analogPin++) { 50 for (int analogPin = 0; analogPin < 3; analogPin++) {
57 int sensor = analogRead(analogPin); 51 int sensor = analogRead(analogPin);
58 dataString += String(sensor); 52 dataString += String(sensor);
59 if (analogPin < 2) { 53 if (analogPin < 2) {
60 dataString += ","; 54 dataString += ",";
61 } 55 }
62 } 56 }
63   57  
64 // open the file. note that only one file can be open at a time, 58 // open the file. note that only one file can be open at a time,
65 // so you have to close this one before opening another. 59 // so you have to close this one before opening another.
66 File dataFile = SD.open("datalog.txt", FILE_WRITE); 60 File dataFile = SD.open("datalog.txt", FILE_WRITE);
67   61  
68 // if the file is available, write to it: 62 // if the file is available, write to it:
69 if (dataFile) { 63 if (dataFile) {
70 dataFile.println(dataString); 64 dataFile.println(dataString);
71 dataFile.close(); 65 dataFile.close();
72 // print to the serial port too: 66 // print to the serial port too:
73 Serial.println(dataString); 67 Serial.println(dataString);
74 count++; 68 count++;
75 } 69 }
76 // if the file isn't open, pop up an error: 70 // if the file isn't open, pop up an error:
77 else { 71 else {
78 Serial.println("error opening datalog.txt"); 72 Serial.println("error opening datalog.txt");
79 } 73 }
80 } 74 }