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.
#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 NoneCreates 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
| Type | Name | Description |
|---|---|---|
uint8_t | mode | INKPLATE_1BIT or INKPLATE_3BIT. |
display.begin()returns voidInitializes the library and the display. Call it once, at the start of `setup()`.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not change what the panel shows until you call `display()`.
ReturnsNothing.
display.display(bool _leaveOn = 0)returns voidDoes 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
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Keeps the panel supply on after the update when true. Defaults to 0. |
display.setTextSize(uint8_t s)returns voidScales text by an integer factor. The base font is 5 x 7 pixels.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Scale factor. |
display.sdCardInit()returns int16_tPowers the microSD card and starts the SD interface.
Returns`1` if the card initialized, `0` if it failed.
display.println(value)returns size_tSame 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
| Type | Name | Description |
|---|---|---|
text or number | value | What to print. |
display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_tRefreshes 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
| Type | Name | Description |
|---|---|---|
bool | _forced | Forces a partial update in cases the library would otherwise do a full one (advanced use with deep sleep). Defaults to false. |
bool | leaveOn | Keeps the panel supply on after the update when true. Defaults to false. |
SdFile()returns NoneCreates 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 boolOpens a file on the card.
Returns`true` if the file opened.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | File name or path on the card. |
oflag_t | oflag | How to open the file, for example O_RDONLY. Defaults to O_RDONLY. |
file.write(const char *str)returns size_tWrites a string to the open file.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | str | The text to write. |
file.close()returns boolCloses the open file. Close a file after writing to avoid corrupting it.
Returns`true` if the file closed.
display.sdCardSleep()returns voidTurns 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.