Introduction: How to Use 2.4 Inch TFT LCD SPFD5408 With Arduino Uno and Mega

This Instructable is to know how to work TFT 2.4 with the controller SPFD5408
with the libraries of Adafruit for TFT.

This library is the Adafruit TFT Libraries changed to works in TFT 2.4 shields with the SPFD5408 controller.

Now it is for Uno and Mega, Teensy is on the way...

It is based in the last version of Adafruit, inclusive with buttons features.

The TFT 2.4 is cheap TFT, that generally is from China, without documentation or libraries for it.

I tested some mcufriend shields.

When we try to use the TFT 2.4 with SPFD5408 controller in sketches with Adafruit TFT libraries, several things can happen :

  • White Screen
  • All screen with noise
  • Touch not works or is inverted (coordinate Y)
  • The colors are inverted
  • And other things

After trying for several days trying the solutions I found on the web, and none works 100%. So I decided to do an library specific to this controller.

I changed the Adafruit libraries for TFT: GFX , TFTLCD and TouchScreen. I join all in this one library, the library SPFD5408, to avoid problems with duplicate libraries and enables also have the original library Adafruit ready for use in other projects with another TFT hardware.

Step 1: Download the SPFD5408 Library

I made available it in my GitHub .

Please download it in: https://github.com/JoaoLopesF/SPFD5408

Attention: This library is ONLY for SPFD5408 TFT controller

If it not work, and you get a blank screen,

please access this site to help you identify the correct library:

http://misc.ws/2015/01/24/lcd-touch-screen-inform...

Step 2: Install

Please follow the details in README.

Step 3: First Test - Graphicstest

I modified the Adafruit graphicstest to work.

Please open the folder in spfd5408_graphicstest sketch example.

Upload this to Arduino Uno

Step 4: Test 2 - Tftpaint

TftPaint is a sketch from Adafruit_TFTLCD, is a one simple paint, but important for learning and test the TFT.

I modified this to work.

Please open the folder in spfd5408_tftpaint sketch example

Step 5: Utility - Calibrate

This TFT is with a resistive touch, and need calibration for accurate the touch

I made one sketch to help this process.

Please open the folder in spfd5408_calibration sketch example.
After the run of this program , you have the parameters of calibration to put in your sketch code.

Comments

author
rich1812 (author)2017-08-04

HI, I have quite a few of these screens for the Arduino aleard. I like it. Now I have a Raspberry Pi, I do really want to buy yet another one just for Pi. Can I convert it to uae on Pi? I think it is a matter or usung the Pi GPIO, or configure Uthe GPIOs to mimic the Adruino, can this be done? Thanks,

author
MostafaV (author)2017-07-18

Dear

My touch screen does not work at all. Do you know whats wrong here?

author
bruhnstv (author)2017-05-16

Thanks much, Joao! I was dreading getting started trying to make these little displays work, with no documentation and the word on the street being that even displays that looked alike might have different controllers. There were several Arduino libraries to pick from, but yours looked relatively straightforward and I liked your Instructables writeup. The libraries and the graphics test sketch worked, kind-of, with same problems as padouet reported, but it was fairly easy to find fixes for those, and now the graphics test sketch is working correctly. (Fixes noted below...)

author
padouet (author)2017-04-23

Hello JoaoLopesF

Thank you for your work and librairies.
With my first tft 2.4 touch screen, it is very good, it is 0x9341 or ili9341..

Now, with my new 2.4 touch screen,I have a big problem., it is 0x9342 or ili9342 and your librairies give a bad result. All other librairies give nothing.

- colors are inverted ;black is 0xffff and white is 0x0000
- texte is mirored
- picture is troncated : landscape <=> paysage

On this no mine video, at 8'15" you can see what I have on screen
https://www.youtube.com/watch?v=5IcvKTeQPiY

The ILI9341 and ILI9342 are very
similar but the latter is configured for 320x240 (960 x 240 outputs)
instead of 240x320 (720 x 320 outputs).

Here data for ili9342

http://www.buydisplay.com/download/ic/ILI9342.pdf

Please, can you release your librairies ?
Thank you very much.

author
bruhnstv (author)padouet2017-05-16

Hello padouet,

I had EXACTLY the same problem this evening when I first started. I fixed each problem with some code changes:

