Arduino Nano - ILI9341 ILI9488 ST7789 TFT LCD Touch Display SPI-Schnittstelle

Diese Anleitung zeigt dir Schritt für Schritt, wie du ein SPI-TFT-Display am Arduino Nano verwendest. Der Nano enthält denselben ATmega328P wie der Uno in einem kompakten, Breadboard-freundlichen Paket - er hat die gleichen Hardware-SPI-Pins, so dass die TFT-Verdrahtung identisch mit der des Uno ist. Die Haupteinschränkung ist der 2KB-SRAM: Vermeide große In-Memory-Bitmaps und verwende PROGMEM für Bilddaten.

Das wirst du durcharbeiten:

Diese Anleitung behandelt sowohl Touch- als auch Non-Touch-SPI-TFT-LCD-Displays. Sie funktioniert mit 1,3-, 1,54-, 2,2-, 2,4-, 2,8-, 3,2- und 3,5-Zoll-Panels, die von ILI9341-, ILI9488- oder ST7789-Controller-Chips angesteuert werden.

Arduino Nano TFT SPI Display

Erforderliche Hardware

1×Official Arduino Nano
1×Alternativ: DIYables Nano V3.0 Development Board
1×USB-A-auf-Mini-B-USB-Kabel
1×SPI-TFT-Display-Modul
1×5V-auf-3,3V-Level-Converter
1×Verbindungskabel
1×(Empfohlen) Schraubklemmen-Erweiterungsboard für Arduino Nano
1×(Empfohlen) Breakout-Erweiterungsboard für Arduino Nano
1×(Empfohlen) Stromverteiler für Arduino Nano

Oder Sie können die folgenden Kits kaufen:

1×DIYables Sensor-Kit (18 Sensoren/Displays)
Offenlegung: Einige der in diesem Abschnitt bereitgestellten Links sind Amazon-Affiliate-Links. Wir können eine Provision für Käufe erhalten, die über diese Links getätigt werden, ohne zusätzliche Kosten für Sie. Wir schätzen Ihre Unterstützung.

Was ist das SPI-TFT-Display?

Ein SPI-TFT-Display-Modul ist ein Farb-LCD-Panel, das von einem Controller-Chip über einen 4-adrigen SPI-Bus angesteuert wird. Die DIYables_TFT_SPI-Bibliothek unterstützt drei beliebte Driver-Chips:

  • ILI9341 - 16-Bit-RGB565-Farbe, bis zu 40 MHz SPI.
  • ILI9488 - 18-Bit-RGB666-Farbe über SPI, bis zu 24 MHz.
  • ST7789 - 16-Bit-RGB565-Farbe, bis zu 40 MHz SPI.

Empfehlung: Wenn du noch kein Display gekauft hast, empfehlen wir den ST7789-Driver. Er ist weit verbreitet, läuft mit voller 40-MHz-SPI-Geschwindigkeit und ist die einfachste Wahl für neue Projekte.

Das gesamte Zeichnen erfolgt über die Adafruit GFX API, die Formen, Text, Schriftarten und Bitmap-Ausgabe bietet.

Pinbelegung

Die meisten SPI-TFT-LCD-Displays haben folgende Pins:

Display-Pins:

Pin Funktion
VCC Stromversorgung
GND Masse
CS Chip-Auswahl — auf niedrig ziehen, um das Display auf dem SPI-Bus auszuwählen
DC / RS Daten-/Befehl-Auswahl — hoch für Pixeldaten, niedrig für Befehle
RST Hardware-Reset — optional; an 3,3V anschließen, wenn nicht verwendet
MOSI / SDI / SDA SPI-Dateneingang (MCU → Display)
SCK / CLK SPI-Takt
MISO / SDO SPI-Datenausgang (Display → MCU) — optional für Display-only-Betrieb
LED / BL / BLK Hintergrundbeleuchtung Stromversorgung — an 3,3V oder einen PWM-Pin anschließen zum Dimmen

SD-Karten-Pins (wenn deine Anwendung auf die SD-Karte zugreifen muss):

Pin Funktion
SD_CS / TF_CS SD-Karten-Chip-Auswahl
MOSI / SDI MOSI — Daten von MCU zu SD-Karte
SCK / CLK SCK — SPI-Takt
MISO / SDO MISO — Daten von SD-Karte zu MCU

Für TFT-Displays mit Touch-Unterstützung gibt es zusätzliche Touch-Pins (wenn deine Anwendung die Touch-Funktion verwendet und das Display dies unterstützt):

Pin Funktion
T_CS Touch-Controller-Chip-Auswahl
T_CLK SCK — SPI-Takt
T_DIN MOSI — Daten von MCU zu Touch-Controller
T_DO MISO — Daten von Touch-Controller zu MCU
T_IRQ Touch-Interrupt — optional; signalisiert, wenn der Bildschirm berührt wird

Hinweis: Einige Non-Touch-Display-Module stellen auch T_CS-, T_CLK-, T_DIN-, T_DO- und T_IRQ-Pins zur Verfügung. Diese funktionieren nicht auf diesen Boards — der Touch-Controller-IC ist nicht bestückt. Sie erscheinen, weil die Leiterplatte das gleiche Layout wie die Touch-aktivierte Version wiederverwendet, um Fertigungsvarianten zu reduzieren.

TFT-SPI-Display-Pinbelegung

Schaltschema

⚠ Wichtig — Spannungsmismatch: Der Arduino Nano ist ein 5V-Board. Seine GPIO-Pins geben 5V-Logik aus, aber SPI-TFT-Displays arbeiten bei 3,3V. Das direkte Verbinden der Signalleitungen (MOSI, SCK, CS, DC, RST und T_CS bei Verwendung von Touch) vom Nano zum Display beschädigt das Display. Du musst einen 5V-auf-3,3V-Level-Converter auf jeder MCU-gesteuerten Signalleitung zwischen dem Nano und dem Display verwenden. Die MISO-Leitung (Display → MCU) benötigt keinen Level-Converter. Ein einzelnes 4-Kanal-Modul reicht für die Non-Touch-Verdrahtung aus; die Touch-Verdrahtung benötigt 6 Kanäle insgesamt (add T_CS), verwende also zwei 4-Kanal-Module oder ein 8-Kanal-Modul. Verbinde die TFT VCC mit dem 3,3V-Pin am Nano.

Ohne Touch

Verbinde MOSI mit D11, SCK mit D13, MISO mit D12 am Nano. CS, DC und RST können beliebige verfügbare GPIO-Pins sein - D10, D9, D8 werden in den Beispielen verwendet.

Display:

TFT-Pin Arduino-Nano-Pin Beschreibung
VCC 3.3V Stromversorgung (nur 3,3V — das Display läuft mit 3,3V)
GND GND Masse
CS D10 Chip-Auswahl
DC / RS D9 Daten-/Befehl-Auswahl
RST D8 Reset (optional)
MOSI / SDI D11 Hardware-SPI MOSI
SCK D13 Hardware-SPI Takt
MISO / SDO D12 Hardware-SPI MISO (optional)
LED / BL 3.3V Hintergrundbeleuchtung Stromversorgung

SD-Karte (wenn deine Anwendung auf die SD-Karte zugreifen muss):

SD-Pin Arduino-Nano-Pin Beschreibung
SD_CS / TF_CS beliebiger freier GPIO SD-Karten-Chip-Auswahl
MOSI / SDI D11 Mit Display MOSI gemeinsam (D11)
SCK / CLK D13 Mit Display SCK gemeinsam (D13)
MISO / SDO D12 Mit Display MISO gemeinsam (D12)
Arduino Nano TFT SPI Display Schaltschema ohne Touch

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Mit Touch

