Log2File serial starts with unreadable character

On a Arduino Uno, I am using log2file monito_filters to print my sensor output to a file. But instead of getting a clean log file, the first few lines are very often looks like the following,

Since I’ll have many log files I do not want to manually delete first line of each log file.

Do you guys know what is causing this issue and how can I solve it?


My code is not printing anything but sensor data. For completion here is the code,

 #include <Wire.h>
 #include <Arduino.h>

 int16_t mpu1[] = {0,0,0,0,0,0,0};
 int16_t mpu2[] = {0,0,0,0,0,0,0};

 void setup() {   
    Wire.begin();
    Wire.beginTransmission(0x68); // b 110 1000 = 6 8; when AD0 is low
    Wire.write(0x6B); // PWR_MGMT_1 register
    Wire.write(0); // set to zero (wakes up the MPU-6050)
    Wire.endTransmission(true);

    Wire.begin();
    Wire.beginTransmission(0x69);// b 110 1001 = 6 9
    Wire.write(0x6B);
    Wire.write(0);
    Wire.endTransmission(true);

    Serial.begin(9600);
 }

 void cPrint(unsigned long time, int16_t x1, int16_t y1, int16_t z1, int16_t x2, int16_t y2, int16_t z2){
   Serial.print(time); Serial.print("\t");
   
   Serial.print((double)x1 / 16384); Serial.print("\t");
   Serial.print((double)y1 / 16384); Serial.print("\t");
   Serial.print((double)z1 / 16384); Serial.print("\t");

   Serial.print((double)x2 / 16384); Serial.print("\t");
   Serial.print((double)y2 / 16384); Serial.print("\t");
   Serial.print((double)z2 / 16384); Serial.print("\t");
 }

 void loop() {
   Wire.beginTransmission(0x68);
   Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
   Wire.endTransmission(false);
   Wire.requestFrom(0x68, 7 * 2, true);
   mpu1[0]= Wire.read() << 8 | Wire.read();
   mpu1[1]= Wire.read() << 8 | Wire.read();
   mpu1[2]= Wire.read() << 8 | Wire.read();
   mpu1[3]= Wire.read() << 8 | Wire.read();
   mpu1[4]= Wire.read() << 8 | Wire.read();
   mpu1[5]= Wire.read() << 8 | Wire.read();
   mpu1[6]= Wire.read() << 8 | Wire.read();
   cPrint(micros(), mpu1[0],mpu1[1],mpu1[2], mpu2[0],mpu2[1],mpu2[2]);

   Wire.beginTransmission(0x69);
   Wire.write(0x3B);
   Wire.endTransmission(false);
   Wire.requestFrom(0x69, 7 * 2, true);
   mpu2[0]= Wire.read() << 8 | Wire.read();
   mpu2[1]= Wire.read() << 8 | Wire.read();
   mpu2[2]= Wire.read() << 8 | Wire.read();
   mpu2[3]= Wire.read() << 8 | Wire.read();
   mpu2[4]= Wire.read() << 8 | Wire.read();
   mpu2[5]= Wire.read() << 8 | Wire.read();
   mpu2[6]= Wire.read() << 8 | Wire.read();  

   Serial.print("\r");

   if(micros() >= 15000000) exit(0);
 }

Edit: Here is the content of platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_filters = log2file

Missing platformio.ini. If it’s an ESP8266, that’s the bootloader doing stuff at a different baudrate, which is also recorded.

Added board information and platformio.ini file content.

Not occurring for me.

First line is all-OK.

Note that by doing a Serial.print("\r"); you’re only printing a carriage return which returns the ‘current cursor’ to the start of the line. In the monitor you’re thus always updating the same line.

Is the behavior different when you do a normal Serial.println() or Serial.print("\n");?

Also, the line if(micros() >= 15000000) exit(0); is weird. If you exit like that (which I have also never seen in a microcontroller programm before), only infinity while(1) {}), you also don’t give the Serial enough time to print the rest.

So you should at least do a Serial.flush() before exiting.

Lastly, it could have something to do with the USB-serial converter chip on your board. Mine uses a genuine ATMega16u2 for that, other clones might have a CH340, which may affect it.