I'm getting an unexpected extra character added to the serial monitor print line only when I initialize an SD card in the code.
Normal
The code below gives me this expected result in the serial monitor:
Serial Monitor
23:31:12.745 -> inputString: abc
Code
const int BUF_DIM = 3;
char inputString[BUF_DIM];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
inputString[0] = 'a';
inputString[1] = 'b';
inputString[2] = 'c';
Serial.print("inputString: ");
Serial.println(inputString);
}
void loop() {
}
Unexpected Character
However, the serial monitor prints an unexpected extra M
at the end of inputString
's value abc
when I add initialization of an SD card.
Serial Monitor
23:24:30.319 -> Initializing SD card...initialization done.
23:24:30.319 -> inputString: abcM
Code
#include <SPI.h>
#include <SD.h>
File myFile;
const int BUF_DIM = 3;
char inputString[BUF_DIM];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//-------------Toggle this block to see the M dissapear-----------
Serial.print("Initializing SD card...");
if (!SD.begin(5)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("DATA.TXT");
//-------------Toggle this block------------
inputString[0] = 'a';
inputString[1] = 'b';
inputString[2] = 'c';
Serial.print("inputString: ");
Serial.println(inputString);
}
Why is this happening and how can I get the serial monitor to behave as expected with the SD card.