Verbinde den XPT2046-Touch-Controller mit dem Arduino-Nano-SPI-Bus und teile D11, D13 und D12 mit dem Display.

Display:

TFT-Pin Arduino-Nano-Pin Beschreibung
VCC 3.3V Stromversorgung (nur 3,3V — das Display läuft mit 3,3V)
GND GND Masse
CS D10 Chip-Auswahl
DC / RS D9 Daten-/Befehl-Auswahl
RST D8 Reset (optional)
MOSI / SDI D11 Hardware-SPI MOSI
SCK D13 Hardware-SPI Takt
MISO / SDO D12 Hardware-SPI MISO (optional)
LED / BL 3.3V Hintergrundbeleuchtung Stromversorgung

Touch-Controller (wenn deine Anwendung die Touch-Funktion verwendet und das Display dies unterstützt):

Touch-Pin Arduino-Nano-Pin Beschreibung
T_CS beliebiger freier GPIO Touch-Chip-Auswahl
T_IRQ beliebiger freier GPIO Touch-Interrupt (optional)
T_DIN D11 Mit Display MOSI gemeinsam (D11)
T_CLK D13 Mit Display SCK gemeinsam (D13)
T_DO D12 Mit Display MISO gemeinsam (D12)
Arduino Nano TFT SPI Display Schaltschema mit Touch

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Siehe Der beste Weg, den Arduino Nano und andere Komponenten mit Strom zu versorgen.

Wenn dein MCU zwei oder mehr Hardware-SPI-Schnittstellen hat, kannst du jedes Peripheriegerät (Display, SD-Karte, Touch-Controller) einem eigenen dedizierten SPI-Bus zuweisen. Wenn dein MCU nur eine Hardware-SPI-Schnittstelle hat, teilen sich alle drei Peripheriegeräte die gleichen drei Datenleitungen (MOSI, SCK, MISO) - am Nano sind dies D11, D13 und D12. Jedes Peripheriegerät hat seinen eigenen CS-Pin, so dass nur einer gleichzeitig aktiv ist. Die DIYables_TFT_SPI-Bibliothek verwaltet sowohl das Display als auch den XPT2046-Touch-Controller über eine einzige API — keine separate SPI-Bibliothek ist für die Touch-Seite erforderlich.

Einrichtung der Bibliothek

  1. Stecke den Arduino Nano mit einem Mini-B-USB-Kabel in deinen Computer.
  2. Öffne Arduino IDE. Stelle das Board auf Arduino Nano ein und wähle den entsprechenden Port aus.
  3. Gehe zum Libraries-Panel auf der linken Seite.
  4. Gib "DIYables_TFT_SPI" in das Suchfeld ein. Suche nach der Bibliothek von DIYables.
  5. Klicke auf Install. Bei Aufforderung installiere alle Abhängigkeiten.
  • Search for DIYables TFT SPI created by DIYables.io and click the Install button.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano
Library Manager
Type:
All
Topic:
All
DIYables TFT SPI by DIYables.io
Works with both touch and non-touch versions of the same SPI TFT modules. Supports ILI9341 (240x320, 16-bit RGB565), ILI9488 (320x480, 18-bit RGB666), and ST7789 (240x320, 16-bit RGB565) displays over SPI. Includes built-in driver for XPT2046 / HR2046 / ADS7843 SPI touch controllers and 4-wire resistive touch panels - no separate touch library required. Use the display-only API for non-touch panels, or add initTouchSPI() to enable touch on modules that include a touch controller. Extends Adafruit GFX for full graphics support. Works with any Arduino-compatible board that has SPI. More info
1.0.3
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
Arduino Nano on COM15
1

Hinweis: Wenn dein Nano einen älteren Bootloader verwendet, wähle ATmega328P (Old Bootloader) unter Processor im Menü Tools aus.

Basiskode

Jede Skizze, die mit der DIYables_TFT_SPI-Bibliothek erstellt wird, beginnt mit dieser Grundlage:

#include <DIYables_TFT_SPI.h> #define TFT_CS_PIN 10 #define TFT_DC_PIN 9 #define TFT_RST_PIN 8 // Entferne den Kommentar zu der Zeile, die deinem Driver-Chip entspricht: // DIYables_ILI9341_SPI TFT_display(240, 320, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(320, 480, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(240, 320, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); void setup() { TFT_display.begin(); TFT_display.setRotation(1); } void loop() { // drawing code here }

Gib die physischen Pixelabmessungen des Panels an den Konstruktor weiter. Eine falsche Größe führt dazu, dass das Bild abgeschnitten oder verschoben wird.

Arduino Nano Code - Zeichne Formen

Das Beispiel DrawShapes zeichnet Kreise, Dreiecke, Rechtecke, abgerundete Rechtecke und Linien mit den Adafruit GFX Primitiven.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // --------------------------------------------- // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // SDO / MISO -> D12 (only needed when reading from display) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // MOSI and SCK use default hardware SPI pins // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define BLACK DIYables_TFT_SPI::colorRGB(0, 0, 0) #define BLUE DIYables_TFT_SPI::colorRGB(0, 0, 255) #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) #define GREEN DIYables_TFT_SPI::colorRGB(0, 255, 0) #define ORANGE DIYables_TFT_SPI::colorRGB(255, 165, 0) #define PINK DIYables_TFT_SPI::colorRGB(255, 192, 203) #define VIOLET DIYables_TFT_SPI::colorRGB(148, 0, 211) #define TURQUOISE DIYables_TFT_SPI::colorRGB(64, 224, 208) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) // Helper to draw a filled diamond void fillDiamond(int cx, int cy, int h, int v, uint16_t color) { int x0 = cx, y0 = cy - v; int x1 = cx + h, y1 = cy; int x2 = cx, y2 = cy + v; int x3 = cx - h, y3 = cy; TFT_display.fillTriangle(x0, y0, x1, y1, x2, y2, color); TFT_display.fillTriangle(x0, y0, x2, y2, x3, y3, color); } void setup() { TFT_display.begin(); TFT_display.setRotation(1); // Landscape } void loop() { TFT_display.fillScreen(WHITE); uint16_t w = TFT_display.width(); uint16_t h = TFT_display.height(); // Scale positions relative to screen size with better spacing int col1 = w / 8; int col2 = w * 3 / 8; int col3 = w * 5 / 8; int col4 = w * 7 / 8; int row1 = h / 4; int row2 = h / 2; int row3 = h * 3 / 4; // Outlined circle TFT_display.drawCircle(col1, row1, 30, RED); // Filled circle TFT_display.fillCircle(col2, row1, 30, RED); // Outlined triangle TFT_display.drawTriangle(col3 - 30, row1 + 25, col3 + 30, row1 + 25, col3, row1 - 25, BLUE); // Filled triangle TFT_display.fillTriangle(col4 - 30, row1 + 25, col4 + 30, row1 + 25, col4, row1 - 25, GREEN); // Outlined rectangle TFT_display.drawRect(col1 - 35, row2 - 20, 70, 40, ORANGE); // Filled rectangle TFT_display.fillRect(col2 - 35, row2 - 20, 70, 40, TURQUOISE); // Outlined round rectangle TFT_display.drawRoundRect(col3 - 35, row2 - 20, 70, 40, 10, VIOLET); // Filled round rectangle TFT_display.fillRoundRect(col4 - 35, row2 - 20, 70, 40, 10, PINK); // Outlined diamond (centered between col1 and col2) int diamond1_x = (col1 + col2) / 2; TFT_display.drawLine(diamond1_x, row3 - 30, diamond1_x + 25, row3, GREEN); TFT_display.drawLine(diamond1_x + 25, row3, diamond1_x, row3 + 30, GREEN); TFT_display.drawLine(diamond1_x, row3 + 30, diamond1_x - 25, row3, GREEN); TFT_display.drawLine(diamond1_x - 25, row3, diamond1_x, row3 - 30, GREEN); // Filled diamond (centered between col3 and col4) int diamond2_x = (col3 + col4) / 2; fillDiamond(diamond2_x, row3, 25, 30, BLUE); delay(10000); }

