Clearing a TFT screen in Arduino is usually a one-line operation: call your display library’s fillScreen() function with the color you want. For example, tft.fillScreen(TFT_BLACK) clears a TFT_eSPI display to black, while an Adafruit_GFX-based display can use tft.fillScreen(ILI9341_BLACK) or the matching color constant for your screen. The exact name depends on the library and display driver.
This guide explains how to clear an entire TFT display, erase only part of it, and avoid common problems such as flicker and slow redraws. The examples cover popular Arduino libraries, including TFT_eSPI and Adafruit GFX.
What Does “Clear the TFT Screen” Mean?
A TFT display shows an image by controlling the color of its pixels. When you clear the screen, your Arduino writes the same background color across the display, replacing the pixels that were already there. This is useful when starting an interface, switching screens, removing old text, or preparing an area for updated graphics.
Clearing is not the same as turning off the display. A cleared screen remains powered and displays a solid color, commonly black or white. To blank the screen without drawing a background color, you would need a display-specific power or sleep command instead.
Clear a TFT Screen with fillScreen()
Most Arduino graphics libraries provide a function called fillScreen(). The function takes a color as its argument and fills the entire display with that color. Call it after initializing the display, and call it again whenever you need to replace the current screen contents.
tft.fillScreen(color);
For instance, the following line clears a compatible display to black:
tft.fillScreen(0x0000);
The value 0x0000 represents black in the common 16-bit RGB565 color format. Using a named color constant is often easier to read, but the available constants vary between libraries.
Clearing a TFT_eSPI Display
TFT_eSPI is a popular library for ESP32 and ESP8266 projects. Its color constants use the TFT_ prefix. First create the display object and initialize it in setup(), then call fillScreen() with a color such as TFT_BLACK.
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
void setup() {
tft.init();
tft.setRotation(1);
// Clear the display to black
tft.fillScreen(TFT_BLACK);
}
void loop() {
}
The setRotation() call is optional; it changes the display orientation, not the clearing behavior. If you use a different TFT_eSPI configuration, make sure the driver and pin settings in the library’s setup configuration match your hardware.
To clear the display to another color, replace TFT_BLACK with a supported constant such as TFT_WHITE, TFT_RED, TFT_GREEN, or TFT_BLUE.
Clearing an Adafruit GFX Display
Adafruit GFX provides drawing functions for many TFT drivers. You generally create an object using the driver library, initialize the display with its startup function, and then call fillScreen(). Use the color definitions provided by the driver you installed.
For example, an ILI9341 display commonly uses Adafruit_ILI9341 and color constants such as ILI9341_BLACK:
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
void setup() {
tft.begin();
// Clear the display to black
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
}
If you have a different Adafruit-compatible driver, use its corresponding header and color constants. For example, an ST7735 or ST7789 display may use constants beginning with ST77XX_. The function name is often the same, but the display object and color definitions depend on the driver library.
Choose the Correct Color
Many small TFT displays use RGB565 color, which stores red, green, and blue information in a 16-bit value. Library constants are convenient because they provide correctly formatted values for common colors. If your library does not define a color you need, check its documentation for the expected color format before passing a number to fillScreen().
Some libraries include a helper for converting separate red, green, and blue values into RGB565. In Adafruit GFX-based projects, for example, a driver may provide a color565() function. The exact method can vary, so consult the documentation for your specific display library.
A color that appears incorrect may indicate that your code is using a value in the wrong format or that the library’s color byte order needs configuration. Check the library examples and driver settings before assuming the screen itself is faulty.
Clear Only Part of the Screen
You do not always need to erase the entire display. To remove old text or update one section of the interface, use fillRect(). It fills a rectangular area with a chosen color and takes the starting position, width, height, and color as arguments.
// Clear a 120-by-30-pixel area starting at x=10, y=20
tft.fillRect(10, 20, 120, 30, TFT_BLACK);
For an Adafruit ILI9341 display, the same idea applies, but use that library’s color constant:
tft.fillRect(10, 20, 120, 30, ILI9341_BLACK);
The coordinates begin at the top-left corner of the display: (0, 0). Before drawing a clearing rectangle, check that its position and dimensions fit within the screen. A rectangle that extends beyond the visible area may be clipped, depending on the library.
Partial clearing is especially useful for dashboards. If a temperature value changes, erase and redraw the value’s area rather than refreshing the whole interface. This can reduce drawing time and help prevent visible flicker.
Clear the Screen Before Drawing Text
When you draw new text over old text, the characters may have different widths. The new text can therefore leave parts of the previous value visible. One simple solution is to clear a rectangle behind the text before drawing the updated value.
// Erase the value area
tft.fillRect(20, 50, 150, 30, TFT_BLACK);
// Draw the updated value
tft.setCursor(20, 50);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.print("Value: 42");
Adjust the rectangle to cover the full text area, including any extra space needed for longer values. If the background is not black, clear the area using the same color as the surrounding background so it blends in.
Some graphics libraries allow you to set both foreground and background text colors. This can make text drawing replace the character background, but it does not necessarily erase the whole previous text area. For reliable updates—particularly when a new string is shorter—clear a suitably sized rectangle first.
Prevent Flicker in the Arduino Loop
A common mistake is clearing the full screen every time loop() runs. The Arduino may execute the loop many times per second, repeatedly painting the background and then redrawing the interface. This can make the display flash, flicker, or appear slower than expected.
Instead, clear the screen when you first initialize the display or when the interface changes. For values that update regularly, erase and redraw only the relevant area. You can also update the display only when the data has changed or when a chosen time interval has passed.
unsigned long previousUpdate = 0;
const unsigned long updateInterval = 1000;
void loop() {
if (millis() - previousUpdate >= updateInterval) {
previousUpdate = millis();
// Clear and redraw only the changing value area
tft.fillRect(20, 50, 150, 30, TFT_BLACK);
tft.setCursor(20, 50);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.print("Updated");
}
}
This example uses a TFT_eSPI-style black and white color naming convention. If you are using another library, replace those constants with the colors defined by your display driver.
Common Problems When Clearing a TFT Display
-
The code does not compile: Check that the correct display library is installed and that your color constant belongs to that library. A constant such as
TFT_BLACKmay not exist in an Adafruit driver. -
The screen stays white or blank: Confirm that the display initialization function runs before
fillScreen(). Also verify the driver selection, wiring, chip-select pin, and other hardware settings. - The wrong area is cleared: Check the rectangle’s x and y coordinates, width, height, and display rotation. Rotation changes the coordinate orientation and may affect the dimensions you expect.
- Colors look wrong: Use the color constants intended for your driver and check whether the library requires a color-order setting or a specific RGB565 format.
-
The display flickers: Avoid clearing and repainting the entire screen on every pass through
loop(). Use partial updates or redraw only when necessary. - The old text is still visible: Make the clearing rectangle large enough to cover the entire previous string, and fill it with the correct background color before drawing the replacement.
If the display does not respond, test a simple example included with your library before changing your own application code. A working library example helps separate a drawing-code problem from a wiring, power, or configuration problem.
Conclusion
To clear a TFT screen in Arduino, initialize the display and call fillScreen() with a color supported by your graphics library. Use fillRect() when you only need to erase part of the display, such as an area behind changing text. The function is straightforward, but the correct color names and setup depend on whether you use TFT_eSPI, Adafruit GFX, or another driver.
For a smooth interface, avoid clearing the entire TFT repeatedly inside loop(). Clear once at startup, then update only the areas that change. This simple approach makes Arduino TFT projects easier to read, more responsive, and less prone to flicker.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.