Project tutorial
Reading Text Files From an SD Card (Arduino)

Reading Text Files From an SD Card (Arduino) © CC BY-NC-SA

This project uses a micro SD card to store a text file and print it out to a 16x2 liquid crystal display.

  • 1,246 views
  • 0 comments
  • 3 respects

Components and supplies

Apps and online services

About this project


This project uses an SD card breakout board to allow for the reading of a.txt file with an Arduino Nano. The Arduino can then display the contents of the text file on a liquid crystal display.

Materials:

As a member of Amazon's affiliate marketing program, I may earn from purchases made through the above links at no additional cost to you.

Create a File

First, we need to create a text file on a micro sd card that the Arduino will read. Insert the card in your computer (you may need an adapter for this if your computer doesn't already have a microSD slot, I included a link to one above).

  • Once the card is in your computer, navigate to its folder: This PC > cardname
  • Right click in the file explorer window and select: new > text document
  • Type something in the document and save it as "myfile.txt" or, just use the example file from below.
  • Once the file is created, eject the card and put it in the breakout board.

Code

Upload the following code to your board in the Arduino IDE:

#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>

File myFile;

const int rs = 2, en = 3, d4 = 7, d5 = 6, d6 = 5, d7 = 4; //lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
if (!SD.begin(10)) { //make sure sd card was found
while (true);
}

lcd.begin(16, 1);
lcd.setCursor(0, 0);
lcd.print("myfile.txt");
delay(2000);
lcd.clear();
}

void loop() {
myFile = SD.open("myfile.txt"); // open the file for reading

lcd.setCursor(16, 0); //
lcd.autoscroll(); //enable auto-scrolling

if (myFile) {
while (myFile.available()) { //execute while file is available
char letter = myFile.read(); //read next character from file
lcd.print(letter); //display character
delay(300);
}

myFile.close(); //close file
}

lcd.clear();
}

Circuit

Create a circuit as shown in the above diagram. If you choose to use another Arduino board instead, such as an UNO, all of the pins used will be the same.

Going Further and Additional Resources

This project is really meant to be an example of how to use an SD card with Arduino. If you are looking for more code examples or uses, here are some other sites you can visit:

Arduino SD Library page - https://www.arduino.cc/en/reference/SD

Micro SD Card Breakout Board Tutorial - https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial

Code

CodeArduino
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>

File myFile;

const int rs = 2, en = 3, d4 = 7, d5 = 6, d6 = 5, d7 = 4; //lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  if (!SD.begin(10)) { //make sure sd card was found
    while (true);
  }

  lcd.begin(16, 1);
  lcd.setCursor(0, 0);
  lcd.print("myfile.txt");
  delay(2000);
  lcd.clear();
}

void loop() {
  myFile = SD.open("myfile.txt"); // open the file for reading

  lcd.setCursor(16, 0); //
  lcd.autoscroll(); //enable auto-scrolling

  if (myFile) { 
    while (myFile.available()) { //execute while file is available
      char letter = myFile.read(); //read next character from file
      lcd.print(letter); //display character
      delay(300);
    }

    myFile.close(); //close file
  }

  lcd.clear();
}
myfile.txtPlain text
Did you ever hear the Tragedy of Darth Plagueis the wise? I thought not. It's not a story the Jedi would tell you. It's a Sith legend. Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could use the Force to influence the midichlorians to create life... He had such a knowledge of the dark side that he could even keep the ones he cared about from dying. The dark side of the Force is a pathway to many abilities some consider to be unnatural. He became so powerful... the only thing he was afraid of was losing his power, which eventually, of course, he did. Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep. It's ironic he could save others from death, but not himself.

Schematics

Breadboard Diagram
Untitled sketch bb fa35nm6thr

Comments

Similar projects you might like

Programming Nextion LCD Without SD Card

by Tamer Savaşcı

  • 23,148 views
  • 6 comments
  • 9 respects

SD Card Temperature Data Logger

Project tutorial by 3 developers

  • 12,158 views
  • 0 comments
  • 10 respects

SD Card Doorbell

Project tutorial by Projecter

  • 3,603 views
  • 0 comments
  • 10 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 69,816 views
  • 71 comments
  • 218 respects

Arduino Mate Dispenser

Project in progress by Roni Bandini

  • 6,320 views
  • 7 comments
  • 46 respects
Add projectSign up / Login