SD Card Error Writing

I was looking up how to read and write to the SD card on the V5 brain, I found @jpearman’s code SD Card Help - #3 by jpearman
however, it continually told me that it had an “Error writing to the SD card”, this in this code, seems to mean it did not successfully write to it. I tried formatting it to fat32, however this did not work.

Unofficial Response
Hello @PokePanda ,
Please make sure you select the correct category. I believe potentially #v5-technical-support or #programming-support:vexcode-v5-blocks-tech-support would be a better tag as your question is not related to #programming-support:vexcode-vr-support .

Anyways, I feel as though the likelihood would be a coding error. If possible, could you provide your code?

1 Like

Here is my code:

/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       {author}                                                  */
/*    Created:      {date}                                                    */
/*    Description:  V5 project                                                */
/*                                                                            */

// Include the V5 Library
#include "vex.h"
using namespace vex;

// storage for some information to save
uint8_t     myTestData[ 100 ];
uint8_t     myReadBuffer[ 1000 ];

int main() {
    // set the test data to something detectable
    for(int i=0;i<100;i++) {
        myTestData[i] = i * 2;
    // write test data to SD Card
    int nWritten = Brain.SDcard.savefile( "test.h", myTestData, sizeof(myTestData) );

    // did that work ?
    if( nWritten > 0) {
        // display on screen how many bytes we wrote
        Brain.Screen.setCursor( 2, 2 );
        Brain.Screen.print( "We wrote %d bytes to the SD Card", nWritten );

        // now read it back into a different buffer
        int nRead = Brain.SDcard.loadfile( "test.h", myReadBuffer, sizeof(myReadBuffer) );

        // display on screen how many bytes we read
        Brain.Screen.setCursor( 3, 2 );
        Brain.Screen.print( "We read %d bytes from the SD Card", nRead );

        // and display some of the data
        Brain.Screen.setCursor( 6, 2 );
        for(int i=0;i<8;i++)
            Brain.Screen.print("%02X ", myReadBuffer[i]);
    else {
        Brain.Screen.printAt( 10, 40, "Error writing to the SD Card" );        

Code is fine (although you don’t need the line vex::brain::sdcard; )
so either bad SD card (or formatting, write protect issue etc.) or hardware fault with the V5.
What make and size is the SD Card ?


Ya sorry the sd card line was trying to fix it, it is a 64 gigabyte, PNY Elite - X

Try replacing int nWritten = Brain.SDcard.savefile( "test.h", myTestData, sizeof(myTestData) ); with int nWritten = Brain.SDcard.savefile( "test.h", (uint8_t*)"test", strlen("test") ); and see if that works. That way you can tell for sure if it’s the SD card.

Unofficial Response

I believe this quote from April, 2019 may be relevant:

1 Like

I’ve used 128GB and its worked fine.

I believe it might not have formatted correctly so I am going to try again to get it to fat32.

1 Like

I said the same thing in the thread I quoted. @jpearman said that some SD cards just don’t want to work with the V5 brain. But he didn’t say that SD cards above 16 GB will not work.

Thanks again, it turned out that the formatting was wrong, thanks again @jpearman for the SD card code, works great.

1 Like

Anything above 32GB will usually format automatically to EXFAT or NTFS.

My goto utility for formatting any drive / card as FAT32 (windows only).

1 Like