Every TFT_eSPI journey starts with the same milestone: text on a screen. This page walks through the canonical Hello World sketch line by line, explains what each call does, and shows you the common variations you’ll reach for next — different colors, different fonts, and positioning text withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Marcussacapuces91/doc-TFT_eSPI/llms.txt
Use this file to discover all available pages before exploring further.
drawString instead of println.
Before uploading any sketch, you must configure
User_Setup.h (or select a pre-made file via User_Setup_Select.h) to match your specific display driver, resolution, and SPI pin assignments. The library will not function correctly until that step is complete.The Complete Sketch
Here is the full Hello World sketch exactly as it runs on an ESP32 or ESP8266 with a 160×128 or 240×320 TFT panel:Step-by-Step Walkthrough
Include the library and create a display object
TFT_eSPI.h pulls in the full driver stack. TFT_eSPI tft = TFT_eSPI() creates the global display instance named tft. All subsequent calls go through this object. The constructor reads pin and driver settings from User_Setup.h at compile time — there are no runtime arguments needed.Initialize the display
tft.init() sends the startup command sequence to the display controller over SPI. It powers on the panel, sets up internal registers, and leaves the display ready to receive drawing commands. Call this once in setup() before any other drawing function.Set the screen rotation
0 through 3 — each rotating the coordinate system 90° clockwise relative to the previous. Rotation 1 puts the display in landscape orientation (wide side horizontal). Changing this value does not clear the screen; call fillScreen() afterward if needed.| Value | Orientation |
|---|---|
0 | Portrait (default) |
1 | Landscape |
2 | Portrait flipped |
3 | Landscape flipped |
Clear the screen
fillScreen() paints every pixel in the given color. Using TFT_BLACK gives a clean black canvas. TFT_eSPI defines a full set of named color constants — TFT_WHITE, TFT_RED, TFT_GREEN, TFT_BLUE, TFT_YELLOW, TFT_CYAN, TFT_MAGENTA, TFT_ORANGE, and more — all encoded as 16-bit RGB565 values.Position the text cursor
setCursor(x, y) sets the pixel position where the next print() or println() call begins. The origin (0, 0) is the top-left corner of the screen in the current rotation. The cursor advances automatically after each character is drawn.Set text color
TFT_WHITE with a transparent background — pixels behind each character are not overwritten. Pass a second argument to set a solid background fill: tft.setTextColor(TFT_WHITE, TFT_BLACK) draws white glyphs on a black fill, which is essential when overwriting old text (see Text Padding).Set text size
setTextSize(n) scales the active font by integer factor n. Size 1 is the native pixel size of the font; size 2 doubles it; size 3 triples it, and so on. For clean, sharp text, keep to size 1 or 2 — higher integer scaling introduces visible pixel staircase artifacts on small fonts.Select a font
0 through 8. Font 2 is a clean 16px proportional font well-suited for general labels and messages. Font 0 is the tiny 6×8 bitmap fallback. Smooth anti-aliased (VLCD/GFXFF) fonts are also available but require additional setup.| Font | Description |
|---|---|
0 | 6×8 Glcd (Adafruit GFX compatible) |
1 | 8px bitmap |
2 | 16px smooth |
4 | 26px smooth |
6 | 48px digit-only |
7 | 48px segment-style digits |
8 | 75px digit-only |
Common Variants
Using drawString for Precise Positioning
println moves the cursor as a side effect, which makes it awkward when you want to place text at exact pixel coordinates. drawString is the preferred alternative — it accepts explicit x and y coordinates and is unaffected by the cursor state:
Different Text Colors
TFT_eSPI’s predefined color constants cover the most common UI colors. You can also construct any 16-bit RGB565 color using thetft.color565(r, g, b) helper:
Different Fonts
Swap the font number to change the appearance of your text. Remember to enable the required fonts inUser_Setup.h — loading unused fonts wastes flash memory:
Fonts 6, 7, and 8 contain only digit characters (
0–9), a colon, and a period. They are designed for large numeric readouts such as clocks or sensor displays and will render blank rectangles for letters.Adapting to Different Display Sizes
Hard-coding pixel coordinates makes sketches fragile when you move to a different panel. Usetft.width() and tft.height() to write code that adapts automatically:
tft.width() and tft.height() return the logical pixel dimensions of the screen in its current rotation, so width > height in landscape and height > width in portrait. Combine them with MC_DATUM (see Text Datum) to centre text perfectly on any display.