First, the colors. I just swapped the color definitions in the test sketch. There may be a better way, but this works. Swap white and black; swap blue and yellow; swap red and cyan; swap green and magenta. (I need to dig into this a little further. The datasheet for the ILI9341 clearly shows 6 bits per color, but in the sketch, only 16 bits are used to define the colors... Also, it may be possible to send some bit(s) to the ILI9341/2 to invert the colors. I only started to look at the data sheet maybe two hours ago, so there is a lot in it I have not investigated.)

Second, for the reversed text: in SPFD5408_Adafruit_TFTLCD.cpp, look for lines with ILI9341_MADCTL_MX. You should find only two such lines, in case 1 and case 2 of the section that deals with rotation. Remove that part (and the following | ) from those two cases and add it to case 0 and case 3. This swaps the "X" direction for each rotation. (The definition of ILI9341_MADCTL_MX is in registers.h; it's just the bit position of MX in the ILI9341 memory address control register.)

Third, for the portrait <-> landscape problem, I just swapped the sizes in the #defines for TFTWIDTH and TFTHEIGHT. It occurs to me that I might have been able to do it differently; I suppose it could depend on what you want to think of as the width and the height of the display: are you using it in portrait or in landscape orientation. But anyway, that worked for me. It's late; I'll try playing with the other tests tomorrow!

author
GianlucaB25 (author)2017-05-02

Hi! This is the first library that seems at least to partially work with my TFT. But I can't get the touch to work properly: in the calibration sketch I can manage to get to the second screen (after A LOT of touches) but then it doesn't respond at all. Any suggestions?

author
Blun7 (author)2017-04-30

two comments:

1) WHERE do you measure the X resistance for the touchscreen (where do I place the probes of the multimeter)?

2) have thus working on an arduino mega board, but want to use it on a custom board with an AVR atmega128A (64 pin MCU). What physical pins do I use?

author
宇承張 (author)2017-04-07

How to display Chinese characters in 24 inch tft lcd ?

author
jainrk (author)2017-03-06

JoaoLopesF, Thanks for work on this fork. I spent numerous hours trying to figure out to get it to work, until I found your library on GIT hub. Now it all works.

author
rupamr2 made it! (author)2017-02-08

You saved my life. Can't say thanks enough!

Just added Vcc, Gnd, Tx, and Rx and Made it a quick debugging display. It reads from serial data, formats and prints it on display. Thanks.

20170209_045809.jpg
author
Biber_null (author)rupamr22017-03-04

hi @rupamr2, share your code will you?

author
theworldowner. (author)2017-02-19

Thanks!

author
KyleGass (author)2017-02-12

I have the same Problem.

With arduino uno r3. I tried it with the arduino ide 1.0.6 & 1.8.1. I use the same display (2.4 TFT SPFD5408) as you and the "SPFD5408 Library 0.9.2". I tried to start any example but there is only a white screen. Do anyone have an idea how to fix it?

author
abhijit2683 (author)2017-01-07

Hi Joao,

When I am using this library, I am getting white screen. Could you please guide me how I will able to solve this problem?

author
RajarshiK2 (author)abhijit26832017-02-09

Abhijit try here....

https://github.com/samuraijap/TFTLCD-Library

author
feddar (author)2016-09-16

I got one of these shields from China. I tried your library and sketch, but I get a white screen. On serial monitor, it looks like the tests are working (in graphicstest). Any idea what I should do?

author
RajarshiK2 (author)feddar2017-02-09

Pls try here

https://github.com/samuraijap/TFTLCD-Library

author
abhijit2683 (author)feddar2017-01-07

Hi Feddar,

I am facing the same problem. Can you please guide me how you solved this problem?

author
feddar (author)abhijit26832017-01-08

i followed the link below and tried a few. Never got it fully working, though. Oh well, a wasted $7...

author
JoaoLopesF (author)feddar2016-10-05

Hi,

This library is only for SPFD5408 controller,

please try the web site to help you identify the correct controller/library:

http://misc.ws/2015/01/24/lcd-touch-screen-informa...

Regards

Joao

author
feddar (author)JoaoLopesF2016-10-06

Thanks!

author
RajarshiK2 (author)2017-02-09

