Arduino SD Card and Data Logging To Excel Tutorial - HowToMechatronics
Arduino SD Card and Data Logging To Excel Tutorial - HowToMechatronics
In this Arduino Tutorial we will learn how to use an SD Card module with the Arduino Board. Also in
combination with the DS3231 Real Time Clock module we will make a data logging example where
we will store the data of a temperature sensor to the SD Card and import it into Excel to make a ARDUINO WIRELESS WEATHER STATION
chart out of it. You can watch the following video or read the written tutorial below. PROJECT
Dejan 2
This website uses cookies to improve user experience. By using our website, you agree to our use of cookies. Ok Read more
POPULAR
Arduino and HC-05 Bluetooth Module
Tutorial
Arduino SD Card Module
Dejan 32
F O L LO W M E !
Join My Newsletter!
Get noti ed when I publish new projects and tutorials
straight to your inbox.
The SD Card Module have six pins, two for powering the module, the VCC and the GND pins, and
four more pins for the SPI communication. Here’s how we need to connect it to the Arduino Board. SUBSCRIBE
How To Mechatronics
21,360 likes
D I Y P R O J E C T BY C R E AT I V I T Y H E R O
Note that each Arduino Board have di erent SPI pins which should be connected accordingly.
You can get the components needed for this Arduino Tutorial from the links below: Arduino Based DIY Interactive LED Co ee Table
*Please note: These are a liate links. I may make a commission if you buy the components through
these links. I would appreciate your support in this way!
1. /*
2. * Arduino SD Card Tutorial Example
3. *
4. * by Dejan Nedelkovski, www.HowToMechatronics.com
5. */
6.
7. #include <SD.h>
8. #include <SPI.h>
9.
10. File myFile;
11.
12. int pinCS = 53; // Pin 10 on Arduino Uno
13.
14. void setup() {
15.
16. Serial.begin(9600);
17. pinMode(pinCS, OUTPUT);
18.
19. // SD Card Initialization
20. if (SD.begin())
21. {
22. Serial.println("SD card is ready to use.");
23. } else
24. {
25. Serial.println("SD card initialization failed");
26. return;
27. }
28.
29. // Create/Open file
30. myFile = SD.open("test.txt", FILE_WRITE);
31.
32. // if the file opened okay, write to it:
33. if (myFile) {
34. Serial.println("Writing to file...");
35. // Write to file
36. myFile.println("Testing text 1, 2 ,3...");
37. myFile.close(); // close the file
38. Serial.println("Done.");
39. }
40. // if the file didn't open, print an error:
41. else {
42. Serial.println("error opening test.txt");
43. }
44.
45. // Reading the file
46. myFile = SD.open("test.txt");
47. if (myFile) {
48. Serial.println("Read:");
49. // Reading the whole file
50. while (myFile.available()) {
51. Serial.write(myFile.read());
52. }
53. myFile.close();
54. }
55. else {
56. Serial.println("error opening test.txt");
57. }
58.
59. }
60. void loop() {
61. // empty
62. }
Code description: So rst we need to include the standard SD and SPI libraries, create a “File” object
and de ne the ChipSelect pin of the SPI bus, the pin 53 in my case for the Arduino Mega Board. For
this example we want our code to be executed only once, so all the code will be placed in the “setup”
section, while the “loop” section will remain empty.
So rst we need to start the serial communication and de ne the ChipSelect pin as output. We have
to do this because the ChipSelect pin needs to be “Low” so that the SPI communication between the
module and the Arduino works.
Next, using the SD.begin() function we will initialize the SD card and if initialization is successful the
“if” statement will become true and the String “SD card is ready to use.” will be printed on the serial
monitor, else the string “SD card initialization failed” will be printed and also the program will be
terminated.
Next, using the SD.open() function we will create a new le named “test.txt”, including the
FILE_WRITE argument meaning that we can both read and write to the le. If the le already exist the
SD.open() function will just open it.
So if the le has been successfully created rst we will print the string “Writing to le” on the serial
monitor and then using the myFile.println() function we will print the text “Testing text 1, 2 ,3…” into
the le. After that we need to use close() function to ensure that the previous data written to the le
is physically saved to the SD Card.
Next, we will see how we can read from the le. So again we will the same function, SD.open(), but
this time as the le “test.txt” has already been created, the function will just open the le. Then using
the myFile.read() function we will read from the le and print it on the serial monitor. The read()
function actually reads just one character at a time, so therefore we need to use the “while” loop and
the function myFile.available() to read all characters or the whole previously written data. At the end
we need to close the le.
Now after uploading the code to the Arduino, if everything is ok, the following will appear on the
serial monitor.
As we can see, the SD card has been successfully initialized, the writing to it has been successful as
well, and also reading the written data or the string “Testing text 1, 2 ,3…” has been successful read.
If we open the SD card on our computer we can see the created “test.txt” le and the written text in
it.
Now let’s make another more interesting example of data logging a temperature sensor. For that
purpose we will use the DS3231 Real Time Clock module which has a built-in temperature sensor as
well. You can nd more details about how to connect and use this module in my previous tutorial.
So after connecting the two modules to the Arduino let’s take a look at the code for this example.
1. /*
2. * Arduino Temperature Data Logging
3. *
4. * by Dejan Nedelkovski, www.HowToMechatronics.com
5. */
6.
7. #include <SD.h>
8. #include <SPI.h>
9. #include <DS3231.h>
10.
11. File myFile;
12. DS3231 rtc(SDA, SCL);
13.
14. int pinCS = 53; // Pin 10 on Arduino Uno
15.
16. void setup() {
17.
18. Serial.begin(9600);
19. pinMode(pinCS, OUTPUT);
20.
21. // SD Card Initialization
22. if (SD.begin())
23. {
24. Serial.println("SD card is ready to use.");
25. } else
26. {
27. Serial.println("SD card initialization failed");
28. return;
29. }
30. rtc.begin();
31. }
32. void loop() {
33. Serial.print(rtc.getTimeStr());
34. Serial.print(",");
35. Serial.println(int(rtc.getTemp()));
36.
37. myFile = SD.open("test.txt", FILE_WRITE);
38. if (myFile) {
39. myFile.print(rtc.getTimeStr());
40. myFile.print(",");
41. myFile.println(int(rtc.getTemp()));
42. myFile.close(); // close the file
43. }
44. // if the file didn't open, print an error:
45. else {
46. Serial.println("error opening test.txt");
47. }
48. delay(3000);
49. }
Code Description: First we need to include the libraries needed for both modules, then create the
two objects and in the setup section initialize them.
In the loop section using the Serial.print() funtion we will print the time and the temperature values
on the serial monitor, with a “comma” character between them and a new line after the temperature
value. We need this form of the lines so that we can easily import them and make a chart in Excel.
Also note that the temperature values are converted into integers.
So these same values will also be written into the newly created “test.txt” le and at the end we just
need to add a delay which will represent the interval of recording the temperature data.
After uploading the code the Arduino will start storing the temperature values each 3 seconds.After
a while we can open the SD card on our computer to see the results.
For creating a chart in Excel we need to import this le 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 le. Here we
will choose “Delimited” and click next, and in the second step, select the comma as delimiter and
then nish 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.
That’s all for this tutorial, feel free to ask any question in the comments section below.
China's Largest PCB Prototype Enterprise, 300,000+ Customers & 10,000+ Online Orders Per Day
R E L AT E D P O S T S
17 RESPONSES
Hello,
great tutorial, how can i create txt les name randomly ? like date “01102016.txt”
automatic by ardu.
REPLY
REPLY
String x;
String y;
y = “Test”;
x = String(y + “.txt”);
myFile = SD.open(x.c_str(), FILE_WRITE);
hi
in Arduino mega2560(R3) pin #50 is MISO and #51 is MOSI
in schematic green wire and blue Must be changed.
thanks
REPLY
That’s correct. Although it’s stated that MISO goes to MISO and MOSI
to MOSI, I have drawn those line wrong. Thanks for the remark. The
circuit schematic is now updated.
REPLY
Hi!
Great tutorial! I was a little problem, but I solved it. My DUE’s pins allocation not the
same as the Mega’s. Here is the connecting:
CS-> pin 10
GND->GND
MISO,MOSI,SCK->SPI for SAM3X8E (center of the board)
Thanks,
Z.
REPLY
Thanks! Yeah, each Arduino have di erent pins for the SPI
communication.
REPLY
Please give tutorial on the RFID 13.56MHz please use MFRC522 System with
tag+keychain.
Thanks! I like your tutorials alot.
REPLY
REPLY
Can you please share link for download SD.h and SPI.h for Arduino SD Card and Data
Logging tutorial?
REPLY
Those are regular Arduino libraries and should be included with your
Arduino installation. Of course you can always use the Manage
Libraries option, Sketch > Include Library > Manage Libraries…, to
insert new libraries.
REPLY
REPLY
Yep, thanks!
REPLY
Thank Dejan for the tutorial. I was wondering if you would put together an advanced
tutorial that would combine many of your older tutorials all together:
I would really appreciate it. Is there a way to donate for such a tutorial?
REPLY
REPLY
L E A V E A R E P LY
Comment
Name* Email*
Website
S U B MIT
H O W T O M E C H AT R O N I C S F O L L O W H O W T O M E C H AT R O N I C S O N DISCLOSURE
SOCIAL MEDIA
HowToMechatronics is an education website in the HowToMechatronics is a participant in the Amazon
area of Mechanical, Electrical and Computer Services LLC Associates Program, an a liate
Engineering. Tutorials, Tips, Tricks, How It Works, advertising program designed to provide a means
Projects, Examples, Source Codes, Download les for sites to earn advertising fees by advertising and
and much more can be found here. linking to Amazon.com