Friday, November 14, 2014

2.4" touch TFT LCD shield

   I purchased from ebay  a cheap 2.4" touch TFT LCD shield for Arduino Uno:
   For control the display I try library recomanded by buyer but not work at me. I search on net and I found article named A 2.4″ TFT TOUCHSCREEN SHIELD FOR ARDUINO
   Smoke and Wires change TFT libray from Adafruit for works with this display. The new library is SWTFT. You must have alse Touch Screen library and Adafruit GFX library.
   First, I tryed examples from SWTFT library and I made a movie:
and few pics:
   For understund the example from SWTFT library, I change the sketch and my results are:


   Finally, I try this:

and my sketch is:
// Original code provided by Smoke And Wires
// http://www.smokeandwires.co.nz
// This code has been taken from the Adafruit TFT Library and modified
//  by us for use with our TFT Shields / Modules
// For original code / licensing please refer to
// https://github.com/adafruit/TFTLCD-Library

// adapted sketch by niq_ro from http://arduinotehniq.blogspot.com/
// ver. 1m5 - 13.11.2014, Craiova - Romania

#include <Adafruit_GFX.h>    // Core graphics library
#include "SWTFT.h" // Hardware-specific library

// 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 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).


#include <Adafruit_GFX.h>    // Core graphics library
#include <SWTFT.h> // Hardware-specific library
#include <TouchScreen.h>




#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#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);

// 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
//#define ROZ     0xFD20
#define ROZ     0xFBE0
#define GRI     0xBDF7
// http://stackoverflow.com/questions/13720937/c-defined-16bit-high-color
// http://wiibrew.org/wiki/U16_colors

SWTFT tft;

#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;
int ics; 

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("Paint!"));
  
  tft.reset();
  
  uint16_t identifier = tft.readID();

  Serial.print(F("LCD driver chip: "));
  Serial.println(identifier, HEX);
    

  tft.begin(identifier);

  tft.fillScreen(BLACK);
  tft.fillRect(0, 0, 320, 240, BLACK);
  tft.setRotation(3);
  tft.setCursor(30, 100);
  tft.setTextColor(RED);  tft.setTextSize(3);
  tft.println("LCD driver chip: ");
  tft.setCursor(100, 150);
  tft.setTextColor(BLUE);
  tft.println(identifier, HEX);

delay(2000);
tft.fillRect(0, 0, 320, 240, BLACK);
  
tft.setRotation(0);
  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  tft.fillRect(0, BOXSIZE, BOXSIZE, BOXSIZE, YELLOW);
  tft.fillRect(0, BOXSIZE*2, BOXSIZE, BOXSIZE, GREEN);
  tft.fillRect(0, BOXSIZE*3, BOXSIZE, BOXSIZE, CYAN);
  tft.fillRect(0, BOXSIZE*4, BOXSIZE, BOXSIZE, BLUE);
  tft.fillRect(0, BOXSIZE*5, BOXSIZE, BOXSIZE, MAGENTA);
  tft.fillRect(0, BOXSIZE*6, BOXSIZE, BOXSIZE, GRI);
  tft.fillRect(0, BOXSIZE*7, BOXSIZE, BOXSIZE,  ROZ);
