Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
70 min
Share

Read a TXT file from SD card

Using Arduino, you can read a file from an SD card and extract variables from it! This can be very useful if you want to store data on an SD card, such as sensor readings or GPS data. Then process this information or use it in another system.

Project Video

Overview

In this tutorial, we will use the Arduino to open and read a file from a memory card. Then process it from the string state to extract the variables.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
MicroSD Card Module
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the SD card reader and the Arduino, as shown in the image below.

SD-card-read-with-DH11-wiring_voltaat


 Connections from the Arduino SD card module:


      • Arduino GND pin → SD card module GND pin (- pin)


      • Arduino 5V pin → SD card module VCC pin (+pin)


      • Arduino pin 12 → SD card module MISO pin


      • Arduino pin 11 → SD card module MOSI pin


      • Arduino pin 13 → SD card module SCK pin


      • Arduino pin 4 → SD card module CS pin

Coding




/*
 Voltaat learn (http://learn.voltaat.com/)
 Link for full tutorial:
 SD Library:

 Connections from the Arduino SD card module:
 • Arduino GND pin → SD card module GND pin (- pin)
 • Arduino 5V pin → SD card module VCC pin (+pin)
 • Arduino pin 12 → SD card module MISO pin
 • Arduino pin 11 → SD card module MOSI pin
 • Arduino pin 13 → SD card module SCK pin
 • Arduino pin 4 → SD card module CS pin
*/

//This library allows you to communicate with SPI devices
#include "SPI.h"

//include the SD library
#include "SD.h"

//Define SD card module cs to arduino digital pin 4
const int cs = 4;

//Define txt and temp vairables
String txt = "" , temp;

//Define incoming variable with type char
char incoming ;

//Define dataFile variable
File dataFile ;

//Define variables
boolean ReadFile = true;
String varName1 = "Time now";
String varName2 = "Date";
String varName3 = "Temperature";
String varName4 = "Humidity";

//Define variables
String Time;
String Date;
float Temperature;
float Humidity;

// Commands inside void setup run once.
void setup()
{
 //Start the serial monitor at 9600 baud rate (9600 bits per second)
 Serial.begin(9600);

 //Function we made to see if the card is present and can be initialized
 Initialize_SDcard();

 //Open a file in SD card in reading mode
 dataFile = SD.open("LOGGERCD.txt");

}

//Commands inside void loop run forever
void loop()
{

 if ( ReadFile) {
   if (dataFile)
   {
     //dataFile.avaialable() check if there are any bytes avialable for reading from the file
     while (dataFile.available())
     {
       //Read from the file
       incoming = dataFile.read();

       if (incoming != '\n')
         txt = txt + incoming;

       else {
         //Print to serial monitor
         Serial.println(txt);

         if ( txt.indexOf(varName1) >= 0 ) {
           //Function uncction to get a substring of a Stringfor example myString.substring(from, to)
           String msg = txt.substring(txt.indexOf(varName1) + varName1.length(), txt.indexOf(varName2));
           //Function to get a version of the String with any leading and trailing whitespace removed
           msg.trim();
           Time = msg;
           //Print to serial monitor
           Serial.print(varName1 + " : ");
           Serial.println(Time);
         }


         if ( txt.indexOf(varName2) >= 0 ) {
           //Function uncction to get a substring of a Stringfor example myString.substring(from, to)
           String msg = txt.substring(txt.indexOf(varName2) + varName2.length(), txt.indexOf(varName3));
           //Function to get a version of the String with any leading and trailing whitespace removed
           msg.trim();
           Date = msg;
           //Print to serial monitor
           Serial.print(varName2 + " : ");
           Serial.println(Date);
         }


         if ( txt.indexOf(varName3) >= 0 ) {
           //Function uncction to get a substring of a Stringfor example myString.substring(from, to)
           String msg = txt.substring(txt.indexOf(varName3) + varName3.length(), txt.indexOf(varName4));
           //Function to get a version of the String with any leading and trailing whitespace removed
           msg.trim();
           Temperature = msg.toFloat();
           //Print to serial monitor
           Serial.print(varName3 + " : ");
           Serial.println(Temperature);
         }


         if ( txt.indexOf(varName4) >= 0 ) {
           //Function uncction to get a substring of a Stringfor example myString.substring(from, to)
           String msg = txt.substring(txt.indexOf(varName4) + varName4.length());
           //Function to get a version of the String with any leading and trailing whitespace removed
           msg.trim();
           Humidity = msg.toFloat();
           //Print to serial monitor
           Serial.print(varName4 + " : ");
           Serial.println(Humidity);
         }

         txt = ""; // clearing the char
       }


     }
     dataFile.close(); //Close the file
   }

   else
     //Print to serial monitor
     Serial.println(" SD card reading failed");


   ReadFile = false;
 }
}

void Initialize_SDcard()
{
 // see if the card is present and can be initialized
 if (!SD.begin(cs))
 {
   Serial.println(" Card failed, or not present");
   // don't do anything more
   return;
 }
}


   

Testing it Out

You should also make sure you have chosen the right baud rate (9600) as specified in the code.
Read_from_SD_card-

Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

serial-monitor

Now, as we see in the following image, the serial monitor prints the TXT file line by line, and after each line, the code extracts the temperature and humidity values from it. We can use these values in more advanced operations, like making an actuator perform a specific task.

serial-monitor_output

If you remove your SD card while the code is running, you will get an error message with the status of the SD card through the serial monitor, getting feedback during the process is very useful and makes it easier for you to discover errors without looking for more than one reason.

SD-card-reading-failed

Resources

No items found.