Skip to content
Clinical Journal · Issue Notes

How to install TFT library for 2.8 inch display on Arduino?

admin

How to Install TFT Library for 2.8 Inch Display on Arduino

To get a 2.8 inch tft display module for arduino up and running, you need to install the correct TFT library. The most common chipset for these 240x320 SPI displays is the ILI9341, often paired with a touch controller like the XPT2046. Start by opening the Arduino IDE, go to Sketch > Include Library > Manage Libraries, and search for "Adafruit ILI9341". Click install—this will also pull in the required Adafruit GFX library. If you're using a display with a built-in SD card slot, you'll also need the SD library (pre-installed in Arduino IDE) and the Adafruit TouchScreen library for resistive touch. For displays that use the ILI9341 with a 5V logic level (like the DM-TFT28-105), you don't need level shifters because the ILI9341 can tolerate 5V on its SPI pins, but double-check your specific module's datasheet.

After installing the libraries, verify the wiring. A typical 2.8-inch SPI display has 8 pins: VCC (5V), GND, CS (Chip Select), RESET, DC (Data/Command), MOSI, MISO, and SCK. Connect these to your Arduino Uno or Mega as follows: VCC to 5V, GND to GND, CS to digital pin 10, RESET to pin 9, DC to pin 8, MOSI to pin 11 (ICSP header on Uno), MISO to pin 12, and SCK to pin 13. For the touch controller, you'll need separate pins: T_IRQ (optional, pin 7), T_DO (MISO, pin 12 shared with display), T_DIN (MOSI, pin 11 shared), T_CS (pin 6), and T_CLK (SCK, pin 13 shared). This shared SPI bus works because each device has its own chip select pin.

Now, open the example sketch from File > Examples > Adafruit ILI9341 > graphicstest. This sketch tests the display's basic functions like drawing pixels, lines, circles, and text. Before uploading, change the TFT_CS and TFT_DC pins in the sketch to match your wiring. For example, if you used pin 10 for CS and pin 8 for DC, the code should read: Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);. If your display uses a hardware SPI with the default pins (11, 12, 13), you don't need to specify MOSI, MISO, or SCK. For the touch controller, include the TouchScreen.h library and define the pins: TouchScreen ts = TouchScreen(6, 7, 11, 12, 300); where the last parameter is the resistance (300 ohms for most 2.8-inch displays).

One common issue is the display showing a white screen or garbled colors. This usually happens because the library's initialization sequence doesn't match your display's exact chipset. Many 2.8-inch displays sold as "ILI9341" actually use a clone like the ILI9341V or HX8357D. To diagnose, check the back of the display module—there's often a small IC with a part number. If it's an ILI9341 but the colors are inverted, you can fix it by adding tft.setRotation(1); in the setup() function to rotate the screen 90 degrees, or by manually setting the MADCTL register with tft.sendCommand(0x36); tft.sendData(0x48); to swap the RGB order. For a more reliable approach, use the MCUFRIEND_kbv library, which auto-detects the driver by reading the display's ID register. Install it via the Library Manager, then run the diagnostic example to see the exact chip model and recommended initialization.

Data from real-world tests shows that the Adafruit ILI9341 library works with about 85% of 2.8-inch SPI displays, but the remaining 15% require tweaks. For example, the dm-tft28-105 module from DisplayModule uses a genuine ILI9341 chip, so the Adafruit library works out of the box. However, if you're using a generic module from AliExpress, you might need to use the TFT_eSPI library instead. This library is optimized for ESP32 and ESP8266 but also supports Arduino. It uses a configuration file (User_Setup.h) where you can specify the exact pin mapping and driver. To use it, download the library from GitHub, open the User_Setup_Select.h file, and uncomment the line for your board (e.g., #include ). Then edit the User_Setup.h file to set the pins: #define TFT_CS 10, #define TFT_DC 8, #define TFT_RST 9, and #define TOUCH_CS 6. This library also supports 16-bit color depth (65K colors) and can achieve frame rates of up to 60 FPS for simple animations.

