Touch Screen on the Arduino Mega 2560

By | June 26, 2014

This is an addition to my post about the Touch Screen Shield for Arduino UNO, so if you’ve landed here, that may be a good place to start for more information.

Several people have asked about using this touch screen shield with the Arduino Mega, but I didn’t have much advice to offer because I didn’t own a Mega until yesterday.  Now that I have one in hand and have had the opportunity to spend an evening experimenting, I’ve been successful in getting all of the features working with an Aruino Mega 2560, using the screen as a shield.  The disadvantage to this method, as opposed to running wires to a detached screen, is that the data pins span three ports and are in no particular order, meaning several bit shifts and masks must be performed to get the data to/from the screen, slowing things down a bit.  As of this writing, I’m focusing solely on using the screen as a shield, not wiring it externally.  Given more time, I’ll experiment with both methods and post speed comparisons.



I haven’t done a lot of Arduino programming, so it took me a little while to figure out the whole pin situation in the header files.  Once I thought I understood it, I sketched everything out on my whiteboard and started modifying the TFTLCD.cpp source file to use the correct pins during read/write operations on the Mega.  Even after I had what I thought should be a working version, however, I wasn’t having any luck.  Oddly, the screen suddenly worked after changing one line of code for an equivalent that accomplished the same task.  This led me to believe that it might be a timing issue, where a certain code sequence might be running just fast enough to throw off the timing off enough to cause communication to fail.  So, I looked at the timing charts for the graphics chip, did some code clean up, and added a couple of short delays in the write operation.  With that, graphics became completely functional.

Touch Screen

The screen uses analog pins A0-A4 for LCD control and touch screen operations.  Luckily, these pins are in the same position on the UNO and the Mega, so the touch screen should work without any modification.  One caveat is that the pins must be left in the right state after touch screen operations.  Otherwise, the screen won’t function correctly.  For the time being, I’m performing that cleanup as follows:

 // Clean up pin modes for LCD
   pinMode(XM, OUTPUT);
   digitalWrite(XM, LOW);
   pinMode(YP, OUTPUT);
   digitalWrite(YP, HIGH);
   pinMode(YM, OUTPUT);
   digitalWrite(YM, LOW);
   pinMode(XP, OUTPUT);
   digitalWrite(XP, HIGH);

SD Card Reader

I’ve been using the sdfat library for SD card access on the Arduino.  One of its built-in features is an ability to use a software-based SPI library.  With this, accessing the screen’s built-in SD card reader with the Mega is a breeze, requiring only a few configurations.

Locate the SdFatConfig.h file inside of the SdFat library directory.  You’ll find two defines that should be changed to a “1” value to enable software SPI:  MEGA_SOFT_SPI and USE_SOFTWARE_SPI.  The default pin configurations are already correct.

 * Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos.
 * Default pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
 * Edit Software Spi pins to change pin numbers.
 * 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
 * Define LEONARDO_SOFT_SPI nonzero to use software SPI on Leonardo Arduinos.
 * Default pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
 * Edit Software Spi pins to change pin numbers.
 * LEONARDO_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
 * on Leonardo Arduinos.  Software SPI works well with GPS Shield V1.1
 * but many SD cards will fail with GPS Shield V1.0.
 * Set USE_SOFTWARE_SPI nonzero to always use software SPI on AVR.
// define software SPI pins so Mega can use unmodified 168/328 shields
/** Default Software SPI chip select pin */
uint8_t const SOFT_SPI_CS_PIN = 10;
/** Software SPI Master Out Slave In pin */
uint8_t const SOFT_SPI_MOSI_PIN = 11;
/** Software SPI Master In Slave Out pin */
uint8_t const SOFT_SPI_MISO_PIN = 12;
/** Software SPI Clock pin */
uint8_t const SOFT_SPI_SCK_PIN = 13;


Source Code

I made some modifications to the TFTLCD.cpp to support using this screen as  a shield with the Mega 2560 .  I’m providing just the modified source file here for now.  The rest of the source can be located under the original article.  I plan to clean up the code further and provide a better means of using the screen either as a shield or externally through more appropriate pin assignments using compiler directives.  If this is what you were looking for, however, here it is:

Download: TFTLCD.cpp


Extract and replace original TFTLCD.cpp in Arduino/libraries/TFTLCD folder.

