Every cool project needs display. So today we going to look how to set up 2.4 TFT LCD display on arduino. Full sketch and library downloads included.

 



  1.  We need three libraries. Download 2.5 inch tft touch library from: https://github.com/adafruit/TFTLCD-Library  , https://github.com/adafruit/Adafruit-GFX-Library and https://github.com/adafruit/Touch-Screen-Library or all from one place + examples HERE.
  2. Install the libraries.
  3. Create new sketch and lets begin
  4. Lets include the libraries and define the constants:
    #include <Adafruit_GFX.h>    // Core graphics library
    #include <Adafruit_TFTLCD.h> // Hardware-specific library
    #include <TouchScreen.h>
    
    #define YP A3  // must be an analog pin, use "An" notation!
    #define XM A2  // must be an analog pin, use "An" notation!
    #define YM 9   // can be a digital pin
    #define XP 8   // 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 in our case A2 and digital pin 8.
    TouchScreen ts = TouchScreen(XP, YP, XM, YM, 310);
    
    #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:
    #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);
    
    #define BOXSIZE 40
    #define PENRADIUS 3
    int oldcolor, currentcolor;

     

  5.  Lets create setup loop:
    void setup() {
      Serial.begin(9600);
      Serial.println("Paint!");
      
      tft.reset();
    
    // This part helps to identify the type of LCD driver you have.
    
      uint16_t identifier = tft.readID();
     if(identifier==0x0101)
          identifier=0x9341;
      if(identifier == 0x9325) {
        Serial.println(F("Found ILI9325 LCD driver"));
      } else if(identifier == 0x9328) {
        Serial.println(F("Found ILI9328 LCD driver"));
      } else if(identifier == 0x4535) {
        Serial.println(F("Found LGDP4535 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;
      }
    
      // Start tft display
      tft.begin(identifier);
    
    
      //Fill screen with black.
      tft.fillScreen(BLACK);
      
      pinMode(13, OUTPUT);
    
    }

     

  6. And main loop:
    void loop() {
      
      // Get touch point.
      TSPoint p = ts.getPoint();
      
      // 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);
    
        // scale from 0->1023 to tft.width
        p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
        p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    
        Serial.print("("); Serial.print(p.x);
        Serial.print(", "); Serial.print(p.y);
        Serial.println(")");
      }
    
      //small loop that creates two moving boxes
      //and erases the trace boxes leave.
      
      tft.fillRect(xplace, 0, 1, BOXSIZE, BLACK);
      tft.fillRect(0, xplace, BOXSIZE, 1, BLACK);
      xplace += 1;
    
      tft.fillRect(xplace, 0, BOXSIZE, BOXSIZE, YELLOW);
      tft.fillRect(0, xplace, BOXSIZE, BOXSIZE, YELLOW);
    
      if (xplace > 320){
        xplace = 0;
      }
    
    
      // Sets Cursor and print hello world
      tft.setCursor(50,50);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);  
      tft.println("Hello World");
    
    
      // Create black rectangle to clear the previous millis() text and print new
      tft.setCursor(60,100);
      tft.fillRect(60, 100, 90, 30, BLACK);
      tft.println(millis());
    
      // Print last touch position x y
      if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
        tft.fillScreen(BLACK);
        tft.setCursor(65,150);
        tft.setTextColor(GREEN);
        tft.setTextSize(1);
        tft.print(p.x);
        tft.print("  ");
        tft.println(p.y);
        
      }
    
      // Lets draw some small lines.
      tft.drawFastVLine(200,200,20, RED);
      tft.drawFastHLine(210,200,20, GREEN);
    
    
      // Some shapes
      tft.drawRect(150, 210, 25, 39, BLUE);
      tft.fillCircle(110, 210, 5, CYAN);
      tft.drawTriangle(70,210,70,220,80,215, MAGENTA);
    
      
      delay(3);
    
    }

     

  7. And that’s that. Here we have enough to start creating our own user interfaces and displays.

Full sketch can be seen from here.