SD Card#
Introduction#
Using an SD card with VEX V5 enables you to store and access files on a removable SD card inserted into the Brain’s SD card slot. Whether you’re saving sensor data or loading custom images, these methods provide full control over file operations. The removable SD card allows you to easily transfer data between your Brain and computer.
The V5 Brain requires an SD card no larger than 32GB, formatted as FAT32. SD cards larger than 32GB use exFAT formatting by default, which is not compatible with the V5 Brain. Ensure your SD card is properly formatted to FAT32 before use.
All files must be placed in the root folder of the SD card to be accessible.
For the examples below, the constructed Brain includes access to the SD card methods and will be used in all subsequent examples throughout this API documentation when referring to those methods.
Below is a list of all methods:
Actions — Perform file operations and display images from the SD card.
loadfile— Loads a file from the SD card into a bytearray.savefile— Saves a bytearray onto the SD card.appendfile— Appends a bytearray to an existing file.draw_image_from_file— Displays a Bitmap Image from the SD card.
Getters — Return SD card and file information.
is_inserted— Returns a Boolean indicating whether an SD card is inserted into the Brain.size— Returns the size of a file in bytes.exists— Returns a Boolean indicating whether a file is found on the SD card.
For information on constructing a Brain to gain access to the sdcard methods, see Brain.
Actions#
loadfile#
loadfile loads a file from the SD card into a bytearray.
Usage:
Brain.SDcard.loadfile(filename, buffer);
Parameters |
Description |
|---|---|
|
The name of the file to load as a string. The file’s name must include the file extension. |
|
An existing bytearray to load the file’s content into. If the existing bytearray is larger, its contents will be overwritten. If the existing bytearray is smaller, the method may not load all data. By default, a new bytearray will be returned if no buffer is provided. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// Create a message to save to the SD card
uint8_t message[] = "Hello from VEX!";
// Save the message into "Text.txt"
Brain.SDcard.savefile("Text.txt", message, sizeof(message));
// Get the size of the file
int fileSize = Brain.SDcard.size("Text.txt");
// Make a buffer to hold the file data
uint8_t buffer[fileSize];
// Load the file from the SD card into the buffer
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
// Display the file contents on the screen
Brain.Screen.print("%s", buffer);
} else {
Brain.Screen.print("Insert SD card");
}
}
savefile#
savefile saves a bytearray onto the SD card.
Usage:
Brain.SDcard.savefile(filename, buffer);
Parameters |
Description |
|---|---|
|
The name of the file to save as a string. The file’s name must include the file extension. If the file already exists, it will be overwritten. If the file doesn’t exist, a new file will be created. |
|
A bytearray to write into the file. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// Create a message to save to the SD card
uint8_t message[] = "Hello from VEX!";
// Save the message into "Text.txt"
Brain.SDcard.savefile("Text.txt", message, sizeof(message));
// Get the size of the file
int fileSize = Brain.SDcard.size("Text.txt");
// Make a buffer to hold the file data
uint8_t buffer[fileSize];
// Load the file from the SD card into the buffer
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
// Display the file contents on the screen
Brain.Screen.print("%s", buffer);
} else {
Brain.Screen.print("Insert SD card");
}
}
appendfile#
appendfile appends a bytearray to an existing file, adding new data to the end of the file without altering the current contents.
Usage:
Brain.SDcard.appendfile(filename, buffer);
Parameters |
Description |
|---|---|
|
The name of the file to append as a string. The file’s name must include the file extension. |
|
A bytearray containing the data to append. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Create a message to save to a new file
uint8_t message[] = "Hello";
// Save only the characters, not the null terminator
Brain.SDcard.savefile("Message.txt", message, sizeof(message) - 1);
// Load the file
int fileSize = Brain.SDcard.size("Message.txt");
uint8_t buffer[fileSize + 1];
Brain.SDcard.loadfile("Message.txt", buffer, fileSize);
// Add null terminator so it can be printed safely
buffer[fileSize] = 0;
Brain.Screen.print("%s\n", buffer);
// Create more text to append to the same file
uint8_t moreText[] = " World!";
// Append only the characters, not the null terminator
Brain.SDcard.appendfile("Message.txt", moreText, sizeof(moreText) - 1);
// Load the updated file
fileSize = Brain.SDcard.size("Message.txt");
uint8_t buffer2[fileSize + 1];
Brain.SDcard.loadfile("Message.txt", buffer2, fileSize);
// Add null terminator
buffer2[fileSize] = 0;
Brain.Screen.newLine();
Brain.Screen.print("%s", buffer2);
}
drawImageFromFile#
drawImageFromFile displays a Bitmap Image (BMP) stored on the SD card.
.bmp files are limited to 5120 pixels and 6KB in size.
Note: The V5 Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.
Usage
Brain.Screen.drawImageFromFile(filename, x, y);
Parameters |
Description |
|---|---|
|
The filename of the image. |
|
The x coordinate for the top-left corner of the image on the screen. |
|
The y coordinate for the top-left corner of the image on the screen. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Draw a bmp file on the Brain's screen at coordinate 0, 0
Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);
}
Getters#
isInserted#
isInserted returns a Boolean indicating whether an SD card is currently inserted into the Brain.
true— The Brain has an SD card inserted.false— The Brain does not have an SD card inserted.
Usage:
Brain.SDcard.isInserted()
Parameters |
Description |
|---|---|
This method has no parameters. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// Create a message to save to the SD card
uint8_t message[] = "Hello from VEX!";
// Save the message into "Text.txt"
Brain.SDcard.savefile("Text.txt", message, sizeof(message));
// Get the size of the file
int fileSize = Brain.SDcard.size("Text.txt");
// Make a buffer to hold the file data
uint8_t buffer[fileSize];
// Load the file from the SD card into the buffer
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
// Display the file contents on the screen
Brain.Screen.print("%s", buffer);
} else {
Brain.Screen.print("Insert SD card");
}
}
size#
size returns the size of a file in bytes.
Usage:
Brain.SDcard.size(filename);
Parameters |
Description |
|---|---|
|
The name of the file as a string. The file’s name must include the file extension. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// Create a message to save to the SD card
uint8_t message[] = "Hello from VEX!";
// Save the message into "Text.txt"
Brain.SDcard.savefile("Text.txt", message, sizeof(message));
// Get the size of the file
int fileSize = Brain.SDcard.size("Text.txt");
// Make a buffer to hold the file data
uint8_t buffer[fileSize];
// Load the file from the SD card into the buffer
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
// Display the file contents on the screen
Brain.Screen.print("%s", buffer);
} else {
Brain.Screen.print("Insert SD card");
}
}
exists#
exists returns a Boolean indicating whether a file is found on the SD card.
true— The file is on the SD card.false— The file is not on the SD card.
Usage:
Brain.SDcard.exists(filename);
Parameters |
Description |
|---|---|
|
The name of the file as a string. The file’s name must include the file extension. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Add more text and display file size
if (Brain.SDcard.exists("Text.txt")) {
// Get the size of the file
int fileSize = Brain.SDcard.size("Text.txt");
// Make a buffer to hold the file data
uint8_t buffer[fileSize];
// Load the file from the SD card into the buffer
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
// Display the file contents on the screen
Brain.Screen.print("%s", buffer);
}
}