For those who are recently purchasing the Chinese ones....and the display remains white .........even on trying out the methods described above...you may try and use the library files available here.......

https://github.com/samuraijap/TFTLCD-Library....

Mine started working!!!

Thank you any ways....

author
leakbarax made it! (author)2017-01-16

Thanks a lot, finally done. I spent a night to try several library for this shields, before found very usefull information in this instruction.

IMG_20170117_065925_HDR.jpgIMG_20170117_065929_HDR.jpg
author
ZackH5 (author)2016-12-15

Hi JoaoLopesF,

Can it work with 2.8 inch TFT LCD ILI9341 controller?

author
hbaker6 (author)2016-12-12

Hey there

I bought a near identical TFT shield on ebay (mcufriend) and it came with NO documentation and I cannot read Chinese characters.

Your corrected libraries are the only ones that have managed to work for me and this setup. so I wanted to thank you for that.

The problem I'm having is that the input from the touch screen is inverted to what is shown on the screen. For example if I drag my finger down the long way of the screen the line is displayed going across the short way of the screen and vice versa.

I attempted the fix you sent to slava_gll but it did not work. I am novice level and played around with some of the code(exchanging every X and Y i could find, and switching them back after they didnt work) but nothing I tried worked. Any idea what is causing this and any help to point me in the right direction would be greatly appriciated, Thanks!

author
VictorZ24 (author)2016-12-04

Hi,

There are 5 tiny buil in buttons at the edge of the LCD. Do you know how to read them ?

Thx, VZ

author
slava_gll (author)2016-11-11

mine screen is ok, but my touch not.

it's flipped 180 degrees

pls help

author
JoaoLopesF (author)slava_gll2016-11-12

Hi,

please edit SPFD5408_TouchScreen.cpp,

and change the line 157-164 to:

return TSPoint(x, y, z);

// *** SPFD5408 change -- Begin
// SPFD5408 change, because Y coordinate is inverted in this controller
//return TSPoint(x, 1023 - y, z);
// -- End

------

Some TFT need this invertion

Regards,

Joao

author
slava_gll (author)JoaoLopesF2016-11-12

//return TSPoint(x, y, z);

// *** SPFD5408 change -- Begin

// SPFD5408 change, because Y coordinate is inverted in this controller

return TSPoint(y,x, z);

// -- End

that helped

author
slava_gll (author)JoaoLopesF2016-11-12

Thank you very much

author
StevenM8 (author)2016-11-08

I am really new at this. The screen works great with your help, but the touch doesn't work at all. Any tips?

author
StevenM8 (author)StevenM82016-11-08

Actually, it seems to register a touch on the initial calibration screen, but then it shows the dot stuck in the top left corner with no way to touch it. I went in and tried to move the dot and it is always in the top left corner no matter what I set "tft.setCursor()" to.

author
pkmonster (author)2016-10-31

Can I port your library to Energia platform? Do you have any experience with that?

I really want to use the LCD TFT with my launchpad Stellaris Tiva C11294 XML.

Your library works very well with my Arduino Uno :P

Hope to see your response soon. Thank you so much

author
aprada66 (author)2016-06-25

I tested all examples, and I have duplicated images in all testings.

IMG-20160625-WA0075.jpg.jpeg
author
JoaoLopesF (author)aprada662016-06-26

Hi, I think the controller not is SPFD5408.

Please try the web site to help you identify the correct controller/library:

http://misc.ws/2015/01/24/lcd-touch-screen-informa...

Regards

Joao

author

Hello, I have the same problem, sketch confirms to me that the driver is sdfp5408. The image is duplicated. any advice?

author
Orlando AntonioM made it! (author)Orlando AntonioM2016-10-14

please help me!

20161014_134907.jpg20161014_134816.jpg20161014_134815.jpgID.jpg
author

Hi! I've the same problem and after a big number of test I've finally discover the correct driver for my lcd.

Take a look to the link: https://forum.arduino.cc/index.php?topic=366304.0

My LCD has an identifier of 0x1520 that isn't not in the other sketch that I've tried.

author
JoaoLopesF (author)aprada662016-10-14

Hi, I dont see it before...
Do you see ID in the site misc.wc ?

Regards

Joao

author
Gerardb23 (author)2016-10-12

