Netduino Day 5 – Writing to an SD Card

Expandable memory is always a plus no matter whether it’s a Phone, Camera or a Microcontroller. Even advantageous if we know how to use it. In this fifth day Netduino tutorial, we will learn a few writing operations about an SD card. We will learn how to write to a text file, shown as an example of writing a log. The Logger class is also capable of creating a text file at any given location then writing some text information to it.

SD Card in Netduino Plus

Circuit Setup and Theory

Well, pop in the SD card to the slot and you are good to go! No messy wires…

When an SD card is inserted into the slot, Netduino Plus will automatically mount it as SD directory. So the root direct is SD. The logger file, by default creates a log at SD\Report\logger.txt location. If you need to write to a custom location then use the LogCustom method.

After logged, typically at the end of your program, you MUST call the Close method otherwise information will not be saved unless you call Flush method from the Logger class.

C# .NET Program

This tutorial is mainly around programming as there is nothing to connect to our Netduino. Writing to a text file (or logging) is done via the Loggerclass. To make life little simpler, there are some static properties and methods which we can be called directly to write to an SD card. Let’s look at the Class Diagram of the Logger class. The highlighted methods/properties are static members.

Logger Class Diagram

Let’s look at some of the key methods of this class such as Log, StreamWriter, etc. In the method, we simply put together all the arguments into one string and then it calls WriteLog.

public static void Log(params object[] strings)
    string message = string.Empty;
    for (int i = 0; i < strings.Length; i++)
        message = message + strings[i].ToString() + " ";
    WriteLog(message, StreamWriter, PrefixDateTime, LogToFile );

One of the arguments used is StreamWriter which is a property and it creates a new instance of StreamWriter when called for the first time.

private static StreamWriter StreamWriter
        if (_streamWriter == null) _streamWriter = new StreamWriter(LogFilePath,(bool)Append);
        return _streamWriter;

The WriteLog method, it first adds the time stamp if required then calls the WriteLine method of a stream writer. Note that Netduino does not have internal clock so time logged by Netduino will not be same as clock time.

private static void WriteLog(string message, StreamWriter streamWriter, bool addDateTime, bool logToFile)
    if (addDateTime)
        DateTime current = DateTime.Now;
        message = "[" + current + ":" + current.Millisecond + "] " + message;
    if (logToFile) streamWriter.WriteLine(message);


When we directly call the Logger Class and start sending logs like below (code), the output may look like the image below.

// Directly start logging, no need to create any instance of Logger class
Logger.LogToFile = true;    // if false it will only do Debug.Print()
Logger.Append = true;       // will append the information to existing if any
Logger.PrefixDateTime = true; // add a time stamp on each Log call. Note: Netduino time is not same as clock time.
// any number of arguments can be passed. They will appended by a white space
Logger.Log("All", "these", "will", "be", "combined", "in", "to", "one", "string");
Logger.Log("This should go into the second line.");

Output of Logger.Log Method

Here is an example of writing to a custom location:

// Create an instance of Logger if you need to write to a custom location.
Logger customLogger = new Logger(@"One\OneOne", "one.txt", true);
customLogger.CustomPrefixDateTime = false;
customLogger.CustomLogToFile = true;
customLogger.LogCustom("All", "these", "will", "be", "combined", "in", "to", "one", "string", "-CustomLogger1.");

Output of Logger.LogCustom Method


1)      C# .NET Code (Solution File)

What Next

Right now, if you need to check what’s written in the SD card, you have to physically take out the card and then insert it into the computer, which obviously, you don’t want to do more frequently. So, we will learn about reading rather sending the information from the text file to computer via Serial Communication, no need to take the card out!

Related Posts


  • Pingback: Tutorial Netduino Parte 5 « Soloelectronicos

  • Hi,

    Am new to netduino and using VS 2012 +.NetMF 4.3.

    Using an N2+ board with a 2GB SD card i am getting an ‘UnauthorizedAccess’ exception thrown from System.IO.FileSystemManager::AddToOpenList the very first time the log file is being created. All subsequent deployments rite log entries without fail. Same error occurs using static implementation and an instantiated logging class. The ‘GetFilePath’ creates the missing log file folder with no problems but using new StreamWriter(LogFilePath, true) fails the first time everytime.

    Would appreciate any pointers.


  • Thanks, this worked great with just a tad of tweaking for MS framework 4.3.

Leave a Reply

Your email address will not be published. Required fields are marked *