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.


Another famous practical example of this project is reading the G-code from the memory card in the 3D printer. It is a standard set of commands that the software understands and executes sequentially.


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.


We will apply it to the previous tutorial file, which you can find by clicking here. It was about using the DHT11 sensor to get temperature and humidity readings and storing them in a TXT file on a memory card. What we'll do now is the exact opposite process.


Getting the items


For this project you will need the following components you can download from our store 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Half-size Breadboard
15 QAR
Sale Off
Voltaat MicroSD Card Module
17 QAR

Make sure you also have a breadboard and jumper wires to connect everything together

Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


To set up the SD card reader in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the SD card reader and the Arduino. Once the SD card reader and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.



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


The function of this code is to read and analyses the TXT file on the memory card to obtain the temperature and humidity variables, and then print these values on your computer.


For other projects, you may need to perform more operations on the values, such as making the 3D printer build a 3D model from a G-code TXT file, which is obviously quite complex.


To avoid difficulties, format your SD card as FAT32 before running the code. Then copy the TXT file to it, being careful to rename it with the same name indicated in the code; you can download the file here!


In order for the code to work correctly, you need to download some libraries. Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors. You can download the library files from the resources section and then install it by following this tutorial.


Let’s take a look at the code, and then you’ll be able to understand it once you follow the instructions and read the comments.


/*
Voltaat learn (http://learn.voltaat.com/)
Link for full tutorial: https://bit.ly/3UmBzwH
SD Library: https://bit.ly/3E9KlZo

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




Now you must have correctly wired the SD card reader to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


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



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It allows us to send and receive different commands as well as view information directly from the Arduino.


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.



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


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.


Resources 


Arduino Code

SD card Library

Fritzing Wiring file

Related Tutorials


LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.


In this tutorial we make a high-efficiency alarm system that will notify you when harmful gases are released into the air using the MQ-135 sensor.


In this tutorial we will use the Arduino and a sound sensor to detect sound levels in your environment and print this data directly on your computer.