Folge mit

  • Verdrahte das TFT-Modul mit dem Nano mit Hilfe der obigen Tabelle.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Das Display zeigt ein sich wiederholendes Raster aus farbigen Formen.

Methodenübersicht

Methode Erklärung Beispielaufruf
begin() Initialisiere den Display-Controller. TFT_display.begin();
setRotation(r) Drehe 0-3. 1=Querformat. TFT_display.setRotation(1);
fillScreen(color) Male den ganzen Bildschirm. TFT_display.fillScreen(WHITE);
colorRGB(r,g,b) Mache einen 16-Bit-Farbwert. colorRGB(0,255,0)
fillCircle(x,y,r,color) Gefüllter Kreis. TFT_display.fillCircle(60,60,30,RED);
drawRect(x,y,w,h,color) Rechteck-Kontur. TFT_display.drawRect(10,10,80,50,BLUE);
fillTriangle(x0,y0,x1,y1,x2,y2,color) Gefülltes Dreieck. TFT_display.fillTriangle(60,10,10,110,110,110,GREEN);

Arduino Nano Code - Text und Zahlen anzeigen

Das Beispiel ShowTextAndNumber druckt Zeichenketten und numerische Werte auf dem Bildschirm unter Verwendung der integrierten GFX-Text-Engine.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // --------------------------------------------- // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // SDO / MISO -> D12 (only needed when reading from display) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define MAGENTA DIYables_TFT_SPI::colorRGB(255, 0, 255) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) void setup() { Serial.begin(9600); Serial.println(F("TFT SPI Display - Show text and numbers")); TFT_display.begin(); TFT_display.setRotation(1); // Landscape TFT_display.fillScreen(WHITE); // Set text color and size TFT_display.setTextColor(MAGENTA); TFT_display.setTextSize(3); // Sample values float temperature = 23.5; float humidity = 78.6; // Display temperature TFT_display.setCursor(20, 20); TFT_display.print("Temperature: "); TFT_display.print(temperature, 1); TFT_display.print(char(247)); TFT_display.println("C"); // Display humidity TFT_display.setCursor(20, 60); TFT_display.print("Humidity: "); TFT_display.print(humidity, 1); TFT_display.print("%"); } void loop() { }

Folge mit

  • Verdrahte und lade wie oben hoch.
  • Das Display druckt mehrere Zeilen von Text und Zahlen aus.

Methodenübersicht

Methode Erklärung Beispielaufruf
setTextColor(color) Setzt die Vordergrundfarbe für den gesamten folgenden Text. TFT_display.setTextColor(WHITE);
setTextSize(size) Skaliert Text um einen Pixelfaktor. Größe 1 = 6×8 px. Halte die Größe klein, um sie auf dem winzigen Bildschirm des Nano zu halten. TFT_display.setTextSize(1);
setCursor(x, y) Positioniert den Textcursor bei (x, y). TFT_display.setCursor(0, 10);
print(value) Druckt eine Zeichenkette oder Zahl am Cursor. Halte Zeichenketten kurz, um SRAM zu sparen. TFT_display.print("Nano!");
println(value) Druckt und umbrochen zur nächsten Zeile. TFT_display.println(42);

Arduino Nano Code - Bild zeichnen

Das Beispiel DrawImage zeigt eine Vollfarb-RGB565-Bitmap auf dem Bildschirm. Der Nano hat nur 2 KB SRAM, daher müssen die Pixeldaten unter Verwendung des PROGMEM-Schlüsselworts im Programmflash bleiben. Die bitmap.h-Begleitdatei enthält ein const uint16_t Array, das PROGMEM deklariert ist. Die Bibliothek liest direkt aus dem Flash zum Zeichnungszeitpunkt, wobei der SRAM unangetastet bleibt.

Platziere bitmap.h im gleichen Skizzenordner vor dem Kompilieren.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> #include "bitmap.h" // ============================================= // Wiring (Arduino Uno / Nano) // --------------------------------------------- // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // SDO / MISO -> D12 (only needed when reading from display) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) int img_width = 120; int img_height = 53; void setup() { Serial.begin(9600); Serial.println(F("TFT SPI Display - Draw Image")); TFT_display.begin(); uint16_t SCREEN_WIDTH = TFT_display.width(); uint16_t SCREEN_HEIGHT = TFT_display.height(); int x = (SCREEN_WIDTH - img_width) / 2; int y = (SCREEN_HEIGHT - img_height) / 2; TFT_display.fillScreen(WHITE); TFT_display.drawRGBBitmap(x, y, myBitmap, img_width, img_height); } void loop() { delay(2000); TFT_display.invertDisplay(true); delay(2000); TFT_display.invertDisplay(false); }

Folge mit

  • Kopiere bitmap.h in den gleichen Ordner wie die Skizze.
  • Verdrahte das TFT-Display mit dem Nano mit Hilfe der obigen Pin-Tabelle (D11, D13, D12 für SPI).
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Das Display rendert die PROGMEM-Bitmap-Bild.

Methodenübersicht

Methode Erklärung Beispielaufruf
drawRGBBitmap(x,y,bitmap,w,h) Zeichnet eine RGB565-Bitmap aus einem PROGMEM-Array. Position (x, y) ist die obere linke Ecke. TFT_display.drawRGBBitmap(0, 0, myImage, 240, 320);
fillScreen(color) Löscht den Bildschirm auf eine durchgehende Farbe vor dem Zeichnen. TFT_display.fillScreen(BLACK);

Arduino Nano Code - Bild von SD-Karte zeichnen

Das Beispiel DrawImageSDcard liest eine rohe RGB565-Bilddatei von einer SD-Karte und streamt sie in kleinen Stücken zum Display. Da Daten gestreamt und nicht in einen vollständigen Puffer geladen werden, kann selbst der Nano große Bilder ohne Speichererschöpfung anzeigen.

