This sample program demonstrates the basic text display function of the TFT LCD screen. It can display text strings, floating-point numbers, and hexadecimal numbers on the screen in different colors and font sizes.
The sample code demonstrated here is based on Arduino UNO R3. The operation logic of other microcontrollers is roughly the same, with only minor differences in the code for Arduino UNO R4.
This program relies on two core libraries to realize TFT LCD display functions:
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
The control pins of the LCD screen are assigned to analog pins. This design allows the pins to be shared with the touch screen function:
#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
The core functions of each control pin are described as follows:
The program defines 8 common 16-bit RGB565 color values for easy reference in the code:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
The TFT LCD screen adopts the 16-bit RGB565 color format, where:
Example:
RED = 0xF800: Binary 11111000 00000000, red is full, green and blue are 0WHITE = 0xFFFF: All color channels are fullCreate an instance of the Elegoo_TFTLCD class to operate the LCD screen:
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
All subsequent screen operations are performed through this tft object. The constructor accepts five control pin parameters to establish the connection between the Arduino and the LCD screen.
The setup() function completes the initialization of the serial port and LCD screen. It runs only once when the Arduino is powered on or reset.
void setup(void) {
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
Serial.println(F("Elegoo 2.8\" TFT Screen"));
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.reset();
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 == 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 if(identifier==0x0101) {
identifier=0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
} else if(identifier==0x1111) {
identifier=0x9328;
Serial.println(F("Found 0x9328 LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
identifier=0x9341;
}
tft.begin(identifier);
}
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
Serial.begin(9600): Set the serial baud rate to 9600 for debugging information outputF() macro: Store strings in Flash memory to save RAM resources. Arduino has limited RAM, and using the F() macro for constant strings is a common optimization technique.Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
Call tft.width() and tft.height() to obtain the screen resolution and print it to the serial port for verification.
tft.reset();
Send a reset signal to the LCD screen to restore it to the initial state.
This is the core part of the initialization process. The code identifies the LCD driver chip model through the following steps:
Read Driver ID: uint16_t identifier = tft.readID();
Call the readID() method to read the ID of the LCD driver chip. Different driver chips have different ID values.
Driver Model Matching:
The code uses multiple if-else if statements to match the read ID with known driver models:
0x9325: ILI9325 driver0x9328: ILI9328 driver0x4535: LGDP4535 driver0x7575: HX8347G driver0x9341: ILI9341 driver (the most common driver)0x8357: HX8357D driverSpecial ID Correction:
Some LCD screens may return incorrect ID values due to hardware compatibility issues:
0x0101, it is corrected to 0x9341 (ILI9341)0x1111, it is corrected to 0x9328 (ILI9328)Default Driver Setting:
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
identifier=0x9341;
}
If the driver ID cannot be recognized, the code defaults to using the ILI9341 driver, which is the most widely used driver model and has good compatibility.
Screen Initialization:
tft.begin(identifier);
Call the begin() method with the identified driver ID to initialize the LCD screen. The initialization process includes configuring screen parameters such as display mode, color format, and refresh rate.
The loop() function is the main loop of the Arduino program, which runs repeatedly after the setup() function is executed. It is responsible for displaying text on the screen.
void loop(void) {
tft.fillScreen(BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(RED); tft.setTextSize(1);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.println();
tft.setTextColor(GREEN); tft.setTextSize(2);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.println();
tft.setTextColor(BLUE); tft.setTextSize(3);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
tft.setTextColor(WHITE); tft.setTextSize(4);
tft.println("Hello!");
tft.setTextColor(YELLOW); tft.setTextSize(5);
tft.println("Hello!");
tft.setTextColor(RED); tft.setTextSize(6);
tft.println("Hello!");
tft.println();
tft.println();
delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);
}
tft.fillScreen(BLACK);
Call the fillScreen() method to fill the entire screen with black color, clearing all previous display content and preparing for new content display.
tft.setCursor(0, 0);
Call the setCursor() method to move the text cursor to the top-left corner of the screen (coordinate x=0, y=0). All subsequent text will start displaying from this position.
The program demonstrates text display in different colors and font sizes:
tft.setTextColor(RED); tft.setTextSize(1);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
setTextColor(RED): Set the text color to redsetTextSize(1): Set the font size to 1 (the smallest size)println("Hello World!"): Display the string "Hello World!" and automatically wrap to the next lineprintln(01234.56789): Display the floating-point number 1234.56789println(0xDEADBEEF, HEX): Display the hexadecimal number 0xDEADBEEF in hexadecimal format. This is a commonly used test value in programming.tft.setTextColor(GREEN); tft.setTextSize(2);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
Same content as above, but displayed in green with font size 2.
tft.setTextColor(BLUE); tft.setTextSize(3);
tft.println("Hello World!");
tft.println(01234.56789);
tft.println(0xDEADBEEF, HEX);
Same content as above, but displayed in blue with font size 3.
tft.setTextColor(WHITE); tft.setTextSize(4);
tft.println("Hello!");
tft.setTextColor(YELLOW); tft.setTextSize(5);
tft.println("Hello!");
tft.setTextColor(RED); tft.setTextSize(6);
tft.println("Hello!");
Display "Hello!" in white (size 4), yellow (size 5), and red (size 6) respectively, demonstrating the effect of larger font sizes.
delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);
Delay for a total of 5 seconds (5 * 1000ms). After the delay, the loop() function restarts, clearing the screen and re-displaying all content.
This sample program demonstrates the basic text display function of the TFT LCD screen through the following steps:
The key functions used in the program include:
tft.reset(): Reset the LCD screentft.readID(): Read the driver chip IDtft.begin(): Initialize the LCD screentft.fillScreen(): Fill the entire screen with a specified colortft.setCursor(): Set the text cursor positiontft.setTextColor(): Set the text colortft.setTextSize(): Set the font sizetft.println(): Display text and automatically wrap to the next lineIf you need to modify the code display effect, you can refer to the following aspects for adjustment:
Modify Display Text Content:
tft.println("Hello World!") to change the displayed texttft.println(01234.56789) to change the displayed numeric valuetft.println(0xDEADBEEF, HEX) to change the displayed hexadecimal valueModify Text Color:
tft.setTextColor(RED). Available colors include: BLACK, BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW, WHITEtft.setTextColor(0xF800) represents redModify Font Size:
tft.setTextSize(1), ranging from 1 to 6. Larger numbers mean larger font sizesModify Cursor Position:
tft.setCursor(0, 0) to change the starting display position of the textModify Background Color:
tft.fillScreen(BLACK) to change the screen background colorModify Delay Time:
delay(1000) (in milliseconds) to change the duration of each displaydelay(1000) five times in succession, totaling 5 seconds of delayAdd New Display Content:
tft.setTextColor(), tft.setTextSize(), and tft.println() statements in the loop() function to add new display lines