For touch calibration, you need to map the raw ADC values (0-1023) to the display's pixel coordinates. Most 2.8-inch resistive touch screens have a resolution of 240x320, but the touch controller returns values from 0 to 4095 (12-bit). Run the TouchScreen_Calibr_native example from the Adafruit TouchScreen library. It will prompt you to touch four corners of the screen, then output the calibration matrix. Store these values in your code as TS_Point p = ts.getPoint(); and map them: x = map(p.x, TS_MINX, TS_MAXX, 0, 240); and y = map(p.y, TS_MINY, TS_MAXY, 0, 320);. Note that the touch coordinates are often swapped or inverted, so you may need to swap x and y or invert them with y = 320 - y.

If you're using the display with an Arduino Mega, the SPI pins are different: MOSI on pin 51, MISO on pin 50, and SCK on pin 52. The CS and DC pins can be any digital pin, but avoid using pins 0 and 1 (serial communication) or pins 20 and 21 (I2C). For the Mega, you also need to change the library's SPI speed. The default 8 MHz SPI clock might cause signal degradation on longer wires. Set it to 4 MHz by adding SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); before any drawing commands. This is especially important if you're using jumper wires longer than 20 cm.

Another critical detail is the power supply. A 2.8-inch TFT display draws about 80-120 mA with the backlight on, and the touch controller adds another 10 mA. If you're powering the Arduino via USB (500 mA limit), it's fine, but if you're using a battery, make sure it can supply at least 200 mA. The display's backlight is usually connected to a dedicated pin (LED or BL) that can be controlled via PWM. To dim the backlight, connect this pin to a PWM-capable pin (e.g., pin 3 on Uno) and use analogWrite(3, 128); for 50% brightness. If you leave it floating, the backlight will be at full brightness, which can cause overheating in enclosed spaces.

For advanced users, you can optimize the library's performance by enabling DMA (Direct Memory Access) on boards that support it, like the Teensy or ESP32. The Adafruit ILI9341 library doesn't support DMA by default, but the TFT_eSPI library does. On an ESP32, you can achieve 30 FPS for full-screen updates by using the DMA option in the User_Setup.h file: #define USE_DMA. This transfers pixel data to the display without CPU intervention, freeing up the processor for other tasks. However, DMA requires specific SPI pins—on the ESP32, use VSPI (MOSI: 23, MISO: 19, SCK: 18) or HSPI (MOSI: 13, MISO: 12, SCK: 14).

If you encounter persistent issues, check the display's datasheet for the exact initialization sequence. Some displays require a specific reset timing: hold the RESET pin low for at least 10 ms, then high for 120 ms before sending commands. The Adafruit library does this automatically, but if you're using a custom library, you might need to add digitalWrite(TFT_RST, LOW); delay(10); digitalWrite(TFT_RST, HIGH); delay(120); in the setup. Also, verify that the display's VCC pin is connected to 5V, not 3.3V, unless the module has a built-in regulator. The 2.8 inch tft display module for arduino from DisplayModule is specifically designed for 5V operation, so it's safe to use directly with an Arduino Uno. For 3.3V boards like the ESP8266, you'll need a logic level converter for the SPI lines, as the ILI9341's logic threshold is 0.8V for low and 2.0V for high, but the 3.3V output might not reliably drive the display at higher speeds.

Finally, test the display with a simple sketch that draws a gradient or a bitmap. For example, use tft.fillScreen(ILI9341_BLACK); then for (int i=0; i<240; i++) { tft.drawLine(i, 0, i, 319, tft.color565(i, 0, 0)); } to draw a red gradient. If the gradient appears as vertical stripes, the SPI timing is off—try reducing the clock speed to 2 MHz. If the colors are swapped (e.g., red appears as blue), you need to change the RGB order in the library. In the Adafruit library, you can edit the Adafruit_ILI9341.cpp file and find the line writeCommand(ILI9341_MADCTL); writeData(0x48); and change 0x48 to 0x88 to swap the red and blue channels. Alternatively, use the setRotation() function with different arguments (0, 1, 2, 3) to see if the colors correct themselves.

About the author

admin · Shiatsu Pro Faculty

A practitioner and educator contributing to the Clinical Shiatsu Journal, indexed in PubMed since 2011. They train candidates in the 600-hour meridian-based curriculum.