Wrong characters writing in file on SD card

Hello everyone,
I am having a problem reading data that I am writing to a file on an SD card. I sent a small example but in the original project it will contain data that should be opened in MS Excel, and in the header it should contain the words "variável,cartão,número" but instead it is appearing "variável,cartão,número".

Please, could anyone tell me how I could fix this?

Thanks.

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

void setup() {

  Serial.begin(9600);

  while (!Serial);

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {

    Serial.println("initialization failed. Things to check:");

    Serial.println("1. is a card inserted?");

    Serial.println("2. is your wiring correct?");

    Serial.println("3. did you change the chipSelect pin to match your shield or module?");

    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");

    while (true);

  }

  Serial.println("initialization done.");
}

void loop() {

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {

    dataFile.println("variável,cartão,número");

    dataFile.close();
  }
  else {
    Serial.println("error opening datalog.txt");
  }
  delay(3000);
}

Encoding problem. Configure your Excel to read the data as UTF-8, then it should work.

Hi pylon,
Thank you for reply.

Following your suggestion, I managed to open the file correctly in two ways:
1-Making the conversion by Excel itself as you suggested;
2-Open the file in the notepad and save as UTF-8 BOM. When I do this the file opens correctly in Excel.

Now if it were possible to save the file already by Arduino, at least with this UTF8 BOM format, it would be good and not need to convert every time.

Thanks.

Write the following three bytes at the start of the file:
0xEF, 0xBB, 0xBF.

1 Like

Wouldn't a simple char translator work? Kinda' along these lines?

char traslate(char inChar) {

   switch (inChar) {
      case 'á' : return something;
      case 'ú' : return somethingElse;
      ...
      default return inChar;
    }
}

-jim lee

Hi pylon,

I tried your solution but what I had was just a file starting with the characters "ï» ¿". I saw that this in some versions of Excel can work or not. It didn't work for me. But your suggestion opened my mind to move on to another solution.

Searching for these characters here, I realized that I could simply convert the characters I need to byte. Then I found this site https://www.branah.com/unicode-converter that converts the characters I need to the corresponding byte. Then I just have to write it down.

jimLee,

I didn't quite understand your suggestion. In case I would just convert the character I need, is that it? That "something" in the case would be the byte to return?

Thank you Guys!

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
File dataFile;

void setup() {

  Serial.begin(9600);

  while (!Serial);

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {

    Serial.println("initialization failed. Things to check:");

    Serial.println("1. is a card inserted?");

    Serial.println("2. is your wiring correct?");

    Serial.println("3. did you change the chipSelect pin to match your shield or module?");

    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");

    while (true);

  }

  Serial.println("initialization done.");
}

void loop() {

  String dataString = "variável;cartão;número";

  unsigned char bom1[] = { 0x56, 0x61, 0x72, 0x69, 0xE1, 0x76, 0x65, 0x6C, 0x3b, 0x43, 0x61, 0x72, 0x74, 0xe3, 0x6f, 0x3b, 0x4e, 0xfa, 0x6d, 0x65, 0x72, 0x6f};

  dataFile = SD.open("datalog.csv", FILE_WRITE);

  if (dataFile) {

    dataFile.print((char*)bom1);
    dataFile.println("");

    dataFile.close();

    Serial.println(dataString);

  }

  else {

    Serial.println("error opening datalog.txt");
  }
  delay(3000);
}

Yes that was my idea. The thought was that there were not many and it would just ignore the ones that didn't need to change.

-jim lee

1 Like

Great! I will improve my code with your suggestion.

Thank you!