Verdrahte das SD-Modul mit den gleichen SPI-Pins wie das Display (D11/D13/D12) und verbinde seine CS-Leitung mit einem separaten Pin, der als SD_CS_PIN definiert ist.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> #include <SD.h> // ============================================= // Wiring (Arduino Uno / Nano) // --------------------------------------------- // TFT + SD module Arduino Uno / Nano // ----------------------- --------------------------------- // VCC -> 5V // GND -> GND // TFT CS -> D10 (TFT_CS_PIN) // TFT RESET -> D9 (TFT_RST_PIN) // TFT DC / RS -> D8 (TFT_DC_PIN) // SD CS -> D4 (SD_CS) // SDI / MOSI (shared) -> D11 (hardware SPI MOSI) // SDO / MISO (shared) -> D12 (hardware SPI MISO) // SCK (shared) -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 #define SD_CS 4 // SD card chip select (must differ from TFT_CS_PIN) // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) #define BUFFPIXEL 20 File bmpFile; uint16_t SCREEN_WIDTH; uint16_t SCREEN_HEIGHT; // Helper functions to read BMP file header uint16_t read16(File &f) { uint16_t result; result = f.read(); result |= (f.read() << 8); return result; } uint32_t read32(File &f) { uint32_t result; result = f.read(); result |= ((uint32_t)f.read() << 8); result |= ((uint32_t)f.read() << 16); result |= ((uint32_t)f.read() << 24); return result; } int32_t readS32(File &f) { int32_t result; result = f.read(); result |= ((uint32_t)f.read() << 8); result |= ((uint32_t)f.read() << 16); result |= ((uint32_t)f.read() << 24); return result; } bool getBMPDimensions(const char *filename, uint32_t &w, uint32_t &h) { File f = SD.open(filename); if (!f) return false; if (read16(f) != 0x4D42) { f.close(); return false; } read32(f); // file size read32(f); // reserved read32(f); // image offset read32(f); // DIB header size w = read32(f); int32_t sh = readS32(f); h = (sh < 0) ? -sh : sh; f.close(); return true; } void drawBMP(const char *filename, int x, int y) { bmpFile = SD.open(filename); if (!bmpFile) { Serial.println("File not found"); return; } if (read16(bmpFile) != 0x4D42) { Serial.println("Not a BMP file"); bmpFile.close(); return; } uint32_t fileSize = read32(bmpFile); read32(bmpFile); // Reserved uint32_t imageOffset = read32(bmpFile); uint32_t dibHeaderSize = read32(bmpFile); uint32_t bmpWidth = read32(bmpFile); int32_t bmpHeight = readS32(bmpFile); bool topDown = false; if (bmpHeight < 0) { bmpHeight = -bmpHeight; topDown = true; } if (read16(bmpFile) != 1) { Serial.println("Invalid BMP file"); bmpFile.close(); return; } uint16_t depth = read16(bmpFile); if (depth != 24) { Serial.println("Only 24-bit BMP is supported"); bmpFile.close(); return; } if (read32(bmpFile) != 0) { Serial.println("Unsupported BMP compression"); bmpFile.close(); return; } bmpFile.seek(imageOffset); uint8_t sdbuffer[3 * BUFFPIXEL]; uint16_t color; uint32_t rowSize = (bmpWidth * 3 + 3) & ~3; if (x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT) return; uint32_t maxRow = min((uint32_t)bmpHeight, (uint32_t)(SCREEN_HEIGHT - y)); uint32_t maxCol = min(bmpWidth, (uint32_t)(SCREEN_WIDTH - x)); for (uint32_t row = 0; row < maxRow; row++) { int32_t rowPos = topDown ? row : bmpHeight - 1 - row; uint32_t filePosition = imageOffset + rowPos * rowSize; bmpFile.seek(filePosition); for (uint32_t col = 0; col < maxCol; col += BUFFPIXEL) { uint32_t pixelsToRead = min((uint32_t)BUFFPIXEL, maxCol - col); bmpFile.read(sdbuffer, 3 * pixelsToRead); for (uint32_t i = 0; i < pixelsToRead; i++) { uint8_t b = sdbuffer[i * 3]; uint8_t g = sdbuffer[i * 3 + 1]; uint8_t r = sdbuffer[i * 3 + 2]; color = DIYables_TFT_SPI::colorRGB(r, g, b); if ((x + col + i) < SCREEN_WIDTH && (y + row) < SCREEN_HEIGHT) { TFT_display.drawPixel(x + col + i, y + row, color); } } } } bmpFile.close(); Serial.println("BMP drawn"); } void setup() { Serial.begin(9600); if (!SD.begin(SD_CS)) { Serial.println("SD card initialization failed!"); return; } Serial.println("SD card initialized."); TFT_display.begin(); TFT_display.setRotation(1); // Landscape SCREEN_WIDTH = TFT_display.width(); SCREEN_HEIGHT = TFT_display.height(); TFT_display.fillScreen(WHITE); uint32_t imgWidth, imgHeight; if (getBMPDimensions("diyables.bmp", imgWidth, imgHeight)) { int x = (SCREEN_WIDTH - imgWidth) / 2; int y = (SCREEN_HEIGHT - imgHeight) / 2; drawBMP("diyables.bmp", x, y); } else { Serial.println("Failed to get BMP dimensions"); } } void loop() { }

Folge mit

  • Verdrahte das SD-Kartenmodul mit dem Nano. Teile den SPI-Bus: MOSI→D11, SCK→D13, MISO→D12. Verbinde SD CS mit dem Pin, der als SD_CS_PIN definiert ist.
  • Kopiere ein rohes RGB565-Binärbild auf die Wurzel der SD-Karte. Bildbreite und -höhe müssen mit deinem Panel übereinstimmen.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle Board und Port aus, füge den Code ein und klicke auf Upload.
  • Das Bild wird von der Karte gelesen und auf dem Bildschirm angezeigt.

Methodenübersicht

Methode Erklärung Beispielaufruf
startWrite() Öffnet eine direkte SPI-Transaktion mit beharrlichem Display-CS. TFT_display.startWrite();
setAddrWindow(x0,y0,x1,y1) Definiert den rechteckigen Bereich zum Empfang von Pixeldaten. TFT_display.setAddrWindow(0, 0, 239, 319);
pushColors(buf, len) Sendet einen Puffer mit RGB565-Pixelwerten zum Display. Halte den Puffer klein, um innerhalb des SRAM-Budgets des Nano zu bleiben. TFT_display.pushColors(buf, 256);
endWrite() Schließt die SPI-Transaktion und gibt CS frei. TFT_display.endWrite();

Arduino Nano Code - Verwende externen Zeichensatz

Das Beispiel UseExternalFont tauscht den integrierten 5×7-Bitmap-Zeichensatz gegen einen schärferen Adafruit GFX-kompatiblen benutzerdefinierten Zeichensatz aus. Wähle den Zeichensatz, indem du einen Zeiger auf seine Struktur an setFont() übergibst. Rufe setFont(NULL) auf, um jederzeit zur integrierten Schriftart zurückzukehren.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> #include <Fonts/FreeSansBold12pt7b.h> // ============================================= // Wiring (Arduino Uno / Nano) // --------------------------------------------- // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // SDO / MISO -> D12 (only needed when reading from display) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define MAGENTA DIYables_TFT_SPI::colorRGB(255, 0, 255) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) void setup() { Serial.begin(9600); Serial.println(F("TFT SPI Display - Use external font")); TFT_display.begin(); TFT_display.setFont(&FreeSansBold12pt7b); TFT_display.setRotation(1); // Landscape TFT_display.fillScreen(WHITE); TFT_display.setTextColor(MAGENTA); TFT_display.setTextSize(1); float temperature = 23.5; float humidity = 78.6; TFT_display.setCursor(20, 30); TFT_display.print("Temperature: "); TFT_display.print(temperature, 1); TFT_display.print(char(247)); TFT_display.println("C"); TFT_display.setCursor(20, 70); TFT_display.print("Humidity: "); TFT_display.print(humidity, 1); TFT_display.print("%"); } void loop() { }

Folge mit

  • Verdrahte das TFT-Display mit dem Nano mit Hilfe der obigen Pin-Tabelle.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Das Display rendert Text unter Verwendung des benutzerdefinierten Zeichensatzes.

Methodenübersicht

