loading

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.

<p>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...)</p>
<p>Hello JoaoLopesF<br><br>Thank you for your work and librairies.<br>With my first tft 2.4 touch screen, it is very good, it is 0x9341 or ili9341..<br><br>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.</p><p>- colors are inverted ;black is 0xffff and white is 0x0000<br>- texte is mirored<br>- picture is troncated : landscape &lt;=&gt; paysage</p><p>On this no mine video, at 8'15&quot; you can see what I have on screen<a href="https://www.youtube.com/watch?v=5IcvKTeQPiY" rel="nofollow"><br>https://www.youtube.com/watch?v=5IcvKTeQPiY</a></p><p>The ILI9341 and ILI9342 are <em>very</em> <br>similar but the latter is configured for 320x240 (960 x 240 outputs) <br>instead of 240x320 (720 x 320 outputs). </p>Here data for ili9342<br><p><a href="http://www.buydisplay.com/download/ic/ILI9342.pdf" rel="nofollow">http://www.buydisplay.com/download/ic/ILI9342.pdf<br></a></p><p>Please, can you release your librairies ?<br>Thank you very much.<br></p>
<p>Hello padouet,</p><p>I had EXACTLY the same problem this evening when I first started. I fixed each problem with some code changes:</p><p>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.)</p><p>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 &quot;X&quot; 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.)</p><p>Third, for the portrait &lt;-&gt; 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!</p>
<p>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?</p>
<p>two comments:</p><p>1) WHERE do you measure the X resistance for the touchscreen (where do I place the probes of the multimeter)?</p><p>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?</p>
<p>How to display Chinese characters in 24 inch tft lcd ?</p>
<p>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.</p>
<p>You saved my life. Can't say thanks enough!</p><p>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. </p>
<p>hi @rupamr2, share your code will you?</p>
<p>Thanks!</p>
<p>I have the same Problem.</p><p>With arduino uno r3. I tried it with the arduino ide 1.0.6 &amp; 1.8.1. I use the same display (2.4 TFT SPFD5408) as you and the &quot;SPFD5408 Library 0.9.2&quot;. I tried to start any example but there is only a white screen. Do anyone have an idea how to fix it?</p>
<p>Hi Joao,</p><p>When I am using this library, I am getting white screen. Could you please guide me how I will able to solve this problem?</p>
<p>Abhijit try here....</p><p>https://github.com/samuraijap/TFTLCD-Library</p>
<p>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?</p>
<p>Pls try here </p><p>https://github.com/samuraijap/TFTLCD-Library</p>
<p>Hi Feddar, </p><p>I am facing the same problem. Can you please guide me how you solved this problem?</p>
<p>i followed the link below and tried a few. Never got it fully working, though. Oh well, a wasted $7...</p>
<p>Hi,</p><p>This library is only for SPFD5408 controller,</p><p>please try the web site to help you identify the correct controller/library:</p><p><a href="http://misc.ws/2015/01/24/lcd-touch-screen-informa..." rel="nofollow">http://misc.ws/2015/01/24/lcd-touch-screen-informa...</a></p><p>Regards</p><p>Joao</p>
Thanks!
<p>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....... </p><p><a href="https://github.com/samuraijap/TFTLCD-Library...." rel="nofollow">https://github.com/samuraijap/TFTLCD-Library....</a></p><p>Mine started working!!!</p><p>Thank you any ways....</p>
<p>Thanks a lot, finally done. I spent a night to try several library for this shields, before found very usefull information in this instruction. </p>
<p>Hi <a href="https://www.instructables.com/member/JoaoLopesF" rel="nofollow">JoaoLopesF</a>,</p><p>Can it work with 2.8 inch TFT LCD ILI9341 controller?</p>
<p>Hey there</p><p>I bought a near identical TFT shield on ebay (mcufriend) and it came with NO documentation and I cannot read Chinese characters.</p><p>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.</p><p>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. </p><p>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!</p>
<p>Hi,</p><p>There are 5 tiny buil in buttons at the edge of the LCD. Do you know how to read them ?</p><p>Thx, VZ</p>
<p>mine screen is ok, but my touch not.</p><p>it's flipped 180 degrees</p><p>pls help</p>
Hi,<br><br>please edit SPFD5408_TouchScreen.cpp,<br><br>and change the line 157-164 to:<br><br> return TSPoint(x, y, z);<br><br> // *** SPFD5408 change -- Begin<br> // SPFD5408 change, because Y coordinate is inverted in this controller<br> //return TSPoint(x, 1023 - y, z);<br> // -- End<br><br>------<br><br>Some TFT need this invertion<br><br>Regards,<br><br>Joao
<p>//return TSPoint(x, y, z);</p><p> // *** SPFD5408 change -- Begin</p><p> // SPFD5408 change, because Y coordinate is inverted in this controller</p><p> return TSPoint(y,x, z);</p><p> // -- End</p><p>that helped</p>
Thank you very much
<p>I am really new at this. The screen works great with your help, but the touch doesn't work at all. Any tips?</p>
<p>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 &quot;tft.setCursor()&quot; to.</p>
<p>Can I port your library to Energia platform? Do you have any experience with that? </p><p>I really want to use the LCD TFT with my launchpad Stellaris Tiva C11294 XML. </p><p>Your library works very well with my Arduino Uno :P </p><p>Hope to see your response soon. Thank you so much</p>
<p>I tested all examples, and I have duplicated images in all testings.</p>
<p>Hi, I think the controller not is SPFD5408.</p><p>Please try the web site to help you identify the correct controller/library:</p><p><a href="http://misc.ws/2015/01/24/lcd-touch-screen-information/" rel="nofollow">http://misc.ws/2015/01/24/lcd-touch-screen-informa...</a></p><p>Regards</p><p>Joao</p>
Hello, I have the same problem, sketch confirms to me that the driver is sdfp5408. The image is duplicated. any advice?
<p>please help me!</p>
<p>Hi! I've the same problem and after a big number of test I've finally discover the correct driver for my lcd.</p><p>Take a look to the link: <a href="https://forum.arduino.cc/index.php?topic=366304.0" rel="nofollow">https://forum.arduino.cc/index.php?topic=366304.0</a></p><p>My LCD has an identifier of 0x1520 that isn't not in the other sketch that I've tried.</p>
Hi, I dont see it before... <br>Do you see ID in the site misc.wc ?<br><br>Regards<br><br>Joao
<p>Hi Joao...</p><p>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. </p><p>I work on it a lot of days, but I can do that....</p><p>Thank for your help....</p>
<p>Nice job! Thanks for sharing!</p>
<p>Any assistance would be greatly appreciated, I have a 2.4&quot; 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<br></p><p>// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY<br>// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.<br>// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.<br><br>// Modified for SPFD5408 Library by Joao Lopes<br>// Version 0.9.2 - Rotation for Mega and screen initial<br><br>// *** SPFD5408 change -- Begin<br>#include &lt;SPFD5408_Adafruit_GFX.h&gt; // Core graphics library<br>#include &lt;SPFD5408_Adafruit_TFTLCD.h&gt; // Hardware-specific library<br>#include &lt;SPFD5408_TouchScreen.h&gt;<br>// *** SPFD5408 change -- End<br><br>// The control pins for the LCD can be assigned to any digital or<br>// analog pins...but we'll use the analog pins as this allows us to<br>// double up the pins with the touch screen (see the TFT paint example).<br>#define LCD_CS A3 // Chip Select goes to Analog 3<br>#define LCD_CD A2 // Command/Data goes to Analog 2<br>#define LCD_WR A1 // LCD Write goes to Analog 1<br>#define LCD_RD A0 // LCD Read goes to Analog 0<br>#define YP A1 // must be an analog pin, use &quot;An&quot; notation!<br>#define XM A2 // must be an analog pin, use &quot;An&quot; notation!<br>#define YM 5 // can be a digital pin<br>#define XP 6 // can be a digital pin<br>#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin<br><br>// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:<br>// For the Arduino Uno, Duemilanove, Diecimila, etc.:<br>// D0 connects to digital pin 8 (Notice these are<br>// D1 connects to digital pin 9 NOT in order!)<br>// D2 connects to digital pin 2<br>// D3 connects to digital pin 3<br>// D4 connects to digital pin 4<br>// D5 connects to digital pin 5<br>// D6 connects to digital pin 6<br>// D7 connects to digital pin 7<br>// For the Arduino Mega, use digital pins 22 through 29<br>// (on the 2-row header at the end of the board).<br><br>// Assign human-readable names to some common 16-bit color values:<br>#define BLACK 0x0000<br>#define BLUE 0x001F<br>#define RED 0xF800<br>#define GREEN 0x07E0<br>#define CYAN 0x07FF<br>#define MAGENTA 0xF81F<br>#define YELLOW 0xFFE0<br>#define WHITE 0xFFFF<br><br>Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);<br>// If using the shield, all control and data lines are fixed, and<br>// a simpler declaration can optionally be used:<br>// Adafruit_TFTLCD tft;<br><br>// -- Setup<br><br>void setup(void) {<br> <br> Serial.begin(9600);<br> <br> progmemPrintln(PSTR(&quot;TFT LCD test&quot;));<br><br>#ifdef USE_ADAFRUIT_SHIELD_PINOUT<br> progmemPrintln(PSTR(&quot;Using Adafruit 2.8\&quot; TFT Arduino Shield Pinout&quot;));<br>#else<br> progmemPrintln(PSTR(&quot;Using Adafruit 2.8\&quot; TFT Breakout Board Pinout&quot;));<br>#endif<br><br> tft.reset();<br> <br> // *** SPFD5408 change -- Begin<br><br>// Original code commented<br><br>// uint16_t identifier = tft.readID();<br>//<br>// if(identifier == 0x9325) {<br>// Serial.println(F(&quot;Found ILI9325 LCD driver&quot;));<br>// } else if(identifier == 0x9328) {<br>// Serial.println(F(&quot;Found ILI9328 LCD driver&quot;));<br>// } else if(identifier == 0x7575) {<br>// Serial.println(F(&quot;Found HX8347G LCD driver&quot;));<br>// } else if(identifier == 0x9341) {<br>// Serial.println(F(&quot;Found ILI9341 LCD driver&quot;));<br>// } else if(identifier == 0x8357) {<br>// Serial.println(F(&quot;Found HX8357D LCD driver&quot;));<br>// } else {<br>// Serial.print(F(&quot;Unknown LCD driver chip: &quot;));<br>// Serial.println(identifier, HEX);<br>// Serial.println(F(&quot;If using the Adafruit 2.8\&quot; TFT Arduino shield, the line:&quot;));<br>// Serial.println(F(&quot; #define USE_ADAFRUIT_SHIELD_PINOUT&quot;));<br>// Serial.println(F(&quot;should appear in the library header (Adafruit_TFT.h).&quot;));<br>// Serial.println(F(&quot;If using the breakout board, it should NOT be #defined!&quot;));<br>// Serial.println(F(&quot;Also if using the breakout, double-check that all wiring&quot;));<br>// Serial.println(F(&quot;matches the tutorial.&quot;));<br>// return;<br>// }<br>//<br>// tft.begin(identifier);<br><br> // Code changed to works <br> <br> tft.begin(0x9341); // SDFP5408<br><br> tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial<br><br> // *** SPFD5408 change -- End<br><br> progmemPrintln(PSTR(&quot;Benchmark Time (microseconds)&quot;));<br><br> progmemPrint(PSTR(&quot;Screen fill &quot;));<br> Serial.println(testFillScreen());<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Text &quot;));<br> Serial.println(testText());<br> delay(3000);<br><br> progmemPrint(PSTR(&quot;Lines &quot;));<br> Serial.println(testLines(CYAN));<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Horiz/Vert Lines &quot;));<br> Serial.println(testFastLines(RED, BLUE));<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Rectangles (outline) &quot;));<br> Serial.println(testRects(GREEN));<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Rectangles (filled) &quot;));<br> Serial.println(testFilledRects(YELLOW, MAGENTA));<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Circles (filled) &quot;));<br> Serial.println(testFilledCircles(10, MAGENTA));<br><br> progmemPrint(PSTR(&quot;Circles (outline) &quot;));<br> Serial.println(testCircles(10, WHITE));<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Triangles (outline) &quot;));<br> Serial.println(testTriangles());<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Triangles (filled) &quot;));<br> Serial.println(testFilledTriangles());<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Rounded rects (outline) &quot;));<br> Serial.println(testRoundRects());<br> delay(500);<br><br> progmemPrint(PSTR(&quot;Rounded rects (filled) &quot;));<br> Serial.println(testFilledRoundRects());<br> delay(500);<br><br> progmemPrintln(PSTR(&quot;Done!&quot;));<br>}<br><br>void loop(void) {<br> for(uint8_t rotation=0; rotation&lt;4; rotation++) {<br> tft.setRotation(rotation);<br> testText();<br> delay(2000);<br> }<br>}<br><br>unsigned long testFillScreen() {<br> unsigned long start = micros();<br> tft.fillScreen(BLACK);<br> tft.fillScreen(RED);<br> tft.fillScreen(GREEN);<br> tft.fillScreen(BLUE);<br> tft.fillScreen(BLACK);<br> return micros() - start;<br>}<br><br>unsigned long testText() {<br> tft.fillScreen(BLACK);<br> unsigned long start = micros();<br> tft.setCursor(0, 0);<br> tft.setTextColor(WHITE); tft.setTextSize(1);<br> tft.println(&quot;Hello World!&quot;);<br> tft.setTextColor(YELLOW); tft.setTextSize(2);<br> tft.println(1234.56);<br> tft.setTextColor(RED); tft.setTextSize(3);<br> tft.println(0xDEADBEEF, HEX);<br> tft.println();<br> tft.setTextColor(GREEN);<br> tft.setTextSize(5);<br> tft.println(&quot;Groop&quot;);<br> tft.setTextSize(2);<br> tft.println(&quot;I implore thee,&quot;);<br> tft.setTextSize(1);<br> tft.println(&quot;my foonting turlingdromes.&quot;);<br> tft.println(&quot;And hooptiously drangle me&quot;);<br> tft.println(&quot;with crinkly bindlewurdles,&quot;);<br> tft.println(&quot;Or I will rend thee&quot;);<br> tft.println(&quot;in the gobberwarts&quot;);<br> tft.println(&quot;with my blurglecruncheon,&quot;);<br> tft.println(&quot;see if I don't!&quot;);<br> return micros() - start;<br>}<br><br>unsigned long testLines(uint16_t color) {<br> unsigned long start, t;<br> int x1, y1, x2, y2,<br> w = tft.width(),<br> h = tft.height();<br><br> tft.fillScreen(BLACK);<br><br> x1 = y1 = 0;<br> y2 = h - 1;<br> start = micros();<br> for(x2=0; x2&lt;w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> x2 = w - 1;<br> for(y2=0; y2&lt;h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> t = micros() - start; // fillScreen doesn't count against timing<br><br> tft.fillScreen(BLACK);<br><br> x1 = w - 1;<br> y1 = 0;<br> y2 = h - 1;<br> start = micros();<br> for(x2=0; x2&lt;w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> x2 = 0;<br> for(y2=0; y2&lt;h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> t += micros() - start;<br><br> tft.fillScreen(BLACK);<br><br> x1 = 0;<br> y1 = h - 1;<br> y2 = 0;<br> start = micros();<br> for(x2=0; x2&lt;w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> x2 = w - 1;<br> for(y2=0; y2&lt;h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> t += micros() - start;<br><br> tft.fillScreen(BLACK);<br><br> x1 = w - 1;<br> y1 = h - 1;<br> y2 = 0;<br> start = micros();<br> for(x2=0; x2&lt;w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);<br> x2 = 0;<br> for(y2=0; y2&lt;h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);<br><br> return micros() - start;<br>}<br><br>unsigned long testFastLines(uint16_t color1, uint16_t color2) {<br> unsigned long start;<br> int x, y, w = tft.width(), h = tft.height();<br><br> tft.fillScreen(BLACK);<br> start = micros();<br> for(y=0; y&lt;h; y+=5) tft.drawFastHLine(0, y, w, color1);<br> for(x=0; x&lt;w; x+=5) tft.drawFastVLine(x, 0, h, color2);<br><br> return micros() - start;<br>}<br><br>unsigned long testRects(uint16_t color) {<br> unsigned long start;<br> int n, i, i2,<br> cx = tft.width() / 2,<br> cy = tft.height() / 2;<br><br> tft.fillScreen(BLACK);<br> n = min(tft.width(), tft.height());<br> start = micros();<br> for(i=2; i&lt;n; i+=6) {<br> i2 = i / 2;<br> tft.drawRect(cx-i2, cy-i2, i, i, color);<br> }<br><br> return micros() - start;<br>}<br><br>unsigned long testFilledRects(uint16_t color1, uint16_t color2) {<br> unsigned long start, t = 0;<br> int n, i, i2,<br> cx = tft.width() / 2 - 1,<br> cy = tft.height() / 2 - 1;<br><br> tft.fillScreen(BLACK);<br> n = min(tft.width(), tft.height());<br> for(i=n; i&gt;0; i-=6) {<br> i2 = i / 2;<br> start = micros();<br> tft.fillRect(cx-i2, cy-i2, i, i, color1);<br> t += micros() - start;<br> // Outlines are not included in timing results<br> tft.drawRect(cx-i2, cy-i2, i, i, color2);<br> }<br><br> return t;<br>}<br><br>unsigned long testFilledCircles(uint8_t radius, uint16_t color) {<br> unsigned long start;<br> int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;<br><br> tft.fillScreen(BLACK);<br> start = micros();<br> for(x=radius; x&lt;w; x+=r2) {<br> for(y=radius; y&lt;h; y+=r2) {<br> tft.fillCircle(x, y, radius, color);<br> }<br> }<br><br> return micros() - start;<br>}<br><br>unsigned long testCircles(uint8_t radius, uint16_t color) {<br> unsigned long start;<br> int x, y, r2 = radius * 2,<br> w = tft.width() + radius,<br> h = tft.height() + radius;<br><br> // Screen is not cleared for this one -- this is<br> // intentional and does not affect the reported time.<br> start = micros();<br> for(x=0; x&lt;w; x+=r2) {<br> for(y=0; y&lt;h; y+=r2) {<br> tft.drawCircle(x, y, radius, color);<br> }<br> }<br><br> return micros() - start;<br>}<br><br>unsigned long testTriangles() {<br> unsigned long start;<br> int n, i, cx = tft.width() / 2 - 1,<br> cy = tft.height() / 2 - 1;<br><br> tft.fillScreen(BLACK);<br> n = min(cx, cy);<br> start = micros();<br> for(i=0; i&lt;n; i+=5) {<br> tft.drawTriangle(<br> cx , cy - i, // peak<br> cx - i, cy + i, // bottom left<br> cx + i, cy + i, // bottom right<br> tft.color565(0, 0, i));<br> }<br><br> return micros() - start;<br>}<br><br>unsigned long testFilledTriangles() {<br> unsigned long start, t = 0;<br> int i, cx = tft.width() / 2 - 1,<br> cy = tft.height() / 2 - 1;<br><br> tft.fillScreen(BLACK);<br> start = micros();<br> for(i=min(cx,cy); i&gt;10; i-=5) {<br> start = micros();<br> tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,<br> tft.color565(0, i, i));<br> t += micros() - start;<br> tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,<br> tft.color565(i, i, 0));<br> }<br><br> return t;<br>}<br><br>unsigned long testRoundRects() {<br> unsigned long start;<br> int w, i, i2,<br> cx = tft.width() / 2 - 1,<br> cy = tft.height() / 2 - 1;<br><br> tft.fillScreen(BLACK);<br> w = min(tft.width(), tft.height());<br> start = micros();<br> for(i=0; i&lt;w; i+=6) {<br> i2 = i / 2;<br> tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));<br> }<br><br> return micros() - start;<br>}<br><br>unsigned long testFilledRoundRects() {<br> unsigned long start;<br> int i, i2,<br> cx = tft.width() / 2 - 1,<br> cy = tft.height() / 2 - 1;<br><br> tft.fillScreen(BLACK);<br> start = micros();<br> for(i=min(tft.width(), tft.height()); i&gt;20; i-=6) {<br> i2 = i / 2;<br> tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));<br> }<br><br> return micros() - start;<br>}<br><br>// Copy string from flash to serial port<br>// Source string MUST be inside a PSTR() declaration!<br>void progmemPrint(const char *str) {<br> char c;<br> while(c = pgm_read_byte(str++)) Serial.print(c);<br>}<br><br>// Same as above, with trailing newline<br>void progmemPrintln(const char *str) {<br> progmemPrint(str);<br> Serial.println();<br>}</p>
<p>Hi, I tested the Library for Uno also Mega, and works well.</p><p>You have another mega board ?</p><p>Regards</p><p>Joao</p>
<p>Hi Joao,</p><p>I believe I tried it on two boards, this weekend I'll give it another try. Thanks for the reply.</p>
<p>This is the sketch I'm using... Hope it can help:</p><p>// Paint example specifically for the TFTLCD breakout board.</p><p>// If using the Arduino shield, use the tftpaint_shield.pde sketch instead!</p><p>// DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO</p><p>// Modified for SPFD5408 Library by Joao Lopes</p><p>// Version 0.9.2 - Rotation for Mega</p><p>// *** SPFD5408 change -- Begin</p><p>#include &lt;SPFD5408_Adafruit_GFX.h&gt; // Core graphics library</p><p>#include &lt;SPFD5408_Adafruit_TFTLCD.h&gt; // Hardware-specific library</p><p>#include &lt;SPFD5408_TouchScreen.h&gt;</p><p>// *** SPFD5408 change -- End</p><p>#if defined(__SAM3X8E__)</p><p> #undef __FlashStringHelper::F(string_literal)</p><p> #define F(string_literal) string_literal</p><p>#endif</p><p>// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:</p><p>// For the Arduino Uno, Duemilanove, Diecimila, etc.:</p><p>// D0 connects to digital pin 8 (Notice these are</p><p>// D1 connects to digital pin 9 NOT in order!)</p><p>// D2 connects to digital pin 2</p><p>// D3 connects to digital pin 3</p><p>// D4 connects to digital pin 4</p><p>// D5 connects to digital pin 5</p><p>// D6 connects to digital pin 6</p><p>// D7 connects to digital pin 7</p><p>// For the Arduino Mega, use digital pins 22 through 29</p><p>// (on the 2-row header at the end of the board).</p><p>// D0 connects to digital pin 22</p><p>// D1 connects to digital pin 23</p><p>// D2 connects to digital pin 24</p><p>// D3 connects to digital pin 25</p><p>// D4 connects to digital pin 26</p><p>// D5 connects to digital pin 27</p><p>// D6 connects to digital pin 28</p><p>// D7 connects to digital pin 29</p><p>// For the Arduino Due, use digital pins 33 through 40</p><p>// (on the 2-row header at the end of the board).</p><p>// D0 connects to digital pin 33</p><p>// D1 connects to digital pin 34</p><p>// D2 connects to digital pin 35</p><p>// D3 connects to digital pin 36</p><p>// D4 connects to digital pin 37</p><p>// D5 connects to digital pin 38</p><p>// D6 connects to digital pin 39</p><p>// D7 connects to digital pin 40</p><p>/*#define YP A2 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define XM A1 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define YM 6 // can be a digital pin</p><p>#define XP 5 // can be a digital pin*/</p><p>//BY DLP WINGS</p><p>#define YP A1 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define XM A2 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define YM 5 // can be a digital pin</p><p>#define XP 6 // can be a digital pin</p><p>// Original values</p><p>//#define TS_MINX 150</p><p>//#define TS_MINY 120</p><p>//#define TS_MAXX 920</p><p>//#define TS_MAXY 940</p><p>// Calibrate values</p><p>#define TS_MINX 170</p><p>#define TS_MINY 116</p><p>#define TS_MAXX 934</p><p>#define TS_MAXY 940</p><p>// For better pressure precision, we need to know the resistance</p><p>// between X+ and X- Use any multimeter to read it</p><p>// For the one we're using, its 300 ohms across the X plate</p><p>TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);</p><p>#define LCD_CS A3</p><p>#define LCD_CD A2</p><p>#define LCD_WR A1</p><p>#define LCD_RD A0</p><p>// optional</p><p>#define LCD_RESET A4</p><p>// Assign human-readable names to some common 16-bit color values:</p><p>#defineBLACK 0x0000</p><p>#defineBLUE 0x001F</p><p>#defineRED 0xF800</p><p>#defineGREEN 0x07E0</p><p>#define CYAN 0x07FF</p><p>#define MAGENTA 0xF81F</p><p>#define YELLOW 0xFFE0</p><p>#define WHITE 0xFFFF</p><p>Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);</p><p>#define BOXSIZE 40</p><p>#define PENRADIUS 3</p><p>int oldcolor, currentcolor;</p><p>void setup(void) {</p><p> //Serial.begin(9600);</p><p> Serial.println(F(&quot;Paint!&quot;));</p><p> tft.reset();</p><p> // *** SPFD5408 change -- Begin</p><p>// uint16_t identifier = tft.readID();</p><p>//</p><p>// if(identifier == 0x9325) {</p><p>// Serial.println(F(&quot;Found ILI9325 LCD driver&quot;));</p><p>// } else if(identifier == 0x9328) {</p><p>// Serial.println(F(&quot;Found ILI9328 LCD driver&quot;));</p><p>// } else if(identifier == 0x7575) {</p><p>// Serial.println(F(&quot;Found HX8347G LCD driver&quot;));</p><p>// } else if(identifier == 0x9341) {</p><p>// Serial.println(F(&quot;Found ILI9341 LCD driver&quot;));</p><p>// } else if(identifier == 0x8357) {</p><p>// Serial.println(F(&quot;Found HX8357D LCD driver&quot;));</p><p>// } else {</p><p>// Serial.print(F(&quot;Unknown LCD driver chip: &quot;));</p><p>// Serial.println(identifier, HEX);</p><p>// Serial.println(F(&quot;If using the Adafruit 2.8\&quot; TFT Arduino shield, the line:&quot;));</p><p>// Serial.println(F(&quot; #define USE_ADAFRUIT_SHIELD_PINOUT&quot;));</p><p>// Serial.println(F(&quot;should appear in the library header (Adafruit_TFT.h).&quot;));</p><p>// Serial.println(F(&quot;If using the breakout board, it should NOT be #defined!&quot;));</p><p>// Serial.println(F(&quot;Also if using the breakout, double-check that all wiring&quot;));</p><p>// Serial.println(F(&quot;matches the tutorial.&quot;));</p><p>// return;</p><p>// }</p><p>//</p><p>// tft.begin(identifier);</p><p> tft.begin(0x9341); // SDFP5408</p><p> tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial</p><p> // Border</p><p> drawBorder();</p><p> // Initial screen</p><p> tft.setCursor (55, 50);</p><p> tft.setTextSize (3);</p><p> tft.setTextColor(RED);</p><p> tft.println(&quot;SPFD5408&quot;);</p><p> tft.setCursor (65, 85);</p><p> tft.println(&quot;Library&quot;);</p><p> tft.setCursor (55, 150);</p><p> tft.setTextSize (2);</p><p> tft.setTextColor(BLACK);</p><p> tft.println(&quot;TFT Paint&quot;);</p><p> tft.setCursor (80, 250);</p><p> tft.setTextSize (1);</p><p> tft.setTextColor(BLACK);</p><p> tft.println(&quot;Touch to proceed&quot;);</p><p> // Wait touch</p><p> waitOneTouch();</p><p>// *** SPFD5408 change -- End</p><p> // -- End</p><p> // Paint</p><p> tft.fillScreen(BLACK);</p><p> tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);</p><p> tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);</p><p> tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);</p><p> tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);</p><p> tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);</p><p> tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);</p><p> // tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> currentcolor = RED;</p><p> pinMode(13, OUTPUT);</p><p>}</p><p>#define MINPRESSURE 10</p><p>#define MAXPRESSURE 1000</p><p>void loop()</p><p>{</p><p> digitalWrite(13, HIGH);</p><p> TSPoint p = ts.getPoint();</p><p> digitalWrite(13, LOW);</p><p> // if sharing pins, you'll need to fix the directions of the touchscreen pins</p><p> //pinMode(XP, OUTPUT);</p><p> pinMode(XM, OUTPUT);</p><p> pinMode(YP, OUTPUT);</p><p> //pinMode(YM, OUTPUT);</p><p> // we have some minimum pressure we consider 'valid'</p><p> // pressure of 0 means no pressing!</p><p> if (p.z &gt; MINPRESSURE &amp;&amp; p.z &lt; MAXPRESSURE) {</p><p> /* </p><p> Serial.print(&quot;X = &quot;); Serial.print(p.x);</p><p> Serial.print(&quot;\tY = &quot;); Serial.print(p.y);</p><p> Serial.print(&quot;\tPressure = &quot;); Serial.println(p.z);</p><p> */</p><p> if (p.y &lt; (TS_MINY-5)) {</p><p> Serial.println(&quot;erase&quot;);</p><p> // press the bottom of the screen to erase </p><p> tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);</p><p> }</p><p> // scale from 0-&gt;1023 to tft.width</p><p> // *** SPFD5408 change -- Begin</p><p> // Bug in in original code</p><p> //p.x = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());</p><p> p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());</p><p> // *** SPFD5408 change -- End</p><p> p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());;</p><p> /*</p><p> Serial.print(&quot;(&quot;); Serial.print(p.x);</p><p> Serial.print(&quot;, &quot;); Serial.print(p.y);</p><p> Serial.println(&quot;)&quot;);*/</p><p> if (p.y &lt; BOXSIZE) {</p><p> oldcolor = currentcolor;</p><p> if (p.x &lt; BOXSIZE) { </p><p> currentcolor = RED; </p><p> tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> } else if (p.x &lt; BOXSIZE*2) {</p><p> currentcolor = YELLOW;</p><p> tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> } else if (p.x &lt; BOXSIZE*3) {</p><p> currentcolor = GREEN;</p><p> tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> } else if (p.x &lt; BOXSIZE*4) {</p><p> currentcolor = CYAN;</p><p> tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> } else if (p.x &lt; BOXSIZE*5) {</p><p> currentcolor = BLUE;</p><p> tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> } else if (p.x &lt; BOXSIZE*6) {</p><p> currentcolor = MAGENTA;</p><p> tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE);</p><p> }</p><p> if (oldcolor != currentcolor) {</p><p> if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);</p><p> if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);</p><p> if (oldcolor == GREEN) tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);</p><p> if (oldcolor == CYAN) tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);</p><p> if (oldcolor == BLUE) tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);</p><p> if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);</p><p> }</p><p> }</p><p> if (((p.y-PENRADIUS) &gt; BOXSIZE) &amp;&amp; ((p.y+PENRADIUS) &lt; tft.height())) {</p><p> tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);</p><p> }</p><p> }</p><p>}</p><p>// Wait one touch</p><p>TSPoint waitOneTouch() {</p><p> // wait 1 touch to exit function</p><p> TSPoint p;</p><p> do {</p><p> p= ts.getPoint(); </p><p> pinMode(XM, OUTPUT); //Pins configures again for TFT control</p><p> pinMode(YP, OUTPUT);</p><p> } while((p.z &lt; MINPRESSURE )|| (p.z &gt; MAXPRESSURE));</p><p> return p;</p><p>}</p><p>void drawBorder () {</p><p> // Draw a border</p><p> uint16_t width = tft.width() - 1;</p><p> uint16_t height = tft.height() - 1;</p><p> uint8_t border = 10;</p><p> tft.fillScreen(RED);</p><p> tft.fillRect(border, border, (width - border * 2), (height - border * 2), WHITE);</p><p>}</p>
<p>Thank you</p>
<p>Thank you very much for the guide and library!</p><p>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:&quot;KeDei 2.4 Inch TFTLCD version 1.0 2016/2/1&quot;</p><p>#define YP A1 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define XM A2 // must be an analog pin, use &quot;An&quot; notation!</p><p>#define YM 5 // can be a digital pin</p><p>#define XP 6 // can be a digital pin</p>
<p>Thanks for your feedback</p>
For touch to work <br>Change <br>#define YP A3 <br>#define XM A2<br>#define YM 9<br>#define XP 8<br><br><br><br>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!<br>May this will be helpful to you!<br>Let me know if so!<br>Good luck!
<p>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.</p>
<p>thanks a lot, i spent hours in attempts to fix touch.</p>
<p>thanks, that worked for me ! somehow the function in the library returns reverse result. </p>

About This Instructable

199,043views

123favorites

License:

More by JoaoLopesF:Remote Debug for Arduino ESP8266 Over Telnet How to use 2.4 inch TFT LCD SPFD5408 with Arduino Uno and Mega 
Add instructable to: