SD card message storage with Arduino
January 2021Recently I hand the need with an Arduino project to show lots of different messages on a graphical display. The storage of these messages would have far exceeded the available memory on my Arduino board.
Thankfully the graphical display I was using included an SD card slot that was accessible to the Arduino board.
After many hours of experimentation and frustration with the poorly documented SDFat library, I reverted to the inbuilt Arduino SD library.
The example code below shows a bare-bones function for retrieving these messages. It will look for a file named STRINGS.TXT in the root folder of the SD card and search through it line by line.
The first two characters on each line are the message code, and the rest of the line is the message (excluding newline). It is assumed that Linux line endings (\n) are used, rather than Windows line endings (\r\n), however you can achieve this on Windows by using a programming editor such as Notepad++ and selecting Edit -> EOL Conversion -> Unix (LF).
As usual, this is available on my Github repository.
/*
This is a quick demo showing how you can index and get messages from
a file on an SD card with an Arduino. Useful for when you have a lot
of static strings to display, but don't have the memory to store it.
Assumes that the SD card is FAT16 or FAT32 formatted, and that the
file is in the root directory of the SD card and is named
STRINGS.TXT.
An example STRINGS.TXT looks like:
01First message
02Something else
03Blah blah
Message numbers don't have to be sequential. There is no error
handling.
Toxicantidote.net - January 2021
*/
void getMessage(String search, char* target) {
// open the file from the SD card. assumes the filename is STRINGS.TXT
File dataFile = SD.open("STRINGS.TXT");
// allow up to two characters for the message identifier
char code[2];
// allow up to 32 characters for the message
char message[32];
// if the file is available...
if (dataFile) {
// while data is available from the file
while (dataFile.available()) {
// get data until a newline character is encountered. note this is a LINUX newline (\n)
// not a Windows newline (\r\n)
String str = dataFile.readStringUntil('\n');
// Get the first two characters on the line
str.substring(0, 3).toCharArray(code, 3);
// if those two characters match our target string
if (search == code) {
// get the remaning text on the line, up to 32 characters long
str.substring(2).toCharArray(message, 32);
// break out of the read loop - we have found what we need
break;
}
}
// close the file
dataFile.close();
}
// set the target to the message
target = message;
}
To get a message, first create a char instance to contain it, and then pass that to the getMessage function - see below:
char msg[32];
getMessage("01", msg);
If you want to get longer messages, simply change the size of the char array allocated to message, and update the call to toCharArray for message.
Use with caution, as no error handling or debugging has been included in this example.