Hi All,
I am writing this post based on a problem and solution I discovered when using the Arduino Micro (ATmega32u4) and using the SD Library to initialize a Adafruit Micro SD Card Break-out board.
In short the problem began when using not USB power from the computer to supply power to the Arduino while it ran its initialization. I was using a combination of libraries taken from Sparkfun for my SD card functions.
To replicate the problem try using the code below to drive the breakout board with the MOSI, RX_LED/SS, SCK, and MISO pin connections on the Micro, as specified by datasheet on the arduino website.
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
In my case I noticed that for the variable chipSelect, it did not matter what value I passed; the code would compile and run when connected to the USB power. If I powered the board via the VIN, +5V or USB via a wall adapter the code would fail to initialize. It seems that there is some "magical" (read variable declaration mismatch within the pin mapping support files) connection by using the computer for the initialization.
The solution to my problem was the change the variable chipSelect to instead be SS (See Below), I assumed that this was a hardcoded pin name for the Micro (matching pin names and numbers for the micro is a nightmare) and presto chango the code works with any type of power supply.
I also commented out the serial communication lines for my debugging and haven't gone back to replace the function to see if this breaks the code again.
// see if the card is present and can be initialized:
if (!SD.begin(SS)) {
// Serial.println("Card failed, or not present");
digitalWrite(GREENLED, HIGH); //Indicates that SD Card is being written to
// don't do anything more:
// return;
}
// Serial.println("card initialized.");
I hope this helps someone else out there, I know there was a lot of discussion around setting Pin Modes to Outputs and writing them HIGH before initializing and those tips could help as well.
Thanks,
SE