SPI mode requires 4 pins total (SPI data in, data out, clock, select, and d/c) for the display, plus 4 pins for the touchscreen (8 pins total minimum for display + touchscreen).
/*
3.5" TFT 320x40 HDX8357 display breakout, AF PN 2050
FeatherWing_HDX8357_TFT_LCD_PN2050.ino
*/
/////////////////////////////////////////////////////////////////////////////\/
// Show serial messages when DEBUG = true, otherwise minimize them.
#define DEBUG false
//////////////////////////////////////////////////////////////////////////////
// Adafruit HX8357 displays
// Install library "Adafruit_HX8357"
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#ifdef ESP8266
#define STMPE_CS 16
#define TFT_CS 0
#define TFT_DC 15
#define SD_CS 2
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
#define STMPE_CS 32
#define TFT_CS 15
#define TFT_DC 33
#define SD_CS 14
#elif defined(TEENSYDUINO)
#define TFT_DC 10
#define TFT_CS 4
#define STMPE_CS 3
#define SD_CS 8
#elif defined(ARDUINO_STM32_FEATHER)
#define TFT_DC PB4
#define TFT_CS PA15
#define STMPE_CS PC7
#define SD_CS PC5
#elif defined(ARDUINO_NRF52832_FEATHER) /* BSP 0.6.5 and higher! */
#define TFT_DC 11
#define TFT_CS 31
#define STMPE_CS 30
#define SD_CS 27
#elif defined(ARDUINO_MAX32620FTHR) || defined(ARDUINO_MAX32630FTHR)
#define TFT_DC P5_4
#define TFT_CS P5_3
#define STMPE_CS P3_3
#define SD_CS P3_2
#else
// Anything else, defaults!
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
#endif
// The following can be assigned to any pin: TFT_CS, TFT_DC, TFT_RST
// For most Feathers, MOSI = 24, SCK = 25, MISO = 23
#define TFT_RST -1 // RST can be set to -1 if you tie it to Arduino's reset
// Hardware SPI is on avg 1.7x faster than SoftSPI
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
// SoftSPI - note that on some processors this might be *faster* than hardware SPI!
//Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO);
// HX8357_BLACK HX8357_WHITE HX8357_RED HX8357_GREEN HX8357_YELLOW
void splashScreen(uint32_t delay_ms) {
// splash screen:
// 01234567890123456789012345
// Mechatronic Solutions LLC
// www.MechatronicSolutionsLLC.com
tft.setRotation(3); // landscape inverted
tft.fillScreen(HX8357_WHITE); // clear the screen
tft.setCursor(0, 0); // top left corner in pixels. +x to right, +y down
tft.setTextSize(3);
tft.setTextColor(HX8357_BLUE);
//tft.println("26 chars wide x 12 rows");
//tft.println("01234567890123456789012345");
for (uint8_t r=0; r<5; r++) {
tft.println("");
}
tft.println(" Mechatronic Solutions LLC");
tft.println(""); tft.println("");
tft.setTextSize(2);
tft.setTextColor(HX8357_BLACK);
//tft.println("0123456789012345678901234567890123456789");
tft.println(" www.MechatronicSolutionsLLC.com");
// drawRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
tft.drawRect(10, 10, tft.width()-20, tft.height()-20, HX8357_BLACK);
yield();
delay(delay_ms);
}
//////////////////////////////////////////////////////////////////////////////
void setup() {
#if DEBUG
Serial.begin(115200);
while (!Serial) {
delay(1);
}
Serial.println("\nSerial ready\n");
#endif
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
// 3.5" TFT 320x40 HDX8357 display breakout, AF PN 2050
#if DEBUG
Serial.print("TFT_CS: "); Serial.println(TFT_CS);
Serial.print("TFT_DC: "); Serial.println(TFT_DC);
Serial.print("TFT_RST: "); Serial.println(TFT_RST);
Serial.print("STMPE_CS: "); Serial.println(STMPE_CS);
Serial.print("SD_CS: "); Serial.println(SD_CS);
Serial.print("SCK: "); Serial.println(SCK);
Serial.print("MOSI: "); Serial.println(MOSI);
Serial.print("MISO: "); Serial.println(MISO);
#endif
tft.begin();
splashScreen(1000);
/*
tft.fillScreen(HX8357_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(HX8357_WHITE); tft.setTextSize(1);
tft.println(".setTextSize(1)");
tft.println("80 chars wide");
tft.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
tft.println("Too small for most people to read");
tft.println("");
tft.setTextColor(HX8357_WHITE); tft.setTextSize(2);
tft.println(".setTextSize(2)");
tft.println("40 chars wide x 19 rows");
tft.println("0123456789012345678901234567890123456789");
tft.println("");
tft.setTextColor(HX8357_WHITE); tft.setTextSize(3);
tft.println(".setTextSize(3)");
tft.println("26 chars wide x 12 rows");
tft.println("01234567890123456789012345");
tft.println("");
*/
} // setup()
void loop() {
tft.fillScreen(HX8357_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(HX8357_WHITE); tft.setTextSize(1);
tft.println(".setTextSize(1)");
tft.println("80 chars wide");
tft.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
tft.println("Too small for most people to read");
tft.println("");
tft.setTextColor(HX8357_RED); tft.setTextSize(2);
tft.println(".setTextSize(2)");
tft.println("40 chars wide x 19 rows");
tft.println("0123456789012345678901234567890123456789");
tft.setTextColor(HX8357_GREEN);
tft.println("0123456789012345678901234567890123456789");
tft.setTextColor(HX8357_BLUE);
tft.println("0123456789012345678901234567890123456789");
tft.setTextColor(HX8357_WHITE);
tft.println("0123456789012345678901234567890123456789");
tft.println("");
tft.setTextColor(HX8357_GREEN); tft.setTextSize(3);
tft.println(".setTextSize(3)");
tft.println("26 chars wide x 12 rows");
tft.setTextColor(HX8357_BLUE);
tft.println("01234567890123456789012345");
tft.println("");
delay(3000);
tft.fillScreen(HX8357_WHITE);
tft.setCursor(0, 0);
tft.setTextColor(HX8357_BLACK); tft.setTextSize(1);
tft.println(".setTextSize(1)");
tft.println("80 chars wide");
tft.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
tft.println("Too small for most people to read");
tft.println("");
tft.setTextColor(HX8357_RED); tft.setTextSize(2);
tft.println(".setTextSize(2)");
tft.println("40 chars wide x 19 rows");
tft.println("0123456789012345678901234567890123456789");
tft.println("");
tft.setTextColor(HX8357_BLUE); tft.setTextSize(3);
tft.println(".setTextSize(3)");
tft.println("26 chars wide x 12 rows");
tft.println("01234567890123456789012345");
tft.setTextColor(HX8357_BLACK);
tft.println("01234567890123456789012345");
tft.println("");
delay(3000);
} // loop()
Designed to use SPI mode for both the display and the touchscreen. SPI mode is about 2 to 4 times slower than 8-bit mode, but adequate for basic graphics. In SPI mode, 4 pins are required for the display (CLK, MOSI, CS, D/C), and 4 pins (Y+, X+, Y-, X-) for the touchscreen (2x digital, 2x analog). Optionally you will need 3 pins for the SD card (MISO, CS/CCS, CD) and 2x to control the display reset (RST) and backlight (Lite).
The HX8357 library is used for the display, and the Adafruit TouchScreen Library is used for the touchscreen.
Do you need help developing or customizing a IoT product for your needs? Send me an email requesting a free one hour phone / web share consultation.
The information presented on this website is for the author's use only. Use of this information by anyone other than the author is offered as guidelines and non-professional advice only. No liability is assumed by the author or this web site.