Methode Erklärung Beispielaufruf
setFont(&FontName) Aktiviert einen benutzerdefinierten GFX-kompatiblen Zeichensatz. Das Übergeben von NULL stellt die integrierte 5×7-Schriftart wieder her. TFT_display.setFont(&FreeSans12pt7b);
setCursor(x, y) Bewegt den Textcursor zur angegebenen Pixelposition. TFT_display.setCursor(10, 40);
setTextColor(color) Setzt die Vordergrundfarbe für gedruckten Text. TFT_display.setTextColor(WHITE);
print(text) Druckt eine Zeichenkette unter Verwendung der aktiven Schriftart am aktuellen Cursor. TFT_display.print("Nano!");

Arduino Nano Code - Touch Punkte lesen

Das Beispiel TouchGetPoint liest rohe ADC-Werte von einem XPT2046-Touch-Controller und druckt sie auf dem Serial Monitor. Gehe zuerst durch dieses Beispiel durch, um den rohen Koordinatenbereich zu sehen, den dein Panel produziert - diese Grenzen sind, was du später in die Kalibrierung einsteckst.

Verdrahtung: Teile den SPI-Bus (D11/D13/D12) zwischen dem XPT2046 und dem Display. Verbinde T_CS mit D7 und T_IRQ mit D6.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Touch Get Point Example ----------------------- This example demonstrates how to read and display touch coordinates using a DIYables SPI TFT display with a 4-wire resistive touch panel. When you touch the screen, the sketch prints the mapped (screen) X and Y coordinates to the Serial Monitor and draws a red dot at the touched location. NOTE: Run the TouchCalibration example first and paste the calibration values into setTouchCalibration() below if the touch coordinates are inaccurate. Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // ============================================= // TFT pins (always required) // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // SDO / MISO -> D12 (only needed when reading from display) // LED -> 3.3V (or any GPIO via initBacklight) // // XPT2046 / HR2046 / ADS7843 SPI touch controller // (modules with pins: T_CS, T_CLK, T_DIN, T_DO, T_IRQ) // Touch pin Arduino Uno / Nano // ------------ --------------------------------- // T_CS -> D2 (TOUCH_CS_PIN) // T_IRQ -> D2 (TOUCH_IRQ_PIN, optional - use -1 to skip) // T_CLK -> D13 (shared with display SCK) // T_DIN -> D11 (shared with display MOSI) // T_DO -> D12 (shared with display MISO) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // MOSI and SCK use default hardware SPI pins // ============================================= // Touch pin definitions (XPT2046 / HR2046 SPI touch controller) // ============================================= #define TOUCH_CS_PIN 2 // T_CS (any GPIO) #define TOUCH_IRQ_PIN -1 // T_IRQ (any GPIO, or -1 if not connected) // ============================================= // ============================================= // Calibration values. // Run the TouchCalibration example and update these if touch is inaccurate. // Typical raw ranges: // - XPT2046 / HR2046 : ~200..3900 (default below) // - 4-wire resistive : ~100..900 // ============================================= #define TOUCH_LEFT_X 300 #define TOUCH_RIGHT_X 3700 #define TOUCH_TOP_Y 300 #define TOUCH_BOT_Y 3700 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) void setup() { Serial.begin(9600); TFT_display.begin(); TFT_display.setRotation(0); TFT_display.fillScreen(WHITE); TFT_display.initTouchSPI(TOUCH_CS_PIN, TOUCH_IRQ_PIN); // If touch X is mirrored on your board, uncomment: //TFT_display.setTouchInvertX(true); // If touch Y is mirrored on your board, uncomment: //TFT_display.setTouchInvertY(false); TFT_display.setTouchCalibration(TOUCH_LEFT_X, TOUCH_RIGHT_X, TOUCH_TOP_Y, TOUCH_BOT_Y); Serial.println("Touch the screen to see coordinates."); } void loop() { int x, y; if (TFT_display.getTouch(x, y)) { Serial.print("Touch at: "); Serial.print(x); Serial.print(", "); Serial.println(y); TFT_display.fillCircle(x, y, 4, RED); delay(200); } }

Folge mit

  • Verdrahte den XPT2046 mit dem Nano. Teile D11 (MOSI), D13 (SCK), D12 (MISO) mit dem Display. T_CS→D7, T_IRQ→D6.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Öffne den Serial Monitor bei 9600 Baud. Drücke verschiedene Teile des Bildschirms, um die rohen X-, Y- und Z-Werte zu sehen.

Methodenübersicht

Methode Erklärung Beispielaufruf
initTouchSPI(cs, irq) Startet den XPT2046 auf dem gemeinsamen SPI-Bus. Gib -1 für irq ein, wenn der Interrupt-Pin nicht verdrahtet ist. TFT_display.initTouchSPI(7, 6);
readTouchRaw(x, y, z) Gibt rohe ADC-Koordinatenwerte ohne angewendete Kalibrierung zurück. Gibt wahr zurück, wenn der Bildschirm berührt wird. TFT_display.readTouchRaw(x, y, z);

Arduino Nano Code - Touch Zeichnen

Das Beispiel TouchDraw erstellt eine Finger-Mal-Anwendung auf dem Nano. Jeder Punkt, den der Finger berührt, erzeugt einen kleinen gefüllten Kreis an der entsprechenden kalibrierten Bildschirmkoordinate, und das Ziehen erstellt einen kontinuierlichen Strich.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Touch Draw Lines Example ------------------------- Draws lines on the screen following the pen. - Touch and drag on the screen to draw. - Lift the pen to stop drawing. - Touch again to start a new line from the last point. NOTE: Run the TouchCalibration example and update setTouchCalibration() below if the touch coordinates are inaccurate. Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // ============================================= // TFT pins (always required) // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // SDO / MISO -> D12 (only needed when reading from display) // LED -> 3.3V (or any GPIO via initBacklight) // // XPT2046 / HR2046 / ADS7843 SPI touch controller // (modules with pins: T_CS, T_CLK, T_DIN, T_DO, T_IRQ) // Touch pin Arduino Uno / Nano // ------------ --------------------------------- // T_CS -> D2 (TOUCH_CS_PIN) // T_IRQ -> D2 (TOUCH_IRQ_PIN, optional - use -1 to skip) // T_CLK -> D13 (shared with display SCK) // T_DIN -> D11 (shared with display MOSI) // T_DO -> D12 (shared with display MISO) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Touch pin definitions (XPT2046 / HR2046 SPI touch controller) // ============================================= #define TOUCH_CS_PIN 2 // T_CS (any GPIO) #define TOUCH_IRQ_PIN -1 // T_IRQ (any GPIO, or -1 if not connected) // ============================================= // ============================================= // Calibration values. // Run the TouchCalibration example and update these if touch is inaccurate. // Typical raw ranges: // - XPT2046 / HR2046 : ~200..3900 (default below) // - 4-wire resistive : ~100..900 // ============================================= #define TOUCH_LEFT_X 300 #define TOUCH_RIGHT_X 3700 #define TOUCH_TOP_Y 300 #define TOUCH_BOT_Y 3700 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) #define PEN_RADIUS 3 void setup() { TFT_display.begin(); TFT_display.setRotation(0); TFT_display.fillScreen(WHITE); TFT_display.initTouchSPI(TOUCH_CS_PIN, TOUCH_IRQ_PIN); // If touch X is mirrored on your board, uncomment: //TFT_display.setTouchInvertX(true); // If touch Y is mirrored on your board, uncomment: //TFT_display.setTouchInvertY(false); TFT_display.setTouchCalibration(TOUCH_LEFT_X, TOUCH_RIGHT_X, TOUCH_TOP_Y, TOUCH_BOT_Y); } void loop() { int x, y; if (TFT_display.getTouch(x, y)) { TFT_display.fillCircle(x, y, PEN_RADIUS, RED); } }

