Access SD Cards from TFT LCD Shield Using Arduino Mega 2560 Soft SPI

Posted by:

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.

ArduinoUno_R3_Pinouts.png

On the other hand, Mega 2560 SPI takes place on 50, 51 and 52 as shown below:

ArduinoMega2560_pinout.png

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

  1. Download the SD Adafruit library and replace the file in your library

  2. comment out #define USE_SPI_LIB from both Sd2Card.h and Sd2Card.cpp. reference here.

  3. Modify Sd2Card.h to enable the softSPI for Mega 2560

  4. I will do it later, I am watching my stories

    bmp-mega.JPG

  5. All the examples works fine after change chipSelect = 10;

3
  Related Posts

Comments

  1. Ceez  November 7, 2015

    Very nice work!

    reply
    • Ceez  November 7, 2015

      Did you try?

      From what I understand when read the file sd2card.h and sd2card.cpp, it should work just with

      reply
      • mistan  November 7, 2015

        Hi Ceez,

        I am getting following error message if leave #define USE_SPI_LIB un-commented.

        As you said, it should work; However, it was stated in this issue that there may be bug in cpp file.

        Thanks for your wonderful work.
        Tony

        reply

Add a Comment