Preamble:
Some things to keep in mind when using the SD Library -from official site and SD library
The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 11, 12, and 13 (on most Arduino boards) or 50, 51, and 52 (Arduino Mega). Additionally, another pin must be used to select the SD card. This can be the hardware SS pin – pin 10 (on most Arduino boards) or pin 53 (on the Mega) – or another pin specified in the call to SD.begin().
Note that even if you don’t use the hardware SS pin, it must be left as an output or the SD library won’t work. Different boards use different pins for this functionality, so be sure you’ve selected the correct pin in SD.begin().
Not all the functions are listed on the main SD library page, because they are part of the library’s utility functions
Issues
SD card uses SPI to transfer data, and Uno D11, D12, D13 as hardware SPI (see below from ceez). That is the reason why Uno works out of box for SD access.
On the other hand, Mega 2560 SPI takes place on 50, 51 and 52 as shown below:
The current LCD shield uses 8-bit bus interface (LCD_D0 – LCD_D7) instead of SPI. That is why LCD works but SD card doesn’t.
Solution
One solution is to employ software SPI, modify the SD library to use software SPI on digital pins located at the same place as SPI pins on UNO board, so that same sketch will work both for Uno and Mega boards.
I guess the other option is to rewire and use hardware SPI from Mega. The sketch code needs to change accordingly.
Adafruit SD library is adapted and some instructions from here.
How to get it work with Mega 2560 with softSPI
-
Download the SD Adafruit library and replace the file in your library
12345cd /usr/share/arduino/librariessudo mv ./SD/ ./~SD.orig/sudo git clone https://github.com/adafruit/SD -
comment out #define USE_SPI_LIB from both Sd2Card.h and Sd2Card.cpp. reference here.
12345sudo nano /usr/share/arduino/libraries/SD/utility/Sd2Card.cpp-------------------------------------------------------//#define USE_SPI_LIB -
Modify Sd2Card.h to enable the softSPI for Mega 2560
12345sudo nano /usr/share/arduino/libraries/SD/utility/Sd2Card.cpp-----------------------------------------------#define MEGA_SOFT_SPI 1 //change it to non-zero from 0 -
I will do it later, I am watching my stories
-
All the examples works fine after change chipSelect = 10;
123const int chipSelect = 10; #from 4
NOV
About the Author:
Beyond 8 hours - Computer, Sports, Family...