//tft.fillRect(BOXSIZE, BOXSIZE, BOXSIZE, BOXSIZE, WHITE);
 
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  currentcolor = RED;


 cifre (WHITE);
  pinMode(13, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  digitalWrite(13, HIGH);
  // Recently Point was renamed TSPoint in the TouchScreen library
  // If you are using an older version of the library, use the
  // commented definition instead.
  // Point p = ts.getPoint();
  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)) stergere();
    // scale from 0->1023 to tft.width
    p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, tft.width(), 0));
    p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
  /*  
    Serial.print("("); Serial.print(p.x);
    Serial.print(", "); Serial.print(p.y);
    Serial.println(")");
  */  
    if (p.x < BOXSIZE) {
       oldcolor = currentcolor;

       if (p.y < BOXSIZE) { 
         currentcolor = RED; 
         tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); 
//         text (currentcolor);
       } else if (p.y < BOXSIZE*2) {
         currentcolor = YELLOW;
         tft.drawRect(0, BOXSIZE, BOXSIZE, BOXSIZE, WHITE);
//         text (currentcolor);
       } else if (p.y < BOXSIZE*3) {
         currentcolor = GREEN;
         tft.drawRect(0, BOXSIZE*2, BOXSIZE, BOXSIZE, WHITE);
//         text (currentcolor);
       } else if (p.y < BOXSIZE*4) {
         currentcolor = CYAN;
         tft.drawRect(0, BOXSIZE*3, BOXSIZE, BOXSIZE, WHITE);       
//         text (currentcolor);
       } else if (p.y < BOXSIZE*5) {
         currentcolor = BLUE;
         tft.drawRect(0, BOXSIZE*4, BOXSIZE, BOXSIZE, WHITE);        
//         text (currentcolor);
       } else if (p.y < BOXSIZE*6) {
         currentcolor = MAGENTA;
         tft.drawRect(0,BOXSIZE*5, BOXSIZE, BOXSIZE, WHITE);        
//         text (currentcolor);
       } else if (p.y < BOXSIZE*7) {
         currentcolor = GRI;
         tft.drawRect(0,BOXSIZE*6, BOXSIZE, BOXSIZE, WHITE);        
//         text (currentcolor);
       } else if (p.y < BOXSIZE*8) {
         currentcolor = ROZ;
         tft.drawRect(0,BOXSIZE*7, BOXSIZE, BOXSIZE, WHITE);
         stergere();        
       }

text (currentcolor);
//cifre (currentcolor);
       if (oldcolor != currentcolor) {
          if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
          if (oldcolor == YELLOW) tft.fillRect(0, BOXSIZE, BOXSIZE, BOXSIZE, YELLOW);
          if (oldcolor == GREEN) tft.fillRect(0, BOXSIZE*2, BOXSIZE, BOXSIZE, GREEN);
          if (oldcolor == CYAN) tft.fillRect(0, BOXSIZE*3, BOXSIZE, BOXSIZE, CYAN);
          if (oldcolor == BLUE) tft.fillRect(0, BOXSIZE*4, BOXSIZE, BOXSIZE, BLUE);
          if (oldcolor == MAGENTA) tft.fillRect(0, BOXSIZE*5, BOXSIZE, BOXSIZE, MAGENTA);
          if (oldcolor == GRI) tft.fillRect(0, BOXSIZE*6, BOXSIZE, BOXSIZE, GRI);
          if (oldcolor == ROZ) tft.fillRect(0, BOXSIZE*7, BOXSIZE, BOXSIZE, ROZ);
     cifre (currentcolor);  
     }
      
    }
   // if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
/*
if (((p.y-PENRADIUS) > 5) && ((p.y+PENRADIUS) < tft.height())) {
   tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
    }
*/
if (((p.x-PENRADIUS) > BOXSIZE) && ((p.x+PENRADIUS) < tft.width())) {
   tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
    }

  }
}

void text (int culoare) 
{
tft.setRotation(3);
tft.setCursor(70, 80);
          tft.setTextColor(culoare);  tft.setTextSize(5);
 /*         tft.println("niq_ro");
          tft.setCursor(12, 130); tft.setTextSize(3);
          tft.println("www.tehnic.go.ro");
          tft.setCursor(20, 165); tft.setTextSize(2);
          tft.println("nicuflorica.blogspot.ro");
*/
          tft.setCursor(5, 220); tft.setTextSize(2);
          tft.println("arduinotehniq.blogspot.com");
tft.setRotation(0);
}

void cifre (int culoare)
{
// number for "buttons"
 tft.setRotation(3);
 tft.setTextColor(culoare);
 tft.setTextSize(3);
 tft.drawLine(0, 0, 40, 40, culoare);
 tft.drawLine(0, 40, 40, 0, culoare);
// tft.setCursor(15, 10);
// tft.println("1");
 tft.setCursor(55, 10);
 tft.println("1");
 tft.setCursor(95, 10);
 tft.println("2");
 tft.setCursor(135, 10);
 tft.println("3");
 tft.setCursor(175, 10);
 tft.println("4");
 tft.setCursor(215, 10);
 tft.println("5");
 tft.setCursor(255, 10);
 tft.println("6");
 tft.setCursor(295, 10);
 tft.println("7");
  
 tft.setRotation(0);
}

