Wednesday, August 10, 2016

TFT LCD Touch Screen - ILI9341

I tested a 2.4 inch 240 x 320 resolution TFT LCD touch screen bought from Aliexpress.



That module has LCD display, touch input, and SD card reader. I have written some example programs to use that module and they are available at the following link.
https://github.com/yan9a/TFT_LCD_Touch_Arduino


LCD Display

The following code is for display. Adafruit_GFX and Adafruit_TFTLCD libraries should be put in Arduino libraries folder.

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

//The control pins for the LCD can be assigned to any digital or analog pins.
#define LCD_CS A3 // Chip Select 
#define LCD_CD A2 // Command/Data 
#define LCD_WR A1 // LCD Write 
#define LCD_RD A0 // LCD Read
#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   0xFFFF
#define BLUE    0xFFE0
#define RED     0x07FF
#define GREEN   0xF81F
#define CYAN    0xF800
#define MAGENTA 0x07E0
#define YELLOW  0x001F
#define WHITE   0x0000

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

void setup(void) {
  tft.reset();
  tft.begin(0x9341);
  tft.setRotation(3);
  tft.fillScreen(BLACK);
}

void loop(void) {
    tft.setCursor(20,20);
    tft.setTextColor(RED & BLUE);  
    tft.setTextSize(8);
    tft.println("16:30");

    tft.setCursor(20,100);
    tft.setTextColor(GREEN); 
    tft.setTextSize(4);
    tft.println("2016/Jul/22");

    tft.setCursor(10,150);
    tft.setTextColor(YELLOW); 
    tft.setTextSize(3);
    tft.println("ME 1378 Waso ");

    tft.setCursor(10,180);
    tft.setTextColor(YELLOW); 
    tft.setTextSize(3);
    tft.println("Waning 3");
    delay(1000);
}


Touch Input

An example for touch input is shown below. Touch screen library should also be put in Arduino libraries folder.



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

#define XP 7  
#define XM A1 
#define YM 6   
#define YP A2   

#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define PENRADIUS 3

#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);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

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

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
void setup(void) {
  tft.reset();
  tft.begin(0x9341);
  tft.fillScreen(BLACK);

  tft.setCursor(20,20);
  tft.setTextColor(YELLOW);  
  tft.setTextSize(3);
  tft.println("Touch me!");
  
  pinMode(13, OUTPUT);
}


void loop()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (p.z  >  MINPRESSURE && p.z < MAXPRESSURE) {
    // 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);
    
    tft.fillCircle(p.x, p.y, PENRADIUS, RED);
  }
}



SD Card

This is just a replication of SD card reading and writing example as shown in Arduino website. I just changed CS pin to match this module.
/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include < SPI.h >
#include < SD.h >

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing lcd sd writing.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.