Folge mit

  • Verdrahte den XPT2046 mit dem Nano wie im Abschnitt Touch Get Point beschrieben.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Ziehe einen Finger über das Display, um auf dem Bildschirm zu zeichnen. Hebe ab und drücke erneut, um einen neuen Strich zu starten.

Methodenübersicht

Methode Erklärung Beispielaufruf
initTouchSPI(cs, irq) Startet den XPT2046 auf dem gemeinsamen SPI-Bus. TFT_display.initTouchSPI(7, 6);
setTouchCalibration(minX,maxX,minY,maxY) Ordnet rohe ADC-Werte den Bildschirm-Pixelkoordinaten zu. Hol dir diese Nummern aus dem TouchCalibration-Beispiel. TFT_display.setTouchCalibration(200, 3800, 300, 3700);
setTouchInvertX(invert) / setTouchInvertY(invert) Dreht die Touch-Achse um, wenn X oder Y auf deinem spezifischen Panel oder Batch gespiegelt wird. Rufe BEFORE setTouchCalibration() an. TFT_display.setTouchInvertY(true);
getTouch(x, y) Gibt kalibrierte Touch-Koordinaten in Pixeln zurück. Gibt wahr zurück, während der Bildschirm gedrückt wird. if (TFT_display.getTouch(x, y)) { ... }
fillCircle(x, y, r, color) Zeichnet einen kleinen Punkt an der Touch-Position. TFT_display.fillCircle(x, y, 3, RED);

Arduino Nano Code - Touch-Schaltfläche

Das Beispiel TouchButton zeichnet rechteckige On-Screen-Schaltflächen und lauscht auf Taps in jeder Schaltflächenregion. Wenn eine Schaltfläche angetippt wird, hebt sie sich hervor und löst ihre Aktion aus. Dies ist die Grundlage für ein Touch-gesteuert Menü auf dem Nano.

T_IRQ ist optional. Übergib -1 als irq-Argument, wenn du lieber den Interrupt-Pin nicht verbindest.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Touch Button Press/Release Example ------------------------------------ This example shows how to detect press and release events on a rectangular button using a DIYables SPI TFT display with a 4-wire resistive touch panel. When you touch inside the button, it changes colour and shows "PRESSED". When you release, it returns to its original state. NOTE: Run the TouchCalibration example and update setTouchCalibration() below if the touch coordinates are inaccurate. Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // ============================================= // TFT pins (always required) // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // SDO / MISO -> D12 (only needed when reading from display) // LED -> 3.3V (or any GPIO via initBacklight) // // XPT2046 / HR2046 / ADS7843 SPI touch controller // (modules with pins: T_CS, T_CLK, T_DIN, T_DO, T_IRQ) // Touch pin Arduino Uno / Nano // ------------ --------------------------------- // T_CS -> D2 (TOUCH_CS_PIN) // T_IRQ -> D2 (TOUCH_IRQ_PIN, optional - use -1 to skip) // T_CLK -> D13 (shared with display SCK) // T_DIN -> D11 (shared with display MOSI) // T_DO -> D12 (shared with display MISO) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Touch pin definitions (XPT2046 / HR2046 SPI touch controller) // ============================================= #define TOUCH_CS_PIN 2 // T_CS (any GPIO) #define TOUCH_IRQ_PIN -1 // T_IRQ (any GPIO, or -1 if not connected) // ============================================= // ============================================= // Calibration values. // Run the TouchCalibration example and update these if touch is inaccurate. // Typical raw ranges: // - XPT2046 / HR2046 : ~200..3900 (default below) // - 4-wire resistive : ~100..900 // ============================================= #define TOUCH_LEFT_X 300 #define TOUCH_RIGHT_X 3700 #define TOUCH_TOP_Y 300 #define TOUCH_BOT_Y 3700 // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); #define BLACK DIYables_TFT_SPI::colorRGB( 0, 0, 0) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) #define GRAY DIYables_TFT_SPI::colorRGB(128, 128, 128) #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) #define BUTTON_X 30 #define BUTTON_Y 100 #define BUTTON_W 180 #define BUTTON_H 60 #define DEBOUNCE_DELAY 50 bool lastPressed = false; bool stablePressed = false; unsigned long lastDebounceTime = 0; void drawButton(bool pressed) { uint16_t bg = pressed ? GRAY : RED; TFT_display.fillRect(BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, bg); TFT_display.drawRect(BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, BLACK); TFT_display.setTextColor(WHITE, bg); TFT_display.setTextSize(3); TFT_display.setCursor(BUTTON_X + 10, BUTTON_Y + 16); TFT_display.print(pressed ? "PRESSED" : " PRESS "); } void setup() { Serial.begin(9600); TFT_display.begin(); TFT_display.setRotation(0); TFT_display.fillScreen(WHITE); TFT_display.initTouchSPI(TOUCH_CS_PIN, TOUCH_IRQ_PIN); // If touch X is mirrored on your board, uncomment: //TFT_display.setTouchInvertX(true); // If touch Y is mirrored on your board, uncomment: //TFT_display.setTouchInvertY(false); TFT_display.setTouchCalibration(TOUCH_LEFT_X, TOUCH_RIGHT_X, TOUCH_TOP_Y, TOUCH_BOT_Y); drawButton(false); } void loop() { int x, y; bool pressed = false; if (TFT_display.getTouch(x, y)) { if (x >= BUTTON_X && x < (BUTTON_X + BUTTON_W) && y >= BUTTON_Y && y < (BUTTON_Y + BUTTON_H)) { pressed = true; } } if (pressed != lastPressed) { lastDebounceTime = millis(); } lastPressed = pressed; if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) { if (pressed != stablePressed) { stablePressed = pressed; drawButton(stablePressed); Serial.println(stablePressed ? "Button PRESSED" : "Button RELEASED"); } } }

Folge mit

  • Verbinde T_CS mit D7. T_IRQ kann nicht verbunden bleiben.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Tippe auf die Schaltflächen auf dem Display. Jede sollte die Farbe wechseln, wenn sie gedrückt wird.

Methodenübersicht

Methode Erklärung Beispielaufruf
initTouchSPI(cs, irq) Startet den XPT2046. Das Übergeben von -1 für irq deaktiviert den Interrupt-Pin und verwendet stattdessen Polling. TFT_display.initTouchSPI(7, -1);
setTouchCalibration(minX,maxX,minY,maxY) Wendet Kalibrierung an, so dass getTouch() genaue Pixelpositionen zurückgibt. TFT_display.setTouchCalibration(200, 3800, 300, 3700);
setTouchInvertX(invert) / setTouchInvertY(invert) Dreht die Touch-Achse um, wenn X oder Y auf deinem spezifischen Panel oder Batch gespiegelt wird. Rufe BEFORE setTouchCalibration() an. TFT_display.setTouchInvertY(true);
getTouch(x, y) Holt die kalibrierte Touch-Position. Gibt wahr zurück, wenn der Bildschirm berührt wird. if (TFT_display.getTouch(x, y)) { ... }
fillRect(x, y, w, h, color) Zeichnet den Schaltflächenhintergrund als durchgehendes Rechteck. TFT_display.fillRect(10, 10, 120, 60, BLUE);

Arduino Nano Code - Touch-Kalibrierung

