240x240 ST7789V2 SPI IPS with FT6236 cap touch on XIAO ESP32S3 using GFX Library for Arduino
My exercise on Seeed Studio XIAO ESP32S3 Sense
(Arduino framework) to display on 1.54 inch 240x240 ST7789V2 SPI IPS with FT6236 cap touch.
ST7789 Display part using GFX Library for Arduino
FT6236 Cap touch part read via I2C (Wire)
Connection:
ST7789V2
--------
GND GND
VCC 3V3
SCL GPIO7
SDA GPIO9
RES GPIO1
DC GPIO2
CS GPIO3
BLK GPIO4
FT6236
------
SCL GPIO6
SDA GPIO5
INT GPIO43 (Not used)
RST GPIO44
Exercise Code:
XS3_ST7789_GFX_color_touch.ino
/*
XIAO ESP32S3 display on 240x240 ST7789 SPI IPS with FT6236 cap touch
using GFX Library for Arduino
- Color and Touch Test
Arduino_GFX
https://github.com/moononournation/Arduino_GFX
Connection:
ST7789V2
--------
GND GND
VCC 3V3
SCL GPIO7
SDA GPIO9
RES GPIO1
DC GPIO2
CS GPIO3
BLK GPIO4
FT6236
------
SCL GPIO6
SDA GPIO5
INT Not use
RST GPIO44
*/
#include <Arduino_GFX_Library.h>
#include <Wire.h>
/*******************************************************************************
* Start of Arduino_GFX setting
******************************************************************************/
//=== Custom to match my connection ===========================================
#define PIN_BLK 4
#define PIN_CS 3
#define PIN_DC 2
#define PIN_RES 1
#define PIN_SDA 9
#define PIN_SCL 7
#define GFX_BL PIN_BLK
#define CTP_INT 43
#define CTP_RST 44
#define FT6236U_ADDR 0x38
#define DISP_WIDTH 240
#define DISP_HEIGHT 240
Arduino_DataBus *bus = new Arduino_HWSPI(PIN_DC, PIN_CS);
Arduino_GFX *gfx = new Arduino_ST7789(bus, PIN_RES, 0 /* rotation */, true /* IPS */, DISP_WIDTH, DISP_HEIGHT);
/*******************************************************************************
* End of Arduino_GFX setting
******************************************************************************/
void setup(void)
{
delay(500);
Serial.begin(115200);
delay(500);
Serial.println("XIAO ESP32S3 display on 240x240 ST7789 SPI IPS");
Serial.println("Hardware Reset on CTP_RST: ");
Wire.begin(); // I2C
delay(100);
pinMode(CTP_RST, OUTPUT);
digitalWrite(CTP_RST, HIGH);
delay(100);
digitalWrite(CTP_RST, LOW);
delay(100);
digitalWrite(CTP_RST, HIGH);
delay(100);
// Init Display
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(RGB565_BLACK);
pinMode(GFX_BL, OUTPUT);
digitalWrite(GFX_BL, HIGH);
gfx->setCursor(0, 0);
gfx->setTextColor(RGB565_WHITE);
gfx->setTextSize(2);
gfx->println("XIAO ESP32S3 display on 240x240 ST7789 SPI IPS");
gfx->println("using GFX Library for Arduino");
gfx->println();
gfx->println("coXXect.blogspot.com");
delay(2000);
gfx->fillScreen(RGB565_WHITE);
gfx->fillRect(2, 2, gfx->width()-4, gfx->height()-4, RGB565_BLACK);
// Color Test
gfx->setTextSize(3);
gfx->fillScreen(RGB565_WHITE);
gfx->fillRect(2, 2, gfx->width()-4, gfx->height()-4, gfx->color565(0xFF, 0x00, 0x00));
gfx->setCursor(10, 200);
gfx->println("RED");
delay(2000);
gfx->fillScreen(RGB565_WHITE);
gfx->fillRect(2, 2, gfx->width()-4, gfx->height()-4, gfx->color565(0x00, 0xFF, 0x00));
gfx->setCursor(10, 200);
gfx->println("GREEN");
delay(2000);
gfx->fillScreen(RGB565_WHITE);
gfx->fillRect(2, 2, gfx->width()-4, gfx->height()-4, gfx->color565(0x00, 0x00, 0xFF));
gfx->setCursor(10, 200);
gfx->println("BLUE");
delay(2000);
gfx->fillScreen(RGB565_WHITE);
gfx->fillRect(2, 2, gfx->width()-4, gfx->height()-4, RGB565_BLACK);
}
void loop()
{
if (get_touch_count() > 0) {
int x, y;
get_touch_point(&x, &y);
gfx->fillCircle(x, y, 1, RGB565_WHITE);
}
delay(10);
}
int get_touch_count() {
// Register 0x02
// Touch count, max 2.
Wire.beginTransmission(FT6236U_ADDR);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(FT6236U_ADDR, 1);
return (Wire.read());
}
// Read x, y
// May be have to exchange/adjust x, y according to screen rotation.
void get_touch_point(int *x, int *y) {
// Read X:
// Register 0x03
// bit7-bit6 Touch event of the 1st touch point
// bit3-bit0 Hight 4 bit of the 1st touch point
// Register 0x04
// Low 8 bit of the 1st touch point
Wire.beginTransmission(FT6236U_ADDR);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(FT6236U_ADDR, 2);
*x = (Wire.read() & 0x0f) << 8 | Wire.read();
*x = DISP_WIDTH - *x;
// Read Y:
// Register 0x05
// bit7-bit6 ID of the 1st touch point
// bit3-bit0 Hight 4 bit of the 1st touch point
// Register 0x06
// Low 8 bit of the 1st touch point
Wire.beginTransmission(FT6236U_ADDR);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(FT6236U_ADDR, 2);
*y = (Wire.read() & 0x0f) << 8 | Wire.read();
*y = DISP_WIDTH - *y;
}
Next:
~ Arduino_GFX_Library + LVGL on ESP32S3 + ST7789 SPI IPS with FT6236 cap touch, create button to toggle onboard LED.
~ Xiao ESP32S3 Sense read MicroSD
~ ESP32S3 (Arduino framework) decode jpg and display on ST7789 SPI Display.
Comments
Post a Comment