4 This example shows how to log data from three analog sensors
5 to an SD card using the SD library.
8 * SD card attached to SPI bus as follows:
9 ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
10 and pin #10 (SS) must be an output
11 ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
12 and pin #52 (SS) must be an output
13 ** Leonardo: Connect to hardware SPI via the ICSP header
14 Pin 4 used here for consistency with other Arduino examples
17 modified 9 Apr 2012 by Tom Igoe
19 This example code is in the public domain.
25 // On the Ethernet Shield, CS is pin 4. Note that even if it's not
26 // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
27 // 53 on the Mega) must be left as an output or the SD library
28 // functions will not work.
29 const int chipSelect = 4;
35 // Open serial communications and wait for port to open:
38 ; // wait for serial port to connect. Needed for Leonardo only
42 Serial.print("Initializing SD card...");
43 // make sure that the default chip select pin is set to
44 // output, even if you don't use it:
47 // see if the card is present and can be initialized:
48 if (!SD.begin(chipSelect)) {
49 Serial.println("Card failed, or not present");
50 // don't do anything more:
53 Serial.println("card initialized.");
55 // Open up the file we're going to log to!
56 dataFile = SD.open("datalog.txt", FILE_WRITE);
58 Serial.println("error opening datalog.txt");
59 // Wait forever since we cant write data
66 // make a string for assembling the data to log:
67 String dataString = "";
69 // read three sensors and append to the string:
70 for (int analogPin = 0; analogPin < 3; analogPin++) {
71 int sensor = analogRead(analogPin);
72 dataString += String(sensor);
78 dataFile.println(dataString);
80 // print to the serial port too:
81 Serial.println(dataString);
83 // The following line will 'save' the file to the SD card after every
84 // line of data - this will use more power and slow down how much data
85 // you can read but it's safer!
86 // If you want to speed up the system, remove the call to flush() and it
87 // will save the file only every 512 bytes - every time a sector on the
88 // SD card is filled with data.
91 // Take 1 measurement every 500 milliseconds