Colour TFT display
Draw colour text and shapes on a 240×320 ILI9341 TFT over SPI.
Displays · Intermediate
In a real product: Smart thermostat display (Smart home): A colour screen shows the controls.
A TFT gives you a full-colour, 240×320 screen. It has far too many pixels for the Uno's RAM, so the library draws straight into the display's own memory over the fast SPI bus.
What you'll learn
- The SPI bus: MOSI, SCK and a chip-select (CS) line
- Data/command (D/C): how the controller tells commands from pixel data
- Colour graphics with the Adafruit ILI9341 library
How it works
The Uno's hardware SPI is on pins 11 (MOSI), 12 (MISO) and 13 (SCK). CS (pin 10) selects the display, D/C (pin 9) says whether a byte is a command or data, and RST (pin 8) resets it. LED powers the backlight.
To fill a rectangle, the library sends the controller a window (columns and rows) and then that many pixels, each a 16-bit RGB565 colour. setup() draws the title and the shapes once; the loop only redraws the uptime line, which is much faster than redrawing the whole screen.
Try this
- Draw a bar chart of a sensor reading that updates every second.
- Use
tft.setRotation(1)for landscape. - Draw a smiley face from circles and arcs.
sketch.ino
// An ILI9341 240x320 TFT on hardware SPI: CS 10, D/C 9, RST 8 (MOSI 11, MISO 12, SCK 13).
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
Adafruit_ILI9341 tft(10, 9, 8);
void setup() {
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(20, 20);
tft.println("Try");
tft.setCursor(20, 50);
tft.println("Embedded");
tft.fillRect(20, 100, 90, 60, ILI9341_RED);
tft.fillCircle(170, 130, 32, ILI9341_GREEN);
tft.drawTriangle(40, 270, 120, 180, 200, 270, ILI9341_YELLOW);
}
void loop() {
tft.fillRect(20, 290, 200, 20, ILI9341_BLACK); // clear the old uptime
tft.setTextSize(2);
tft.setCursor(20, 290);
tft.print("Uptime: ");
tft.print(millis() / 1000);
tft.print(" s");
delay(1000);
}