Data Logger with MKR SD Proto Shield

Learn how to log data on an SD card using the MKR SD Proto Shield.


In this tutorial, we will find out how we can log data on an SD card. The MKR SD Proto Shield is a MKR form factor shield, that has a slot for a micro SD card. A great add-on for any MKR board (except for Arduino MKR Zero which already has a micro SD card slot), that can be used to log data offline, or to store larger amounts of data.

In this tutorial we will only focus on logging offline data, by uploading a sketch that reads three of the analog pins on a MKR family board.

Important: The MKR SD Proto Shield does not come with pins soldered to the circuit board. These will have to be manually soldered to be able to fit on top of a MKR family board directly. If you do not have the option to solder, you can check out the MKR MEM Shield, a similar shield that also provides extra Flash memory, that comes with the pins pre-soldered.


The goals of this project are:

  • Read data from the analog pins on a MKR family board.
  • 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, or simply have 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 small in size 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 the storage limit.

Useful Scenarios

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 the 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, which we can then store 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 have now been logged, and these values can easily be converted into a graph. For example, we can see the patterns of temperature going up and down as well as the wind increasing.

Graphing the data.
Graphing the data.


The circuit for this tutorial is very easy. First, let's mount the MKR SD Proto Shield on top of a MKR family board of your choice.

Mounting the shield.
Mounting the shield.

Now, we need to insert a micro SD card into the slot on the shield.

Inserting the Micro SD card.
Inserting the Micro SD card.

Programming the Board

We will now get to the programming part of this tutorial. We are going to create a program that will make readings on A0, A1 and A2 pins, 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. Here are some of the core functions of this sketch:

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

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

1#include <SPI.h>
2#include <SD.h>
4const int chipSelect = SDCARD_SS_PIN;
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 }
14 Serial.print("Initializing SD card...");
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.");
25void loop() {
26 // make a string for assembling the data to log:
27 String dataString = "";
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 }
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 ="datalog.txt", FILE_WRITE);
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);

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

command introduced in the setup, which prevents the program from running unless the Serial Monitor opens.

On 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

file where we are printing the values 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 that the data logged on the SD card went well. To check this, let's follow a few simple steps:

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


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 a MKR family board, a MKR SD Proto Shield, 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

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


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 family boards (instructions are above the code).
  • Make sure you open the Serial Monitor.


In this tutorial, we have tested the main functionality of the MKR SD Proto Shield: storing data on an SD Card.

As a very basic example, we read the values of three analog pins, stored them in a

file and later viewed the data on our computer.

The analog pins are currently not generating any useful data, as we have not connected anything to them, but for further exploration you can connect some type of sensor, that would provide 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 is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.


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