Das Beispiel TouchCalibration führt dich durch das Finden des rohen ADC-Bereichs für dein XPT2046-Panel. Berühre jede Ecke des Bildschirms der Reihe nach und notiere die minimalen und maximalen X- und Y-Werte aus dem Serial Monitor. Stecke diese vier Nummern in setTouchCalibration() in allen anderen Touch-Skizzen.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Touch Screen Calibration Example --------------------------------- This example measures the raw touch coordinates at all four screen corners and prints ready-to-use calibration values to the Serial Monitor. It uses readTouchRaw() directly — it does NOT rely on getTouch() or any existing calibration values, so it works even when touch is completely broken. INSTRUCTIONS: 1. Upload this sketch to your board. 2. Open the Serial Monitor (Ctrl+Shift+M) and set baud rate to 9600. 3. The screen shows a blinking red dot in each corner, numbered 1–4: 1 = Top-left 2 = Top-right 3 = Bottom-right 4 = Bottom-left 4. Press and HOLD firmly on the blinking dot. Keep holding until the Serial Monitor prints "Captured!" for that corner. 5. Release, then wait for the next dot to appear and repeat. 6. After all 4 corners, the Serial Monitor prints the calibration values and a ready-to-use setTouchCalibration() call. Copy it into your sketch. NOTE: While waiting, the Serial Monitor continuously prints the live raw Z/X/Y readings so you can confirm that touch is being detected. Created by DIYables This example code is in the public domain Product page: https://diyables.io */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano) // ============================================= // TFT pins (always required) // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // SDO / MISO -> D12 (only needed when reading from display) // LED -> 3.3V (or any GPIO via initBacklight) // // XPT2046 / HR2046 / ADS7843 SPI touch controller // (modules with pins: T_CS, T_CLK, T_DIN, T_DO, T_IRQ) // Touch pin Arduino Uno / Nano // ------------ --------------------------------- // T_CS -> D2 (TOUCH_CS_PIN) // T_IRQ -> D2 (TOUCH_IRQ_PIN, optional - use -1 to skip) // T_CLK -> D13 (shared with display SCK) // T_DIN -> D11 (shared with display MOSI) // T_DO -> D12 (shared with display MISO) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Touch pin definitions (XPT2046 / HR2046 SPI touch controller) // ============================================= #define TOUCH_CS_PIN 2 // T_CS (any GPIO) #define TOUCH_IRQ_PIN -1 // T_IRQ (any GPIO, or -1 if not connected) // ============================================= // ============================================= // Create display object (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN); // Minimum pressure to count as a valid touch. #define TOUCH_Z_MIN 10 // How many consecutive valid samples required before a corner is accepted. #define SAMPLES_NEEDED 10 // Delay between samples (ms). #define SAMPLE_DELAY_MS 30 #define DOT_RADIUS 12 #define BLACK DIYables_TFT_SPI::colorRGB( 0, 0, 0) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) // Corner pixel positions — filled in setup() once display size is known. // Order: 0=top-left, 1=top-right, 2=bottom-right, 3=bottom-left int cx[4], cy[4]; // Captured averaged raw values per corner. int cap_x[4], cap_y[4]; // ----------------------------------------------------------------------- void drawDot(int corner, bool on) { uint16_t color = on ? RED : WHITE; TFT_display.fillCircle(cx[corner], cy[corner], DOT_RADIUS, color); TFT_display.setTextSize(2); TFT_display.setTextColor(BLACK, color); TFT_display.setCursor(cx[corner] - 6, cy[corner] - 8); TFT_display.print(corner + 1); } void captureCorner(int corner) { const char* names[] = { "Top-left", "Top-right", "Bottom-right", "Bottom-left" }; Serial.println(); Serial.print("Corner "); Serial.print(corner + 1); Serial.print(" ("); Serial.print(names[corner]); Serial.println(")"); Serial.println(" Press and HOLD firmly on the blinking dot."); Serial.println(" Keep holding until you see 'Captured!'"); unsigned long lastBlink = 0; unsigned long lastPrint = 0; bool dotOn = false; int goodSamples = 0; long sumX = 0, sumY = 0; while (true) { // Blink the dot if (millis() - lastBlink > 400) { lastBlink = millis(); dotOn = !dotOn; drawDot(corner, dotOn); } int raw_x, raw_y, z; TFT_display.readTouchRaw(raw_x, raw_y, z); // Print live readings every 500 ms if (millis() - lastPrint > 500) { lastPrint = millis(); Serial.print(" Z="); Serial.print(z); Serial.print(" X="); Serial.print(raw_x); Serial.print(" Y="); Serial.println(raw_y); } if (z >= TOUCH_Z_MIN) { sumX += raw_x; sumY += raw_y; goodSamples++; if (goodSamples >= SAMPLES_NEEDED) { cap_x[corner] = sumX / goodSamples; cap_y[corner] = sumY / goodSamples; Serial.print(" Captured! raw_x="); Serial.print(cap_x[corner]); Serial.print(" raw_y="); Serial.println(cap_y[corner]); drawDot(corner, false); delay(500); return; } } else { goodSamples = 0; sumX = 0; sumY = 0; } delay(SAMPLE_DELAY_MS); } } // ----------------------------------------------------------------------- void setup() { Serial.begin(9600); TFT_display.begin(); TFT_display.setRotation(0); // Always calibrate in rotation 0 TFT_display.fillScreen(WHITE); TFT_display.initTouchSPI(TOUCH_CS_PIN, TOUCH_IRQ_PIN); // If touch X is mirrored on your board, uncomment the line below // BEFORE calibrating (so the printed values match your panel): //TFT_display.setTouchInvertX(true); // If touch Y is mirrored on your board, uncomment: //TFT_display.setTouchInvertY(false); int w = TFT_display.width(); int h = TFT_display.height(); int m = DOT_RADIUS + 4; cx[0] = m; cy[0] = m; cx[1] = w - m; cy[1] = m; cx[2] = w - m; cy[2] = h - m; cx[3] = m; cy[3] = h - m; Serial.println("=== Touch Calibration ==="); for (int i = 0; i < 4; i++) { captureCorner(i); } // Derive calibration values from the four corners int min_x = (cap_x[0] + cap_x[3]) / 2; // left edge int max_x = (cap_x[1] + cap_x[2]) / 2; // right edge int min_y = (cap_y[0] + cap_y[1]) / 2; // top edge int max_y = (cap_y[2] + cap_y[3]) / 2; // bottom edge Serial.println(); Serial.println("=== Calibration Results ==="); Serial.print(" Left X (min_x): "); Serial.println(min_x); Serial.print(" Right X (max_x): "); Serial.println(max_x); Serial.print(" Top Y (min_y): "); Serial.println(min_y); Serial.print(" Bot Y (max_y): "); Serial.println(max_y); Serial.println(); Serial.println("Copy this line into your sketch:"); Serial.print(" TFT_display.setTouchCalibration("); Serial.print(min_x); Serial.print(", "); Serial.print(max_x); Serial.print(", "); Serial.print(min_y); Serial.print(", "); Serial.print(max_y); Serial.println(");"); TFT_display.fillScreen(WHITE); TFT_display.setTextColor(BLACK); TFT_display.setTextSize(2); TFT_display.setCursor(10, 10); TFT_display.println("Done! Check"); TFT_display.setCursor(10, 35); TFT_display.println("Serial Monitor"); } void loop() {}

