Raspberry Pi Pico 2 + Waveshare 1.9inch Touch LCD, using Arduino_GFX_Library in Arduino Framework.

Raspberry Pi Pico 2Waveshare 1.9inch Touch LCD (170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch) in Arduino Framework, interface with ST7789 using Arduino_GFX_Library (GFX Library for Arduino by Moon On Our Nation), and reading register from CST816 via I2C.


Connection:

Code:

rpPico2_Arduino_GFX.ino, setup for Arduino_GFX_Library, color test.

/*
Raspberry Pi Pico 2 + 
Waveshare 1.9inch Touch LCD, 170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch.
Using Arduino_GFX_Library (GFX Library for Arduino by Moon On Our Nation)

Details:
https://coxxect.blogspot.com/2025/12/raspberry-pi-pico-2-waveshare-19inch.html

Using board of Raspberry Pi Pico/RP2040/RP2350 by Earle F. Philhower III in Boards Manager.
https://github.com/earlephilhower/arduino-pico

Install Earle Philhower Raspberry Pi Pico Arduino core in Arduino IDE
https://coxxect.blogspot.com/2023/08/install-earle-philhower-raspberry-pi.html

*/
/*******************************************************************************
 * Start of Arduino_GFX setting
 * Raspberry Pi Pico dev board : CS: 17, DC: 22, RST: 21, BL: 20, SCK: 18, MOSI: 19, MISO: 16
 ******************************************************************************/
#include <Arduino_GFX_Library.h>

#define SPI_MOSI      19
#define SPI_SCK       18
#define TFT_CS  17
#define TFT_DC  22
#define TFT_RST 21
#define TFT_BL  20

// #define TFT_ROTATION 0
// #define TFT_WIDTH 170
// #define TFT_HEIGHT 320
// #define TFT_COLSTART 35
// #define TFT_ROWSTART 0
#define TFT_ROTATION 3
#define TFT_WIDTH 170
#define TFT_HEIGHT 320
#define TFT_COLSTART 35
#define TFT_ROWSTART 0

/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
Arduino_DataBus *bus = new Arduino_RPiPicoSPI(TFT_DC, TFT_CS, SPI_SCK, SPI_MOSI, GFX_NOT_DEFINED /* MISO */);
//Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, TFT_CS);
//Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, SPI_SCK, SPI_MOSI, GFX_NOT_DEFINED /* MISO */);

/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, TFT_ROTATION, true /* IPS */,
                                      TFT_WIDTH, TFT_HEIGHT,
                                      TFT_COLSTART, TFT_ROWSTART);

/*******************************************************************************
 * End of Arduino_GFX setting
 ******************************************************************************/

void setup(void)
{

  delay(2000);
  Serial.begin(115200);
  delay(500);

  Serial.println("--- Start ---");

  // Get default pins of SPI and I2C, defined in:
  // C:\Users\<user>\AppData\Local\Arduino15\packages\rp2040\hardware\rp2040\5.4.3\variants\rpipico2\pins_arduino.h
  // C:\Users\<user>\AppData\Local\Arduino15\packages\rp2040\hardware\rp2040\5.4.3\variants\generic\common.h
  Serial.println("Default pins for SPI and I2C");
  Serial.println("- SPI -");
  Serial.printf("SS:    %i\n", SS);
  Serial.printf("MOSI:  %i\n", MOSI);
  Serial.printf("MISO:  %i\n", MISO);
  Serial.printf("SCK:   %i\n", SCK);
  Serial.println("- I2C -");
  Serial.printf("SDA:   %i\n", SDA);
  Serial.printf("SCL:   %i\n", SCL);

  // Serial.setDebugOutput(true);
  // while(!Serial);
  Serial.println("Arduino_GFX_Library color test");

  // Init Display
  if (!gfx->begin())
  {
    Serial.println("gfx->begin() failed!");
  }
  gfx->fillScreen(RGB565_BLACK);

  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, LOW);  //Turn On Backlight

  gfx->drawRect(0, 0, gfx->width()-1, gfx->height()-1, RGB565_WHITE);

  gfx->setCursor(10, 10);
  gfx->setTextSize(3 /* x scale */, 3 /* y scale */, 3 /* pixel_margin */);
  gfx->println("Raspberry Pi Pico 2");
  gfx->setTextSize(2 /* x scale */, 2 /* y scale */, 2 /* pixel_margin */);
  gfx->println();
  gfx->println("Waveshare 1.9inch Touch LCD, 170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch.");
  gfx->println("Using Arduino_GFX_Library.");

  delay(7000); // 5 seconds
}

#define num_of_color 5
int color[num_of_color] = {RGB565_RED, RGB565_GREEN, RGB565_BLUE, RGB565_WHITE, RGB565_BLACK};
String color_name[num_of_color] = {"RED", "GREEN", "BLUE", "WHITE", "BLACK"};
int color_index = 0;

