How to implement touch to Squareline/LVGL project, ILI9488 / XPT2046 drivers, ESP32 and Arduino


I exported a Squareline project and imported it in arduino. Settings are fine for the display since it shows exactly what it’s supposed to. But I can’t get the touch to work, when I click the button, the graphic effect of it being pressed does not appear. Also, on the serial terminal, nothing shows when I press on the touch but its supposed to display X and Y values. All the pins are well set up in the User_setup.h file for the TFT_eSPI library and wiring is fine since the TestCalibration project is working as it should (display fine, touch working). So what am I missing here? Thanks in advance!!!

What MCU/Processor/Board and compiler are you using?

Arduino IDE on ESP32-S2-MINI Custom Dev Board (I created my own dev board for test purpose)

What LVGL version are you using?


What do you want to achieve?

Make touch working so I can move ahead with my project!!! :sweat:

What have you tried so far?

Play with different pins, change SPI frequency, when to bed at 4am after watching tons of videos and tutorials… :sleeping:

Code to reproduce

Here is the ino file code:

#include <lvgl.h>
#include <TFT_eSPI.h>
#include "ui.h"

/*If you want to use the LVGL examples,
  make sure to install the lv_examples Arduino library
  and uncomment the following line.
#include <lv_examples.h>

/*Change to your screen resolution*/
static const uint16_t screenWidth  = 320;
static const uint16_t screenHeight = 480;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * screenHeight / 10 ];

TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */

#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char * buf)

/* Display flushing */
void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
    uint32_t w = ( area->x2 - area->x1 + 1 );
    uint32_t h = ( area->y2 - area->y1 + 1 );

    tft.setAddrWindow( area->x1, area->y1, w, h );
    tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );

    lv_disp_flush_ready( disp );

/*Read the touchpad*/
void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
    uint16_t touchX = 0, touchY = 0;

    bool touched = false;//tft.getTouch( &touchX, &touchY, 600 );

    if( !touched )
        data->state = LV_INDEV_STATE_REL;
        data->state = LV_INDEV_STATE_PR;

        /*Set the coordinates*/
        data->point.x = touchX;
        data->point.y = touchY;

        Serial.print( "Data x " );
        Serial.println( touchX );

        Serial.print( "Data y " );
        Serial.println( touchY );

void setup()
    Serial.begin( 115200 ); /* prepare for possible serial debug */

    String LVGL_Arduino = "Hello Arduino! ";
    LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();

    Serial.println( LVGL_Arduino );
    Serial.println( "I am LVGL_Arduino" );


#if LV_USE_LOG != 0
    lv_log_register_print_cb( my_print ); /* register print function for debugging */

    tft.begin();          /* TFT init */
    tft.setRotation( 2 ); /* Landscape orientation, flipped */

    uint16_t calData[5] = { 250, 3433, 290, 3583, 2 };

    lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 );

    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init( &disp_drv );
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register( &disp_drv );

    /*Initialize the (dummy) input device driver*/
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register( &indev_drv );


    Serial.println( "Setup done" );

void loop()
    lv_timer_handler(); /* let the GUI do its work */

Here is the User_setup.h file code:

#define USER_SETUP_INFO "User_Setup"
#define ILI9488_DRIVER
#define TFT_WIDTH  480
#define TFT_HEIGHT 320
#define TFT_MISO 19
#define TFT_MOSI 21
#define TFT_SCLK 18
#define TFT_CS 15 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
#define TFT_RST 4 // Reset pin (could connect to RST pin)
#define TFT_BL 26 // LED back-light (only for ST7789 with backlight control pin)
#define TOUCH_CS 16 // Chip select pin (T_CS) of touch screen
#define LOAD_GLCD
#define SPI_FREQUENCY  20000000
#define SPI_READ_FREQUENCY  20000000
#define SPI_TOUCH_FREQUENCY  2500000

Screenshot and/or video

I might be missing something but I suspect this is your problem:

bool touched = false;//tft.getTouch( &touchX, &touchY, 600 );

Shouldnt it be:

bool touched = tft.getTouch( &touchX, &touchY, 600 );


1 Like

@elgerg Haha, I was exactly trying that at the moment! Posting here made me notice that. I am trying right now, will let you know if I get it to work! Thanks for your fast response!

Thanks alot!! Working like a charm now!!! Wasted 3 entire days of my life for a commented line in my main ino file… Feels good now!! Thanks again!!

LOL, no problem. Glad I spotted it.
Please mark the answer as the solution so it will help anyone else in the same situation.

Kind regards

1 Like

Squareline should have automatically generated code so it can be used immediately. Without having to change the code “false”. Is it a bug?