Write a text file to a microSD card
What this example shows
Write a line of text to a file named text.txt on a microSD card and report each step in the Serial Monitor. The screen is not used. To read the file back on the screen, see Read a text file from a microSD card.
Open the Serial Monitor at 115200 baud before the sketch runs its steps, or reset the board once it is open.
Example code
#include "Inkplate.h" // Include Inkplate library to the sketch
Inkplate display; // Create an object on Inkplate library
SdFile file; // Create SdFile object used for accessing files on SD card
char *fileName = "text.txt"; // The name of a file with the extension.
// The text you want to write in the file
char *dataToWrite = "Hello! This is the file writing example for Inkplate 7SPECTRA.\n"; // "\n" represents a new line
void setup()
{
Serial.begin(115200); // Init serial communication
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
Serial.println("SD Card ok!");
if (!file.open(fileName, FILE_WRITE))
{ // If it fails to create, send an error message to display, otherwise write to file.
Serial.println("Error while creating the file!");
display.sdCardSleep();
}
else
{
Serial.println("Writing in the file...");
file.write(dataToWrite); // Write data string in the file
Serial.println("Data has been written successfully!");
file.close(); // Close the file
display.sdCardSleep(); // Put sd card in sleep mode
}
}
else
{ // If card init was not successful, display error on screen, put sd card in sleep mode, and stop the program
// (using infinite loop)
Serial.println("SD Card error!");
display.sdCardSleep();
while (true)
;
}
}
void loop()
{
// Nothing...
}
This example also uses SdFile, a file class from the SdFat code bundled with the Inkplate library (it is in the library's src/features/SdFat folder), and Serial, which comes with the ESP32 board package (arduino-esp32). Their functions below are described as the sketch uses them.
Functions used
Inkplate()returns NoneCreates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.
ReturnsConstructor; no return value.
SdFilereturns NoneFile class from the SdFat code bundled with the Inkplate library. The sketch creates one object, file, to open, write and close the file on the card.
ReturnsType; no return value.
Serial.begin(unsigned long baud)returns voidStarts serial communication at the given baud rate, here 115200.
ReturnsNothing.
display.begin()returns voidInitializes the library, the peripherals and the frame buffer. Call it once in setup().
ReturnsNothing.
display.sdCardInit()returns int16_tInitializes the microSD card so files on it can be opened.
ReturnsNon-zero when the card was initialized; the sketch treats 0 as an error.
Serial.println(value)returns size_tPrints text followed by a new line to the Serial Monitor.
ReturnsNumber of bytes printed.
file.open(const char *path, oflag_t oflag)returns boolOpens a file on the card. The sketch opens text.txt with FILE_WRITE, to write to it.
Returnstrue if the file was opened.
file.write(const char *str)returns size_tWrites a string to the open file. The sketch writes one line of text ending in a new line.
ReturnsNumber of bytes written.
file.close()returns boolCloses the open file so the data is saved on the card.
Returnstrue if the file was closed.
display.sdCardSleep()returns voidPuts the microSD card in sleep mode.
ReturnsNothing.
What to expect
With a working card, the Serial Monitor shows the three steps and the card now holds text.txt with the sketch's line in it.
SD Card ok!
Writing in the file...
Data has been written successfully!
If the card cannot be initialized, the sketch prints SD Card error! and stops. If the file cannot be created, it prints Error while creating the file!.
🔗Open the complete example
examples/Inkplate7SPECTRA/Advanced/SD/Inkplate7SPECTRA_SD_TXT_Write