Hi Joao...

I have a problem...myTFTLCD, has 183.000ohms (183K) between X+ y X-, and then is not posible work it how tactil screen, i buy 2 TFTLCD on different store (isn`t mcufriend) and the value of resistance is self.

I work on it a lot of days, but I can do that....

Thank for your help....

author
WirttiP (author)2016-10-12

Nice job! Thanks for sharing!

author
gadgets_guy (author)2016-09-24

Any assistance would be greatly appreciated, I have a 2.4" LCD and an Arduino mega 2560, tried multiple tutorials on getting the LCD to work with a mega with no success. No issues with an Uno model but at this point I'm stumped as to what's going wrong with the Mega 2560. Below is a sketch which compiles without an error, however the LCD just remains lit/white. Any assistance would be appreciated

// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.

// Modified for SPFD5408 Library by Joao Lopes
// Version 0.9.2 - Rotation for Mega and screen initial

// *** SPFD5408 change -- Begin
#include <SPFD5408_Adafruit_GFX.h> // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
// *** SPFD5408 change -- End

// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 5 // can be a digital pin
#define XP 6 // can be a digital pin
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).

// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;

// -- Setup

void setup(void) {

Serial.begin(9600);

progmemPrintln(PSTR("TFT LCD test"));

#ifdef USE_ADAFRUIT_SHIELD_PINOUT
progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif

tft.reset();

// *** SPFD5408 change -- Begin

// Original code commented

// uint16_t identifier = tft.readID();
//
// if(identifier == 0x9325) {
// Serial.println(F("Found ILI9325 LCD driver"));
// } else if(identifier == 0x9328) {
// Serial.println(F("Found ILI9328 LCD driver"));
// } else if(identifier == 0x7575) {
// Serial.println(F("Found HX8347G LCD driver"));
// } else if(identifier == 0x9341) {
// Serial.println(F("Found ILI9341 LCD driver"));
// } else if(identifier == 0x8357) {
// Serial.println(F("Found HX8357D LCD driver"));
// } else {
// Serial.print(F("Unknown LCD driver chip: "));
// Serial.println(identifier, HEX);
// Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
// Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
// Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
// Serial.println(F("If using the breakout board, it should NOT be #defined!"));
// Serial.println(F("Also if using the breakout, double-check that all wiring"));
// Serial.println(F("matches the tutorial."));
// return;
// }
//
// tft.begin(identifier);

// Code changed to works

tft.begin(0x9341); // SDFP5408

tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial

// *** SPFD5408 change -- End

progmemPrintln(PSTR("Benchmark Time (microseconds)"));

progmemPrint(PSTR("Screen fill "));
Serial.println(testFillScreen());
delay(500);

progmemPrint(PSTR("Text "));
Serial.println(testText());
delay(3000);

progmemPrint(PSTR("Lines "));
Serial.println(testLines(CYAN));
delay(500);

progmemPrint(PSTR("Horiz/Vert Lines "));
Serial.println(testFastLines(RED, BLUE));
delay(500);

progmemPrint(PSTR("Rectangles (outline) "));
Serial.println(testRects(GREEN));
delay(500);

progmemPrint(PSTR("Rectangles (filled) "));
Serial.println(testFilledRects(YELLOW, MAGENTA));
delay(500);

progmemPrint(PSTR("Circles (filled) "));
Serial.println(testFilledCircles(10, MAGENTA));

progmemPrint(PSTR("Circles (outline) "));
Serial.println(testCircles(10, WHITE));
delay(500);

progmemPrint(PSTR("Triangles (outline) "));
Serial.println(testTriangles());
delay(500);

progmemPrint(PSTR("Triangles (filled) "));
Serial.println(testFilledTriangles());
delay(500);

progmemPrint(PSTR("Rounded rects (outline) "));
Serial.println(testRoundRects());
delay(500);

progmemPrint(PSTR("Rounded rects (filled) "));
Serial.println(testFilledRoundRects());
delay(500);

progmemPrintln(PSTR("Done!"));
}

void loop(void) {
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testText();
delay(2000);
}
}

unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(BLACK);
tft.fillScreen(RED);
tft.fillScreen(GREEN);
tft.fillScreen(BLUE);
tft.fillScreen(BLACK);
return micros() - start;
}

unsigned long testText() {
tft.fillScreen(BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextColor(RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");
return micros() - start;
}

unsigned long testLines(uint16_t color) {
unsigned long start, t;
int x1, y1, x2, y2,
w = tft.width(),
h = tft.height();

tft.fillScreen(BLACK);

x1 = y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t = micros() - start; // fillScreen doesn't count against timing

tft.fillScreen(BLACK);

x1 = w - 1;
y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;

tft.fillScreen(BLACK);

x1 = 0;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;

tft.fillScreen(BLACK);

x1 = w - 1;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);

return micros() - start;
}

unsigned long testFastLines(uint16_t color1, uint16_t color2) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height();

tft.fillScreen(BLACK);
start = micros();
for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);

return micros() - start;
}

unsigned long testRects(uint16_t color) {
unsigned long start;
int n, i, i2,
cx = tft.width() / 2,
cy = tft.height() / 2;

tft.fillScreen(BLACK);
n = min(tft.width(), tft.height());
start = micros();
for(i=2; i<n; i+=6) {
i2 = i / 2;
tft.drawRect(cx-i2, cy-i2, i, i, color);
}

return micros() - start;
}

unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
unsigned long start, t = 0;
int n, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(BLACK);
n = min(tft.width(), tft.height());
for(i=n; i>0; i-=6) {
i2 = i / 2;
start = micros();
tft.fillRect(cx-i2, cy-i2, i, i, color1);
t += micros() - start;
// Outlines are not included in timing results
tft.drawRect(cx-i2, cy-i2, i, i, color2);
}

return t;
}

unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;

tft.fillScreen(BLACK);
start = micros();
for(x=radius; x<w; x+=r2) {
for(y=radius; y<h; y+=r2) {
tft.fillCircle(x, y, radius, color);
}
}

return micros() - start;
}

unsigned long testCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, r2 = radius * 2,
w = tft.width() + radius,
h = tft.height() + radius;

// Screen is not cleared for this one -- this is
// intentional and does not affect the reported time.
start = micros();
for(x=0; x<w; x+=r2) {
for(y=0; y<h; y+=r2) {
tft.drawCircle(x, y, radius, color);
}
}

return micros() - start;
}

unsigned long testTriangles() {
unsigned long start;
int n, i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(BLACK);
n = min(cx, cy);
start = micros();
for(i=0; i<n; i+=5) {
tft.drawTriangle(
cx , cy - i, // peak
cx - i, cy + i, // bottom left
cx + i, cy + i, // bottom right
tft.color565(0, 0, i));
}

return micros() - start;
}

unsigned long testFilledTriangles() {
unsigned long start, t = 0;
int i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(BLACK);
start = micros();
for(i=min(cx,cy); i>10; i-=5) {
start = micros();
tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(0, i, i));
t += micros() - start;
tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(i, i, 0));
}

return t;
}

unsigned long testRoundRects() {
unsigned long start;
int w, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(BLACK);
w = min(tft.width(), tft.height());
start = micros();
for(i=0; i<w; i+=6) {
i2 = i / 2;
tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
}

return micros() - start;
}

unsigned long testFilledRoundRects() {
unsigned long start;
int i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(BLACK);
start = micros();
for(i=min(tft.width(), tft.height()); i>20; i-=6) {
i2 = i / 2;
tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
}

return micros() - start;
}

// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
char c;
while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
progmemPrint(str);
Serial.println();
}

author
JoaoLopesF (author)gadgets_guy2016-10-05

Hi, I tested the Library for Uno also Mega, and works well.

You have another mega board ?

Regards

Joao

author
gadgets_guy (author)JoaoLopesF2016-10-05

Hi Joao,

I believe I tried it on two boards, this weekend I'll give it another try. Thanks for the reply.

author
DLPWings (author)gadgets_guy2016-09-25

This is the sketch I'm using... Hope it can help:

// Paint example specifically for the TFTLCD breakout board.

// If using the Arduino shield, use the tftpaint_shield.pde sketch instead!

// DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO

// Modified for SPFD5408 Library by Joao Lopes

// Version 0.9.2 - Rotation for Mega

// *** SPFD5408 change -- Begin

#include <SPFD5408_Adafruit_GFX.h> // Core graphics library

#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library

#include <SPFD5408_TouchScreen.h>

// *** SPFD5408 change -- End

#if defined(__SAM3X8E__)

#undef __FlashStringHelper::F(string_literal)

#define F(string_literal) string_literal

#endif

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:

// For the Arduino Uno, Duemilanove, Diecimila, etc.:

// D0 connects to digital pin 8 (Notice these are

// D1 connects to digital pin 9 NOT in order!)

// D2 connects to digital pin 2

// D3 connects to digital pin 3

// D4 connects to digital pin 4

// D5 connects to digital pin 5

// D6 connects to digital pin 6

// D7 connects to digital pin 7

// For the Arduino Mega, use digital pins 22 through 29

// (on the 2-row header at the end of the board).

// D0 connects to digital pin 22

// D1 connects to digital pin 23

// D2 connects to digital pin 24

// D3 connects to digital pin 25

// D4 connects to digital pin 26

// D5 connects to digital pin 27

// D6 connects to digital pin 28

// D7 connects to digital pin 29

// For the Arduino Due, use digital pins 33 through 40

// (on the 2-row header at the end of the board).

// D0 connects to digital pin 33

// D1 connects to digital pin 34

// D2 connects to digital pin 35

// D3 connects to digital pin 36

// D4 connects to digital pin 37

// D5 connects to digital pin 38

// D6 connects to digital pin 39

// D7 connects to digital pin 40

/*#define YP A2 // must be an analog pin, use "An" notation!

#define XM A1 // must be an analog pin, use "An" notation!

#define YM 6 // can be a digital pin

#define XP 5 // can be a digital pin*/

//BY DLP WINGS

#define YP A1 // must be an analog pin, use "An" notation!

#define XM A2 // must be an analog pin, use "An" notation!

#define YM 5 // can be a digital pin

#define XP 6 // can be a digital pin

// Original values

//#define TS_MINX 150

//#define TS_MINY 120

//#define TS_MAXX 920

//#define TS_MAXY 940

// Calibrate values

#define TS_MINX 170

#define TS_MINY 116

#define TS_MAXX 934

#define TS_MAXY 940

// For better pressure precision, we need to know the resistance

// between X+ and X- Use any multimeter to read it

// For the one we're using, its 300 ohms across the X plate

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3

#define LCD_CD A2

#define LCD_WR A1

#define LCD_RD A0

// optional

#define LCD_RESET A4

// Assign human-readable names to some common 16-bit color values:

#defineBLACK 0x0000

#defineBLUE 0x001F

#defineRED 0xF800

#defineGREEN 0x07E0

#define CYAN 0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define WHITE 0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BOXSIZE 40

#define PENRADIUS 3

int oldcolor, currentcolor;

void setup(void) {

//Serial.begin(9600);

Serial.println(F("Paint!"));

tft.reset();

// *** SPFD5408 change -- Begin

// uint16_t identifier = tft.readID();

//

// if(identifier == 0x9325) {

// Serial.println(F("Found ILI9325 LCD driver"));

// } else if(identifier == 0x9328) {

// Serial.println(F("Found ILI9328 LCD driver"));

// } else if(identifier == 0x7575) {

// Serial.println(F("Found HX8347G LCD driver"));

// } else if(identifier == 0x9341) {

// Serial.println(F("Found ILI9341 LCD driver"));

// } else if(identifier == 0x8357) {

// Serial.println(F("Found HX8357D LCD driver"));

// } else {

// Serial.print(F("Unknown LCD driver chip: "));

// Serial.println(identifier, HEX);

// Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));

// Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));

// Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));

// Serial.println(F("If using the breakout board, it should NOT be #defined!"));

// Serial.println(F("Also if using the breakout, double-check that all wiring"));

// Serial.println(F("matches the tutorial."));

// return;

// }

//

// tft.begin(identifier);

tft.begin(0x9341); // SDFP5408

tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial

// Border

drawBorder();

// Initial screen

tft.setCursor (55, 50);

tft.setTextSize (3);

tft.setTextColor(RED);

tft.println("SPFD5408");

tft.setCursor (65, 85);

tft.println("Library");

tft.setCursor (55, 150);

tft.setTextSize (2);

tft.setTextColor(BLACK);

tft.println("TFT Paint");

tft.setCursor (80, 250);

tft.setTextSize (1);

tft.setTextColor(BLACK);

tft.println("Touch to proceed");

// Wait touch

waitOneTouch();

// *** SPFD5408 change -- End

// -- End

// Paint

tft.fillScreen(BLACK);

tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);

tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);

tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);

tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);

tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);

tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);

// tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);

tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);

currentcolor = RED;

pinMode(13, OUTPUT);

}

#define MINPRESSURE 10

#define MAXPRESSURE 1000

void loop()

{

digitalWrite(13, HIGH);

TSPoint p = ts.getPoint();

digitalWrite(13, LOW);

// if sharing pins, you'll need to fix the directions of the touchscreen pins

//pinMode(XP, OUTPUT);

pinMode(XM, OUTPUT);

pinMode(YP, OUTPUT);

//pinMode(YM, OUTPUT);

// we have some minimum pressure we consider 'valid'

// pressure of 0 means no pressing!

if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

/*

Serial.print("X = "); Serial.print(p.x);

Serial.print("\tY = "); Serial.print(p.y);

Serial.print("\tPressure = "); Serial.println(p.z);

*/

if (p.y < (TS_MINY-5)) {

Serial.println("erase");

// press the bottom of the screen to erase

tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);

}

// scale from 0->1023 to tft.width

// *** SPFD5408 change -- Begin

// Bug in in original code

//p.x = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());

p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());

// *** SPFD5408 change -- End

p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());;

/*

Serial.print("("); Serial.print(p.x);

Serial.print(", "); Serial.print(p.y);

Serial.println(")");*/

if (p.y < BOXSIZE) {

oldcolor = currentcolor;

if (p.x < BOXSIZE) {

currentcolor = RED;

tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x < BOXSIZE*2) {

currentcolor = YELLOW;

tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x < BOXSIZE*3) {

currentcolor = GREEN;

tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x < BOXSIZE*4) {

currentcolor = CYAN;

tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x < BOXSIZE*5) {

currentcolor = BLUE;

tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x < BOXSIZE*6) {

currentcolor = MAGENTA;

tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE);

}

if (oldcolor != currentcolor) {

if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);

if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);

if (oldcolor == GREEN) tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);

if (oldcolor == CYAN) tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);

if (oldcolor == BLUE) tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);

if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);

}

}

if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {

tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);

}

}

}

// Wait one touch

TSPoint waitOneTouch() {

// wait 1 touch to exit function

TSPoint p;

do {

p= ts.getPoint();

pinMode(XM, OUTPUT); //Pins configures again for TFT control

pinMode(YP, OUTPUT);

} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));

return p;

}

void drawBorder () {

// Draw a border

uint16_t width = tft.width() - 1;

uint16_t height = tft.height() - 1;

uint8_t border = 10;

tft.fillScreen(RED);

tft.fillRect(border, border, (width - border * 2), (height - border * 2), WHITE);

}

author
gadgets_guy (author)DLPWings2016-09-25

Thank you

author
DLPWings (author)2016-09-22

Thank you very much for the guide and library!

I got one and tried your library with Arduino Mega, first it didn't work, after some cross-referencing it's working, had to change pin definitions (yup, one more pin-out to try) In the back the shield says:"KeDei 2.4 Inch TFTLCD version 1.0 2016/2/1"

#define YP A1 // must be an analog pin, use "An" notation!

#define XM A2 // must be an analog pin, use "An" notation!

#define YM 5 // can be a digital pin

#define XP 6 // can be a digital pin

author
JoaoLopesF (author)DLPWings2016-10-05

Thanks for your feedback

author
VicrantT (author)2016-05-20

For touch to work
Change
#define YP A3
#define XM A2
#define YM 9
#define XP 8



AND in touchscreen.cpp rather than return TSPoint (1023-x,1023-y,z) change it to return TSPoint (1100-x,1043-y,z) for better results!
May this will be helpful to you!
Let me know if so!
Good luck!

author
russellh31 (author)VicrantT2016-09-30

i found this solution at http://stackoverflow.com/questions/34652970/2-4-inch-tft-lcd-spfd5408-with-arduino-uno-touch-not-working - i'm pleased to say this worked for me too.