25 thoughts on “Touch Screen on the Arduino Mega 2560

  1. Pingback: Touch Screen Shield for Arduino UNO |

  2. daniel

    sorry friend could spend your code example use couple see pictures library and smucha sdfat if Molesti and thanks c:

    1. Justin Post author

      I’ll try to get a code example. I did a little bit of work then broke my screen.

  3. Ankit

    I have tried this modification as u describe but it shows me “Initializing SD card…. failed” in MyBitmapExperiment. Give some suggestios… Thx in advance.

  4. Ankit

    FYI. I have also tried Quick Start program from SdFat library and its working great when I insert number 10 in that program. It gives me successful output for that.

  5. sdjngyq

    Thank you very much for your support for our products, we are the biggest advantage is to provide high quality, low price of the LCD panel,If there is need help, I’ll do my best, thank you again for your email :[email protected], here I can provide all kinds of size LCD screen, and is the lowest price.

  6. David

    The support for the LCD sucks though… I asked a honest question about one TFT LCD and all that came back was
    “Gmail is junk E-mail”.

  7. GPSSerbia

    Great! That modified TFTLCD.cpp just worked on my shield (which have mirrored screen). Thank you!

    1. Justin Post author

      Has the card worked in other scenarios, such as on the UNO? I’ve bought two LCD screens, and on both I had to re-solder the card slot to the board because it was loose. Also, do you have “#define SD_CS 10” or similar in your code?

  8. Elnur

    Hi, can you please guide me through. I have the same type of screen, but chipset is different, mine is HX8357. What needs to be changed in your library (sketch) files to get it working?
    I post test results here:

    where I did manage to get it working with adafruit library, but this is only by deattached board running extention wires to Arduino MEGA board as screen needs to be connected to digital pins 22-29…

    Appreciate for quick reply


  9. Pingback: LCD Touch Screen Information |

  10. Jeremi Roivas

    Hello, you TFTLCD library worked fine on arduino uno, but it seems the 2.4″ LCD gives only WHITE screen and does nothing on MEGA2560. I downloaded your updated TFTLCD library but seems not working at all.

    1. Fadhelah

      HiJeremi >>> i have same your problem >> do you solve it ?? plez help

  11. John Dyer

    Thank you very much for your code, I bought one of these displays and spent a couple of days trying to figure out how to access the touchscreen, now I know!

  12. hermanto

    how if we use this display connected to arduino due…can you help me?


    Hi I download you code the TFTLCD.cpp_zip and uploaded the code onto arduino MEGA and connect the tft lcd data pin (D0-D7) to pin 22-29 of arduino mega. The result on the white screen was a white screen only. Please help

  14. Jai Ganesh

    Hai I am using Arduino mega…

    i hv download ur TFTCLD library and updated TFTLCD.cpp as u said for mega

    now when i compile the code for graphictest.ino i am facing the following problem

    C:\Program Files\Arduino\libraries\TFTLCD\glcdfont.c:9:23: error: variable ‘font’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

    static unsigned char font[] PROGMEM = {

    can u please suggest a solution for this as soon as possible…

    1. Randy

      I’m getting this same error, is there any solution?

      Arduino\libraries\TFTLCD\glcdfont.c:9:23: error: variable ‘font’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

      static unsigned char font[] PROGMEM = {

      1. david

        Between static and unsigned, write ‘const’, in the glcdfont.c file
        static const unsigned char font[] PROGMEM = {

  15. Albert

    Hi, Randy,
    I hope you have already found the solution to your problem described above. I ran into the same…
    Just in case you haven´t (or anybody else fights the same struggle) here is what I did:
    I had a look inside glcdfont.c (it´s inside the libraries folder) and changed the declaration of the variable “font” from “static” to “const”.
    Before: static unsigned char font[] PROGMEM = {
    After: const unsigned char font[] PROGMEM = {
    Now it´s working.
    Lots of thanks to Justin. My screen had the C0C0 initials first and was just white. First I used it as breakout board and was a little uncertain concerning the wiring. I was glad to detect the library for the shield. After I mounted it as a shield, the screen went red on running the LCD_ ID_READER. As a breakout board it hadn´t done so. Next came the graphicstest with the error mentioned above….


Leave a Reply

Your email address will not be published. Required fields are marked *