You are on page 1of 3

SAVE DATA TO MICRO SD CARD

#include <SD.h>
#include <SPI.h>

File myFile;

int pinCS = 53; // Pin 10 on Arduino Uno

void setup() {

Serial.begin(9600);
pinMode(pinCS, OUTPUT);

// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}

myFile = SD.open("test.txt", FILE_WRITE); // Create/Open file

// if the file opened okay, write to it:


if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("Testing text 1, 2 ,3...");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}

myFile = SD.open("test.txt");// Reading the file


if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
}
void loop() {
// empty
}
1. #include <SD.h>
2. #include <SPI.h>
3. #include <DS3231.h>
4.
5. File myFile;
6. DS3231 rtc(SDA, SCL);
7.
8. int pinCS = 53; // Pin 10 on Arduino Uno
9.
10. void setup() {
11.
12. Serial.begin(9600);
13. pinMode(pinCS, OUTPUT);
14.
15. // SD Card Initialization
16. if (SD.begin())
17. {
18. Serial.println("SD card is ready to use.");
19. } else
20. {
21. Serial.println("SD card initialization failed");
22. return;
23. }
24. rtc.begin();
25. }
26. void loop() {
27. Serial.print(rtc.getTimeStr());
28. Serial.print(",");
29. Serial.println(int(rtc.getTemp()));
30.
31. myFile = SD.open("test.txt", FILE_WRITE);
32. if (myFile) {
33. myFile.print(rtc.getTimeStr());
34. myFile.print(",");
35. myFile.println(int(rtc.getTemp()));
36. myFile.close(); // close the file
37. }
38. // if the file didn't open, print an error:
39. else {
40. Serial.println("error opening test.txt");
41. }
42. delay(3000);
43. }
For creating a chart in Excel we need to import this file and here’s how we will do
that:

From the data menu we need to click “Get Data From Text” button and select the text
file. Here we will choose “Delimited” and click next, and in the second step, select the
comma as delimiter and then finish the wizard.

So this process will insert the time and the temperature values into separate
columns. Now we just need to select both columns and from the insert menu select
“Insert line chart”. This will create the chart where we can see the temperature values
each 3 seconds.

You might also like