The TFT shield consists of a touch screen and a micro SD card module which is not compatible with the Arduino Mega board. We have seen in previous tutorials how to use the shield screen and its SD card module. We’ll now look at how to modify the SD library to make the SD card module compatible with the Arduino Mega board.
Hardware
- Computer
- Arduino MEGA
- USB A Male cable
- TFT LCD Shield with SD module
- microSD card
Explanation
The TFT shield uses the SPI port to communicate with the SD module. However, like most shields, it has been developed for the Arduino UNO and is not compatible with the Arduino Mega. In fact, the pins of the SD module are connected to pins 11, 12 and 13 of the Arduino, which corresponds to the SPI port of a UNO but not a Mega (51, 52, 53).
Arduino Board | MOSI | MISO | SCK | SS |
UNO | 11 | 12 | 13 | 10 |
Mega | 51 | 50 | 52 | 53 |
Solution 1: Solder the pins
To avoid modifying the code, you can solder the pins according to the following list:
- MOSI 11 –> 51
- MISO 12 –> 50
- SCK 13 –> 52
The advantage is that you don’t have to delve into the code of the Arduino libraries and you retain the performance of the SPI port. The disadvantage is that you lose I/O.
Solution 2: Modify the SD.h library
If you don’t want to solder wires on the Arduino, you can modify the code in the SD.h library.
To make the TFT shield’s SD module compatible, 3 files must be modified: Sd2Card.h, Sd2Card.cpp and SD.cpp.
File Sd2Card.h
This file is located in the directory, C:\Program Files (x86)\Arduino\libraries\SD\src\utility
Line 47, set the MEGA_SOFT_SPI variable to 1, comment on line 38 and place
- Before
/** USE_SPI_LIB: if set, use the SPI library bundled with Arduino IDE, otherwise run with a standalone driver for AVR. */ #define USE_SPI_LIB /** Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos. Pins used are SS 10, MOSI 11, MISO 12, and SCK 13. MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used on Mega Arduinos. Software SPI works well with GPS Shield V1.1 but many SD cards will fail with GPS Shield V1.0. */ #define MEGA_SOFT_SPI 0 //XW //------------------------------------------------------------------------------ #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) #define SOFTWARE_SPI #endif // MEGA_SOFT_SPI //------------------------------------------------------------------------------ // SPI pin definitions // #ifndef SOFTWARE_SPI // hardware pin defs
- After
/** USE_SPI_LIB: if set, use the SPI library bundled with Arduino IDE, otherwise run with a standalone driver for AVR. */ //#define USE_SPI_LIB /** Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos. Pins used are SS 10, MOSI 11, MISO 12, and SCK 13. MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used on Mega Arduinos. Software SPI works well with GPS Shield V1.1 but many SD cards will fail with GPS Shield V1.0. */ #define MEGA_SOFT_SPI 1 //------------------------------------------------------------------------------ #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) #define SOFTWARE_SPI #endif // MEGA_SOFT_SPI //------------------------------------------------------------------------------ // SPI pin definitions // #ifndef SOFTWARE_SPI #define USE_SPI_LIB
File Sd2Card.cpp
This file is located in the directory, C:\Program Files (x86)\Arduino\libraries\SD\src\utility
On line 20, comment out the #define USE_SPI_LIB command. Then place this line in #if #endif after calling the file Sd2Card.h
- Before
#define USE_SPI_LIB #include <Arduino.h> #include "Sd2Card.h"
- After
//#define USE_SPI_LIB #include <Arduino.h> #include "Sd2Card.h" //modif mega #ifndef SOFTWARE_SPI //Added to enable use of MEGA #define USE_SPI_LIB #endif //SOFTWARE_SPI //modif mega
SD.cpp file
On line 356, modify the definition of the SDClass::begin() function to remove the sdSpiClock line.
- Before
boolean SDClass::begin(uint32_t clock, uint8_t csPin) { if (root.isOpen()) { root.close(); } return card.init(SPI_HALF_SPEED, csPin) && card.setSpiClock(clock) && volume.init(card) && root.openRoot(volume); }
- After
#ifndef SOFTWARE_SPI boolean SDClass::begin(uint32_t clock, uint8_t csPin) { return card.init(SPI_HALF_SPEED, csPin) && card.setSpiClock(clock) && volume.init(card) && root.openRoot(volume); } #else boolean SDClass::begin(uint32_t clock, uint8_t csPin) { return card.init(SPI_HALF_SPEED, csPin) && volume.init(card) && root.openRoot(volume); } #endif //SOFTWARE_SPI
You can now operate the SD module of the TFT shield with the Arduino Mega board and use the codes from the previous tutorials.
N.B: Don’t forget to set MEGA_SOFT_SPI to 0 when using another Arduino board.