MKR Zero Data Logger

Learn how to log data on an SD card.

Introduction

In this tutorial, we will find out how we can log data on an SD card. The Arduino MKR Zero comes equipped with a slot for a micro SD card, a great feature to use when there's a need to log data offline.

We will test this out by uploading a sketch that reads three of the analog pins.

Goals

The goals of this project are:

  • Read data from the analog pins.
  • Store the data on the SD card.
  • View the data using a text editor on a computer.

Hardware & Software Needed

Secure Digital (SD) Card

You have most likely used, nevertheless heard, of the SD card. It is short for Secure Digital, and is a great option for storing large quantities of data. The SD card is tiny and weighs practically nothing, yet, some of them are capable of storing up to a terabyte of data. In comparison, it is quite standard for a laptop to have between 128 - 256 gigabytes of storage space.

However, to log data from e.g. sensors, it would take quite some time to reach that limit.

Useful Scenarios

But let's take a look at when and where we might want to use an SD card. There are typically two main scenarios when they are quite useful for an Arduino project:

  • Storing larger files, e.g. sound files or icons (useful for projects using a display).
  • Logging data offline.

Let's imagine we have an Arduino board and a couple of environmental sensors, and we want to do a weather project. We do not have any way of transmitting real time data, but we would like to track the weather for the next month, to later be analysed. We do not have the possibility to connect the board to a computer, since it needs to be outdoor.

The solution: an SD card. A really simple way of logging data over a longer period of time, without any connection to Internet or a computer is to simply store it locally. Let's say we do a reading every 10 seconds of the temperature and wind speed, we can then also store that reading on the SD card.

Storing different data types.
Storing different data types.

When we have finished recording, we simply remove the SD card and plug it into a computer. The data has now nicely been logged, and these values can easily be converted into a graph. We can then for example see the pattern of temperature going up and down and the wind increasing.

Graphing the data.
Graphing the data.

Circuit

The circuit for this tutorial is very easy. All we have to do is insert the SD card into the MKR Zero board.

Inserting the SD card.
Inserting the SD card.

Programming the Board

We will now get to the programming part of this tutorial. We will here create a program that will make readings on A0, A1 and A2, create a file on the SD card, and log the readings in that file.

1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. We can now take a look at some of the Board Package functions of this sketch:

  • while (!Serial)
  • SD.begin(chipSelect)
    - initializes the SD card with selected pin.
  • File dataFile = SD.open("datalog.txt", FILE_WRITE)
    - creates the
    dataFile
    object to access the library functionalities, and opens/create a file named
    datalog.txt
    on the SD card, and set it to write mode.
  • dataFile.print()
    - prints something to the
    datalog.txt
    file.
  • dataFile.close()
    - closes the file.

The sketch can be found in the snippet below. The sketch can also be accessed from the IDE, by navigating to File > Examples > SD > Datalogger. Upload the code to the board.

1#include <SPI.h>
2#include <SD.h>
3
4const int chipSelect = SDCARD_SS_PIN;
5
6void setup() {
7 // Open serial communications and wait for port to open:
8 Serial.begin(9600);
9 while (!Serial) {
10 ; // wait for serial port to connect. Needed for native USB port only
11 }
12
13
14 Serial.print("Initializing SD card...");
15
16 // see if the card is present and can be initialized:
17 if (!SD.begin(chipSelect)) {
18 Serial.println("Card failed, or not present");
19 // don't do anything more:
20 while (1);
21 }
22 Serial.println("card initialized.");
23}
24
25void loop() {
26 // make a string for assembling the data to log:
27 String dataString = "";
28
29 // read three sensors and append to the string:
30 for (int analogPin = 0; analogPin < 3; analogPin++) {
31 int sensor = analogRead(analogPin);
32 dataString += String(sensor);
33 if (analogPin < 2) {
34 dataString += ",";
35 }
36 }
37
38 // open the file. note that only one file can be open at a time,
39 // so you have to close this one before opening another.
40 File dataFile = SD.open("datalog.txt", FILE_WRITE);
41
42 // if the file is available, write to it:
43 if (dataFile) {
44 dataFile.println(dataString);
45 dataFile.close();
46 // print to the serial port too:
47 Serial.println(dataString);
48 }
49 // if the file isn't open, pop up an error:
50 else {
51 Serial.println("error opening datalog.txt");
52 }
53 delay(1000);
54}

Testing It Out

After uploading the code to the board, we need to open the Serial Monitor to initialize the program. This is due to the

while(!Serial)
command introduced in the setup, which prevents the program from running.

When we open the Serial Monitor, we will see data from the three analog pins being printed, each separated by a comma. This is also how it should look inside the

.txt
file that we are printing the values to as well.

Serial Monitor output of the data recorded from pin A0-A2.
Serial Monitor output of the data recorded from pin A0-A2.

Now if we are seeing these values, and no error messages, we can assume the data logging on the SD card has gone well. To check this, let's follow a few simple steps:

1. Cut the power to the MKR Zero board.
2. Remove the micro SD card from the board.
3. Insert the micro SD card to an adapter that fits your computer.
4. Insert the card into a computer, and open up the

datalog.txt
file.

This file should now show the same values that we saw in the Serial Monitor.

Inside the datalog.txt file.
Inside the datalog.txt file.

And that's how we can log data, using only the MKR Zero board, the SD library and a micro SD card.

Bonus: If you want to log data in a file that can be opened with excel, change the

.txt
to
.csv
. These files can then be opened with excel, where you can do more interesting thing with the data, such as creating graphs.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • Check that the SD card is working.
  • Check that you have installed the Board Package for the MKR Zero board (instructions are above the code).
  • Make sure you open the Serial Monitor.

Conclusion

In this tutorial, we have tested out one of the many great features that comes with the MKR Zero board: the possibility to store data directly to a micro SD card. As a very basic example, we read the values of three analog pins, stored them in a

.txt
file and later viewed the data on our computer.

The analog pins are of course not generating any useful data, as we have not connected anything to them, but you can now go looking for some interesting sensors that could provide some more useful data.

Feel free to explore the SD library further, and try out some of the many cool functions in this library.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.