void loop()
{
  unsigned long startTime = millis();
  gfx->fillScreen(color[color_index]);
  gfx->drawRect(0, 0, gfx->width()-1, gfx->height()-1, color[color_index]^0xFFFF);

  gfx->setTextColor(color[color_index]^0xFFFF);
  gfx->setCursor(10, 70);
  gfx->setTextSize(6 /* x scale */, 6 /* y scale */, 2 /* pixel_margin */);
  gfx->println(color_name[color_index]);
  unsigned long stopTime = millis();

  gfx->setCursor(10, 140);
  gfx->setTextSize(3 /* x scale */, 3 /* y scale */, 3 /* pixel_margin */);
  //gfx->println(String(stopTime-startTime));
  gfx->printf("%i ms", stopTime-startTime);
  Serial.println(String(stopTime-startTime));

  color_index++;
  if (color_index==num_of_color){
    color_index = 0;
  }

  delay(1000); // 1 second
}



rpPico2__CST816.ino, Read CST816 registers via I2C.
/*
Raspberry Pi Pico 2 + 
Waveshare 1.9inch Touch LCD, 170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch.
Read CST816 registers via I2C.

Details:
https://coxxect.blogspot.com/2025/12/raspberry-pi-pico-2-waveshare-19inch.html

Using board of Raspberry Pi Pico/RP2040/RP2350 by Earle F. Philhower III in Boards Manager.
https://github.com/earlephilhower/arduino-pico

Install Earle Philhower Raspberry Pi Pico Arduino core in Arduino IDE
https://coxxect.blogspot.com/2023/08/install-earle-philhower-raspberry-pi.html

*/
#include <Wire.h>

#define CST816_ADDR 0x15

#define TP_SDA 4  // default I2C SDA
#define TP_SCL 5  // default I2C SCL
#define TP_RST 3
#define TP_IRQ 2

// Struct to store CST816 register read-in.
struct TouchData {
  uint8_t gesture;
  uint8_t fingers;
  uint16_t x;
  uint16_t y;

  // Compare if registers changed
  bool operator!=(const TouchData &other) const {
    return (gesture != other.gesture ||
            fingers != other.fingers ||
            x != other.x ||
            y != other.y);
  }
};

// Current and Last CST816 register read-in
TouchData currentData = {0, 0, 0, 0};
TouchData lastData    = {0, 0, 0, 0};

void setup() {
  delay(2000);
  Serial.begin(115200);
  delay(500);
  Serial.println("~ Start ~");
  
  Wire.begin();

  // Send a Reset to CST816
  pinMode(TP_RST, OUTPUT);
  digitalWrite(TP_RST, HIGH);
  delay(50);
  digitalWrite(TP_RST, LOW);
  delay(30);
  digitalWrite(TP_RST, HIGH);
  delay(50);
}

void loop() {
  // Read registers started from 0x01
  Wire.beginTransmission(CST816_ADDR);
  Wire.write(0x01);
  Wire.endTransmission(false);

  Wire.requestFrom(CST816_ADDR, 6);
  if (Wire.available() == 6) {
    currentData.gesture = Wire.read();
    currentData.fingers = Wire.read();
    currentData.x = (Wire.read() & 0x0F) << 8 | Wire.read();
    currentData.y = (Wire.read() & 0x0F) << 8 | Wire.read();

    // check if CST816 register read-in changed
    if (currentData != lastData) {
      Serial.print("Gesture: "); Serial.print(currentData.gesture);
      Serial.print(" Fingers: "); Serial.print(currentData.fingers);
      Serial.print(" X: "); Serial.print(currentData.x);
      Serial.print(" Y: "); Serial.println(currentData.y);

      // Update CST816 register read-in
      lastData = currentData;
    }
  }

  delay(30);
}



rpPico2_ST7789_CST816.ino, interface with ST7789 using Arduino_GFX_Library, read CST816 registers via I2C, and draw something on screen.

/*
Raspberry Pi Pico 2 + 
Waveshare 1.9inch Touch LCD, 170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch.
Interface with ST7789 using Arduino_GFX_Library, read CST816 registers via I2C, and draw something on screen.

Details:
https://coxxect.blogspot.com/2025/12/raspberry-pi-pico-2-waveshare-19inch.html

Using board of Raspberry Pi Pico/RP2040/RP2350 by Earle F. Philhower III in Boards Manager.
https://github.com/earlephilhower/arduino-pico

Install Earle Philhower Raspberry Pi Pico Arduino core in Arduino IDE
https://coxxect.blogspot.com/2023/08/install-earle-philhower-raspberry-pi.html

*/
/*******************************************************************************
 * Start of Arduino_GFX setting
 * Raspberry Pi Pico dev board : CS: 17, DC: 22, RST: 21, BL: 20, SCK: 18, MOSI: 19, MISO: 16
 ******************************************************************************/
#include <Arduino_GFX_Library.h>
#include <Wire.h>

