I'm trying to use an SD card and SdFat 2.2.3 for a datalogger, but having intermittent issues with SD.begin(). I'm able to successfully run the datalogger and write to the SD card for hours at a time, but when I upload new code or power cycle the Arduino, it will often fail at SD.begin().
I'm assuming I might not be properly "shutting down" the SD card, but it seems like a standard situation I've not been able to figure out a repeatable solution. I basically end up pushing wires in, reseating the SD card, and resetting the Arduino until it happens to start working again, but it really seems random.
I'm currently using the bench example file and getting:
19:04:16.943 -> begin() failed
19:04:16.943 -> Do not reformat the SD.
19:04:16.943 -> No card, wrong chip select pin, or wiring error?
19:04:16.943 -> SdError: 0X1,0XFF
CS pin is correct and the wiring isn't changing between when it works and the next time it doesn't.
Is there something I should be doing differently to shut down or restart the SD card connection?
As we know Arduinos are designed for experimentation and learning, often used with breadboards and loose wires, which can become unreliable if vibrated. They are not built for harsh, dirty, or electrically noisy environments commonly found in industrial, automotive, or other commercial applications, making them unsuitable for such settings.
To ensure we can provide the most accurate assistance, please include detailed information about your setup. Specify the exact board, shield, wiring configuration, and power sources you are using. An annotated schematic is ideal, as it provides a clear overview of your setup, but be sure to also include links to the technical specifications of your hardware components. A a clear photo of the setup would help.
Before posting your code, please review the forum guidelines How to get the best out of this forum and use code tags for better readability. Also, provide links to the technical information about your hardware, and include a preliminary schematic that shows all power, ground, and connections. For additional guidance, you can refer to this link: Useful Links and Tutorials. Note that in this context, breadboard diagrams are not considered proper schematics.
Is the SD card power cycled at the same time? If not, you might have a situation where the card is already initialized, but you're trying to initialize it again, which it may not understand. In any case, it may be necessary to power cycle the card, then wait a second or so to allow it to boot up before trying to initialize it.
I am currently using the Nano ESP32 (CS=D1) but have also had the issue on MKR1000/1010 boards so I'm reasonably confidant its not directly board related.
As stated I'm using the bench example code included in the SdFat library.
Do you mean literally removing power from the SD card as well? Yeah the SD card is powered off the Arduino output and the "power cycling" I'm referring to is pulling the USB cable supplying power. Though, sometimes I've been trying just an Arduino reset which would not power cycle the SD card, so I can try being more explicit about that. Is there a way to manually reinitialize the the card?
If you power cycle both, then everything should work the same as when you first powered everything up. Resetting just the MCU via a reset button does not reset the card, and that might cause a problem.
However, it is good to wait a second or so after powerup to allow the card to initialize internally (it has a processor too).