Skip to main content

Write a text file to a microSD card

What this example shows​

Creating test.txt on a FAT, FAT32 or exFAT microSD card and writing a line of text into it. The display shows the progress.

Example code​

Trimmed from the pinned example: only the file header comment is removed and the board check is joined onto one line. The rest of the code is unchanged.

Select Board
Inkplate10_microSD_TXT_Write.ino
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#include "Inkplate.h" // Include Inkplate library to the sketch
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
SdFile file; // Create SdFile object used for accessing files on SD card

char *fileName = "test.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 10.\n"; // "\n" represents a new line

void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.display(); // Put clear image on display
display.setTextSize(5);

// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
display.println("SD Card ok!");
display.partialUpdate();

// Try to create the text file
if (!file.open(fileName, FILE_WRITE))
{ // If it fails to create, send an error message to display, otherwise write to file.
display.println("Error while creating the file!");
display.partialUpdate();
display.sdCardSleep();
}
else
{
display.println("Writing in the file...");
display.partialUpdate();
file.write(dataToWrite); // Write data string in the file
display.println("Data has been written successfully!");
display.partialUpdate();
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)
display.println("SD Card error!");
display.partialUpdate();
display.sdCardSleep();
while (true)
;
}
}

void loop()
{
// Nothing...
}

Functions used​

Inkplate(uint8_t mode)returns None

Creates the display object. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` gives 8 levels of grey but only full updates.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tmodeINKPLATE_1BIT or INKPLATE_3BIT.
display.begin()returns void

Initializes the library and the display. Call it once, at the start of `setup()`.

ReturnsNothing.

display.clearDisplay()returns void

Clears the frame buffer in memory. It does not change what the panel shows until you call `display()`.

ReturnsNothing.

display.display(bool _leaveOn = 0)returns void

Does a full refresh: sends the frame buffer to the panel. If `_leaveOn` is true, the panel supply stays on after the refresh, which saves time on the next update.

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnKeeps the panel supply on after the update when true. Defaults to 0.
display.setTextSize(uint8_t s)returns void

Scales text by an integer factor. The base font is 5 x 7 pixels.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsScale factor.
display.sdCardInit()returns int16_t

Powers the microSD card and starts the SD interface.

Returns`1` if the card initialized, `0` if it failed.

display.println(value)returns size_t

Same as `print()`, then moves the cursor to the start of the next line. It comes from the Arduino `Print` class that `Adafruit_GFX` builds on.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text or numbervalueWhat to print.
display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_t

Refreshes only the parts of the screen that changed, without a full refresh. It works only in `INKPLATE_1BIT` mode.

ReturnsNumber of pixels that changed from black to white, which can leave blur.

Parameters

TypeNameDescription
bool_forcedForces a partial update in cases the library would otherwise do a full one (advanced use with deep sleep). Defaults to false.
boolleaveOnKeeps the panel supply on after the update when true. Defaults to false.
SdFile()returns None

Creates a file object for reading and writing files on the card. `SdFile` is part of the SdFat library bundled with the Inkplate library.

ReturnsConstructor; no return value.

file.open(const char *path, oflag_t oflag = O_RDONLY)returns bool

Opens a file on the card.

Returns`true` if the file opened.

Parameters

TypeNameDescription
const char *pathFile name or path on the card.
oflag_toflagHow to open the file, for example O_RDONLY. Defaults to O_RDONLY.
file.write(const char *str)returns size_t

Writes a string to the open file.

ReturnsNumber of bytes written.

Parameters

TypeNameDescription
const char *strThe text to write.
file.close()returns bool

Closes the open file. Close a file after writing to avoid corrupting it.

Returns`true` if the file closed.

display.sdCardSleep()returns void

Turns off the power to the microSD card and sets the SD interface pins to inputs to save energy.

ReturnsNothing.

What to expect​

The display shows SD Card ok!, Writing in the file... and Data has been written successfully! in turn. The card then holds test.txt with the line Hello! This is the file writing example for Inkplate 10.. The file opens with FILE_WRITE, which creates the file and appends if it already exists, so running the sketch again adds the line again. If the file cannot be created, the display shows Error while creating the file!. If the card does not start, it shows SD Card error! and the sketch stops.

The example closes the file after writing. Always call file.close() when you are done writing, or the file can be corrupted.