#define SPI_MOSI  19
#define SPI_SCK   18
#define TFT_CS    17
#define TFT_DC    22
#define TFT_RST   21
#define TFT_BL    20

#define TP_SDA 4  // default I2C SDA
#define TP_SCL 5  // default I2C SCL
#define TP_RST 3
#define TP_IRQ 2

#define TFT_ROTATION 0
#define TFT_WIDTH 170
#define TFT_HEIGHT 320
#define TFT_COLSTART 35
#define TFT_ROWSTART 0

/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
Arduino_DataBus *bus = new Arduino_RPiPicoSPI(TFT_DC, TFT_CS, SPI_SCK, SPI_MOSI, GFX_NOT_DEFINED /* MISO */);
//Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, TFT_CS);
//Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, SPI_SCK, SPI_MOSI, GFX_NOT_DEFINED /* MISO */);

/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, TFT_ROTATION, true /* IPS */,
                                      TFT_WIDTH, TFT_HEIGHT,
                                      TFT_COLSTART, TFT_ROWSTART);

/*******************************************************************************
 * End of Arduino_GFX setting
 ******************************************************************************/

#define CST816_ADDR 0x15

// Struct to store CST816 register read-in.
struct TouchData {
  uint8_t gesture;
  uint8_t fingers;
  uint16_t x;
  uint16_t y;

  // Compare if registers changed
  bool operator!=(const TouchData &other) const {
    return (gesture != other.gesture ||
            fingers != other.fingers ||
            x != other.x ||
            y != other.y);
  }
};

// Current and Last CST816 register read-in
TouchData currentData = {0, 0, 0, 0};
TouchData lastData    = {0, 0, 0, 0};

void setup(void)
{
  delay(2000);
  Serial.begin(115200);
  delay(500);
  Serial.println("--- Start ---");

  Wire.begin();

  // Send a Reset to CST816
  pinMode(TP_RST, OUTPUT);
  digitalWrite(TP_RST, HIGH);
  delay(50);
  digitalWrite(TP_RST, LOW);
  delay(30);
  digitalWrite(TP_RST, HIGH);
  delay(50);

  // Init Display
  if (!gfx->begin())
  {
    Serial.println("gfx->begin() failed!");
  }
  gfx->fillScreen(RGB565_BLACK);

  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, LOW);  //Turn On Backlight

  gfx->drawRect(0, 0, gfx->width()-1, gfx->height()-1, RGB565_WHITE);

  gfx->setCursor(10, 10);
  gfx->setTextSize(3 /* x scale */, 3 /* y scale */, 3 /* pixel_margin */);
  gfx->println("RPi Pico 2");
  gfx->setTextSize(2 /* x scale */, 2 /* y scale */, 2 /* pixel_margin */);
  gfx->println();
  gfx->println("Waveshare 1.9inch Touch LCD, 170x320 ST7789V2 SPI LCD and CST816 I2C capacitive touch.");
  gfx->println("Using Arduino_GFX_Library,");
  gfx->println("+ CST816 cap. touch.");

  delay(5000); // 5 seconds

  gfx->fillScreen(RGB565_GRAY);
  gfx->drawRect(0, 0, gfx->width()-1, gfx->height()-1, RGB565_WHITE);
}

void loop(){
  // Read registers started from 0x01
  Wire.beginTransmission(CST816_ADDR);
  Wire.write(0x01);
  Wire.endTransmission(false);

  Wire.requestFrom(CST816_ADDR, 6);
  if (Wire.available() == 6) {
    currentData.gesture = Wire.read();
    currentData.fingers = Wire.read();
    currentData.x = (Wire.read() & 0x0F) << 8 | Wire.read();
    currentData.y = (Wire.read() & 0x0F) << 8 | Wire.read();

    // check if CST816 register read-in changed
    if (currentData != lastData) {
      //Serial.print("Gesture: "); Serial.print(currentData.gesture);
      //Serial.print(" Fingers: "); Serial.print(currentData.fingers);
      //Serial.print(" X: "); Serial.print(currentData.x);
      //Serial.print(" Y: "); Serial.println(currentData.y);

      if ((lastData.fingers==0x00) && (currentData.fingers!=0x00)){
        // Touch down, clear screen.
          gfx->fillScreen(RGB565_GRAY);
          gfx->drawRect(0, 0, gfx->width()-1, gfx->height()-1, RGB565_WHITE);
          gfx->fillCircle(currentData.x, currentData.y, 8, RGB565_BLUE);
      }else{
          gfx->drawCircle(currentData.x, currentData.y, 3, RGB565_RED);
      }

      // Update CST816 register read-in
      lastData = currentData;
    }
  }

  delay(30);
}



Related:
Install Earle Philhower Raspberry Pi Pico Arduino core


Comments

Popular posts from this blog

Drive 320x240 ILI9341 SPI TFT using ESP32-S3 (NodeMCU ESP-S3-12K-Kit) using TFT_eSPI library, in Arduino Framework.

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library