void stergere ()
{
      Serial.println("erase");
      // press the bottom of the screen to erase 
 //     tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
   tft.fillRect(0, 0, 240, 320, BLACK);
   tft.setRotation(0);
  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  tft.fillRect(0, BOXSIZE, BOXSIZE, BOXSIZE, YELLOW);
  tft.fillRect(0, BOXSIZE*2, BOXSIZE, BOXSIZE, GREEN);
  tft.fillRect(0, BOXSIZE*3, BOXSIZE, BOXSIZE, CYAN);
  tft.fillRect(0, BOXSIZE*4, BOXSIZE, BOXSIZE, BLUE);
  tft.fillRect(0, BOXSIZE*5, BOXSIZE, BOXSIZE, MAGENTA);
  tft.fillRect(0, BOXSIZE*6, BOXSIZE, BOXSIZE, GRI);
  tft.fillRect(0, BOXSIZE*7, BOXSIZE, BOXSIZE,  ROZ);
//tft.fillRect(BOXSIZE, BOXSIZE, BOXSIZE, BOXSIZE, WHITE);
 
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  currentcolor = RED;
     cifre (WHITE);  
    }
   

23 comments:

  1. Hi, I have the same board, Arduino uno and problems making it work..
    I did put libraries you mentioned into libraries folder, and display stays white anyway.. :(

    Did you changed anything else? Hardware ID maybe?

    regards

    matjaz

    ReplyDelete
  2. Hi,

    I have the same problem. Followed the instructions and put things in the libraries.
    Compiles and uploads OK, the sketch runs fine as can be seen on the serial monitor, see attached.

    Did the hardware change??

    Looking for a solution.

    Regards,

    Erik

    LCD driver chip: 154
    Benchmark Time (microseconds)
    Screen fill 1321524
    Text 191456
    Lines 1075816
    Horiz/Vert Lines 137028
    Rectangles (outline) 99828
    Rectangles (filled) 3068580
    Circles (filled) 1211164
    Circles (outline) 463832
    Triangles (outline) 342404
    Triangles (filled) 1663364
    Rounded rects (outline) 221344
    Rounded rects (filled) 3585636
    Done!

    ReplyDelete
    Replies
    1. Hi,
      I have your same problem, driver chip ID is 154 and it remains white.
      Did you solve your problem?

      Thanks

      Delete
    2. driver chip id 154 and remains white screen

      Delete
  3. I don't have any problem with this library.. but at mine ID is 9325... try to give some data from display buyer...
    try with ucglib library but you need know what name driver ... see name on phisical chips from display board.. and search on net..

    ReplyDelete
  4. DIsplays with id=154 uses a different hardware. I got it to work using a different TFTLCD library
    In the tread http://forum.arduino.cc/index.php?topic=185924.0 a probably working library od old Arduino 0022 is mentioned and they discussed how to change the TFTLCD library to fit to the newer Arduino versions -may be that helps.

    BTW, up to now I did not hve any success to use the touchscreen of the display.

    ReplyDelete
  5. if Lcd Driver Chip: 154
    0x0154
    Samsung S6D0154

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

    if use Arduino mega 2560 change
    pin_magic.h

    https://drive.google.com/folderview?id=0B_beZobIWlVab0MyTFl0OHN3TE0&usp=sharing

    ReplyDelete
    Replies
    1. That's interesting.
      The work of library functions drawPixel and readPixel.

      The program does not work properly

      void loop(void) {
      ...
      testColor=GREEN;
      Serial.print(testColor);
      tft.drawPixel(0, 0, testColor);
      testColor=tft.readPixel(0, 0);
      Serial.print("<->");
      tft.drawPixel(5,5,testColor);
      Serial.println(testColor);
      ...
      }

      And this is - well ...

      void loop(void) {
      ...
      testColor=GREEN;
      Serial.print(testColor);
      tft.drawPixel(2, 0, testColor);
      testColor=tft.readPixel(0, 0);
      Serial.print("<->");
      tft.drawPixel(5,5,testColor);
      Serial.println(testColor);
      ...
      }

      Delete
    2. Lying readPixel().
      This is evident from the pixels on the display.

      Delete
    3. tank you Volkan :) it works

      Delete
  6. I have and LCD shield, which looks exactly the same as the "http://www.mcufriend.com" shield above.
    The chip was identified as 154, and after trying for a while, I got the samuraijap library to work.
    I thought I'd have to use the SHIELD pinout, but apparently the pinout is the same as the breakout pins, so after commenting out the line in the header file again, it worked!

    ReplyDelete
    Replies
    1. Yes, Yes, Yes It work!!!
      I had this library downloaded from another site and it didn't work. At the begining I thought that I had already tested it but in this url https://github.com/samuraijap/TFTLCD-Library there is a new version that include the option for 154 identifier.

      Delete
  7. It work !!
    Thanks

    ReplyDelete
  8. Hello,
    I read with interest the tutorial and tested. everything works perfectly.
    Many thanks for the excellent work.
    Now let me ask a question.
    I would use this display to display data from sensors I2C
    For example, an RTC DS-1307
    My problem is the following to use the display to view text pins are used
    // #define LCD_CS A3 // goes to Chip Select Analog 3
    // #define LCD_CD A2 // Command / Data goes to Analog 2
    // #define LCD_WR A1 // Write LCD goes to Analog 1
    // #define LCD_RD A0 // Read LCD goes to Analog 0

    // #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

    but the pin A4 from the sensors is used as I2C SDA.

    I would like to have from you is an indication of the code and is a diagram for connections from fare.per not have this problem.

    ReplyDelete
    Replies
    1. Did you ever get this working? I have same problem

      Delete
  9. So cool. I love reading about other people's coding journey. I try and fiddle every day. I guess it is more of a hobby for me. I would love to make it more than that. I just think I need to improve before I could even think about making it my job. But I guess I can dream for now.

    Brian Hopkins @ Microtips USA

    ReplyDelete
  10. The 2.4" touch TFT LCD shield is extremely compact and sturdily built for such a small screen size and resolution. There are numerous video clips available on-line which show demonstrations of the lovely vivid colours available to write with. There are six bright writing colours to choose from and all of them are vibrant with a fluorescent tone to them.

    Raymond @ CKS Global Solutions LTD

    ReplyDelete
  11. Very informative and interesting post. It is really a big help. Thank you so much for sharing it with us.
    Touch Screen Displays

    ReplyDelete
  12. Hi All,

    I did what was described:
    1. Installed “SWTFT-Shield” from the extracted archive to arduino Libraries
    2. Installed Touch Screen Library to access the Touch Screen
    3. Installed Adafruit Core Graphics Library

    When I try to run the TFTPaint I get the following error:

    C:\Users\Test\Documents\Arduino\libraries\SWTFT-Shield\SWTFT.cpp: In member function ‘void SWTFT::drawLine3Byte(int16_t, int16_t, int16_t, int16_t, uint8_t, uint8_t, uint8_t)’:

    C:\Users\Test\Documents\Arduino\libraries\SWTFT-Shield\SWTFT.cpp:629:16: error: ‘swap’ was not declared in this scope

    swap(x0, y0);

    ^

    C:\Users\Test\Documents\Arduino\libraries\SWTFT-Shield\SWTFT.cpp:634:16: error: ‘swap’ was not declared in this scope

    swap(x0, x1);

    ^

    exit status 1
    Error compiling for board Arduino/Genuino Mega or Mega 2560.

    Any suggestions how to overcome this error?

    ReplyDelete
  13. Nice Blog, Thanks for sharing with us! Keep Sharing.

    Do you want to buy Resistive Touch Monitors online at affordable rate?


    Buy Resistive Touch Monitors

    ReplyDelete