Folge mit

  • Verdrahte den XPT2046 mit dem Nano wie im Abschnitt Touch Get Point beschrieben.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Öffne den Serial Monitor bei 9600 Baud. Berühre jede Ecke, wenn dazu aufgefordert.
  • Notiere die vier gedruckten Werte und verwende sie in setTouchCalibration() in deinen anderen Touch-Skizzen.

Methodenübersicht

Methode Erklärung Beispielaufruf
initTouchSPI(cs, irq) Initialisiert den XPT2046-Controller. TFT_display.initTouchSPI(7, 6);
readTouchRaw(x, y, z) Liest rohe ADC-Werte, die zur Ermittlung des Kalibrierungsbereichs verwendet werden. TFT_display.readTouchRaw(x, y, z);
setTouchCalibration(minX,maxX,minY,maxY) Speichert die vier Kalibrierungskonstanten, so dass getTouch() rohe Werte Pixelkoordinaten zuordnet. TFT_display.setTouchCalibration(200, 3800, 300, 3700);
setTouchInvertX(invert) / setTouchInvertY(invert) Dreht die Touch-Achse um, wenn X oder Y auf deinem spezifischen Panel oder Batch gespiegelt wird. Rufe BEFORE auf, um die Kalibrierung auszuführen, damit die gespeicherten Werte deinem Panel entsprechen. TFT_display.setTouchInvertY(true);

Arduino Nano Code - Custom SPI

Das Beispiel CustomSPI zeigt, wie du einen expliziten SPIClass-Zeiger an den Display-Konstruktor übergibst. Auf dem Nano gibt es einen einzigen Hardware-SPI-Bus (D11/D13/D12). Das explizite Übergeben von &SPI ermöglicht dir, die SPI-Einstellungen präzise zu kontrollieren - beispielsweise die Taktgeschwindigkeit bei langen Drähten zum Display zu reduzieren.

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino-nano/arduino-nano-tft-lcd-touch-display-spi */ /* Created by DIYables This example code is in the public domain Product page: https://diyables.io This example demonstrates how to use a custom (non-default) SPI bus with the DIYables TFT SPI library. This is useful on boards that have multiple SPI interfaces, such as: - ESP32: HSPI / VSPI - Arduino Giga / Portenta: SPI1 - Raspberry Pi Pico: SPI1 */ // ============================================= // Single include brings in the base class plus all driver classes. // ============================================= #include <DIYables_TFT_SPI.h> // ============================================= // Wiring (Arduino Uno / Nano - default SPI bus) // --------------------------------------------- // NOTE: Uno / Nano have only ONE hardware SPI bus, so this example is // most useful on boards with multiple buses (ESP32, Giga, RP2040...). // On Uno / Nano, MY_SPI must remain &SPI and the wiring is the standard // hardware SPI mapping below. // // TFT module Arduino Uno / Nano // ------------ --------------------------------- // VCC -> 5V // GND -> GND // CS -> D10 (TFT_CS_PIN) // RESET -> D9 (TFT_RST_PIN) // DC / RS -> D8 (TFT_DC_PIN) // SDI / MOSI -> D11 (hardware SPI MOSI) // SCK -> D13 (hardware SPI SCK) // LED -> 3.3V (or any GPIO via initBacklight) // SDO / MISO -> D12 (only needed when reading from display) // ============================================= // ============================================= // SPI pin definitions (adjust for your board) // ============================================= #define TFT_CS_PIN 10 #define TFT_DC_PIN 8 #define TFT_RST_PIN 9 // Panel resolution in native (portrait) orientation - change to match your module #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // ============================================= // Select alternate SPI bus (uncomment for your board) // ============================================= // --- ESP32: use HSPI --- // SPIClass hspi(HSPI); // #define MY_SPI &hspi // --- Arduino Giga / Portenta / RP2040: use SPI1 --- // #define MY_SPI &SPI1 // --- Default SPI (fallback) --- #define MY_SPI &SPI // ============================================= // Create display object with custom SPI bus // (uncomment matching driver) // ============================================= // DIYables_ILI9341_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN, MY_SPI); // DIYables_ILI9488_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN, MY_SPI); DIYables_ST7789_SPI TFT_display(TFT_WIDTH, TFT_HEIGHT, TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN, MY_SPI); #define BLACK DIYables_TFT_SPI::colorRGB(0, 0, 0) #define WHITE DIYables_TFT_SPI::colorRGB(255, 255, 255) #define RED DIYables_TFT_SPI::colorRGB(255, 0, 0) #define GREEN DIYables_TFT_SPI::colorRGB(0, 255, 0) #define BLUE DIYables_TFT_SPI::colorRGB(0, 0, 255) void setup() { Serial.begin(9600); // For ESP32 HSPI: optionally remap pins before begin() // hspi.begin(14, 12, 13, -1); // SCK, MISO, MOSI, SS TFT_display.begin(); TFT_display.setRotation(1); // Landscape TFT_display.fillScreen(BLACK); uint16_t w = TFT_display.width(); uint16_t h = TFT_display.height(); // Draw a simple test pattern TFT_display.fillRect(0, 0, w / 3, h, RED); TFT_display.fillRect(w / 3, 0, w / 3, h, GREEN); TFT_display.fillRect(w * 2 / 3, 0, w / 3, h, BLUE); TFT_display.setTextColor(WHITE); TFT_display.setTextSize(2); TFT_display.setCursor(10, h / 2 - 10); TFT_display.print("Custom SPI bus OK"); } void loop() { // Nothing to do }

Folge mit

  • Verdrahte das TFT-Display mit dem Nano wie im Schaltschema gezeigt.
  • Verbinde den Nano mit deinem Computer mit einem Mini-B-USB-Kabel.
  • Öffne Arduino IDE, wähle das Board und den Port aus, füge den Code ein und klicke auf Upload.
  • Das Display startet auf dem explizit konfigurierten SPI-Bus und zeigt ein Testmuster mit Farbbalken.

Methodenübersicht

Methode Erklärung Beispielaufruf
DIYables_ILI9341_SPI(w,h,cs,dc,rst,spi) Konstruktor, der einen expliziten SPIClass-Zeiger übernimmt. Lasse das letzte Argument weg, um auf &SPI als Standard einzustellen. DIYables_ILI9341_SPI tft(240, 320, 10, 9, 8, &SPI);
begin() Initialisiert das Display auf dem konfigurierten SPI-Bus. TFT_display.begin();

Fehlerbehebung

  1. Schwarzer Bildschirm - Überprüfe CS (D10), DC (D9), MOSI (D11) und SCK (D13). Ein schwebender CS-Pin ist der häufigste Schuldige.
  2. Falsche Farben oder Müllpixel - Bestätige, dass du den richtigen Driver-Konstruktor auskommentiert hast.
  3. SRAM-Überlauf - Der Nano hat 2 KB SRAM. Speichere Bitmaps in PROGMEM mit dem PROGMEM Schlüsselwort und dem DrawImage-Beispiel.
  4. Upload schlägt fehl mit "nicht synchronisiert" - Versuche, ATmega328P (Old Bootloader) im Menü Processor in Tools auszuwählen.

Plattformunterstützung

Die Bibliothek verwendet nur Arduino-Standard-APIs (SPI, digitalWrite, millis) und funktioniert auf allen Arduino-kompatiblen Plattformen.

※ UNSERE NACHRICHTEN

  • Sie können gerne den Link zu diesem Tutorial teilen. Bitte verwenden Sie jedoch unsere Inhalte nicht auf anderen Websites. Wir haben viel Mühe und Zeit in die Erstellung der Inhalte investiert, bitte respektieren Sie unsere Arbeit!