ESPHome & ILI9341 touchscreen

The screen display and the touch interface are separate devices as far as esphome is concerned. I cannot remember where I read that the ili9341 was paired with the xpt2046, probably here ILI9341 IPS - openHASP

I certainly suggest giving openhasp a try.

For future folks need a jump on the screen pins.
SCK to the TCLK
MOSI to the TDO
MISO to the TDIN

So is this right?

SCK to the T_CLK
MOSI to the T_DO
MISO to the T_DIN
CS to the T_CS
Pin 16 to the T_IRQ?

Where the first four pins are shared and the IRQ one is just connected to pin 16 on the esp32?

Not getting any luck with the pins above can someone confirm if they are correct? Just get a blank screen and this (without touching the screen at all)

[10:16:31][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:33][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:43][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:44][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:45][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:45][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:16:45][I][cal:082]: x=0, y=240, x_raw=0, y_raw=0
[10:29:44][I][cal:083]: x=320, y=0, x_raw=8191, y_raw=8191
[10:29:44][I][cal:083]: x=320, y=0, x_raw=8191, y_raw=8191
[10:29:44][I][cal:083]: x=320, y=0, x_raw=8191, y_raw=8191

yaml:

spi:
  clk_pin: GPIO18 # (Pin on display - SCK/T_CLK)
  mosi_pin: GPIO23 # (Pin on display - SDI(MOSI)/T_DIN)
  miso_pin: GPIO19 # (Pin on display - SDO(MISO)/T_DO)

touchscreen:
  platform: xpt2046
  id: my_touchscreen
  cs_pin: GPIO14
  interrupt_pin: GPIO16
  on_touch:
    - lambda: |-
          ESP_LOGI("cal", "x=%d, y=%d, x_raw=%d, y_raw=%0d",
              id(my_touchscreen).x,
              id(my_touchscreen).y,
              id(my_touchscreen).x_raw,
              id(my_touchscreen).y_raw
              );

display:
  - platform: ili9341
    model: TFT 2.4
    rotation: 90
    cs_pin: GPIO14 # (Pin on display - CS)
    dc_pin: GPIO27 # (Pin on display - DC)
    led_pin: GPIO32 # (Pin on display - LED)
    reset_pin: GPIO33 # (Pin on display - RESET)
    update_interval: 3s
    id: my_display

I have all the pins on ili9341 connected

I have the same problem, first it doesn’t spam the calibration message at all but after touching the screen once, it starts just spamming with all zeros. My configuration looks pretty much the same and I have connected clock, miso and mosi to both the lcd and touch

I was having the same issue with the touch aspect calibration. I followed the wiring in this video ILI9341 TFT LCD to ESP32 - Full HOW TO for display, SD card and Touch. Using TFT_eSPI driver - YouTube which had the MOSI jumped to T_DIN, the MISO jumped to T_DO and left the T_IRQ unused. It now works exactly as it should.

Is it possible you could share the exact connection and the esp home config?

Look here. At the bottom of the page

1 Like

Just for every one here. This is how I got solved it with the help of @pepe59

ESPHome version 2023.8.2

  • mosi_pin is connected to SDI(MOSI) and T_DIN
  • miso_pin is only connected to T_DO only.
  • clk_pin is connected to SCK and T_CLK

Please make sure you connect the T_IRQ to the interrupt_pin. It is required for the touch to work.

Hi, the components are updated and your code is not accepted in ESPHome anymore. Did you update your code and would you be able to share the updated code ? Thanks!

See 2 posts above.
I put the working code including touch on github.
For calibration, proceed according to the documentation.

1 Like

Thank you for pointing to the github. Unfortunately i still do not get it to work. Maybe you know what i am doing wrong ?

ESPHome Console error:
[23:53:16][W][component:204]: Component ili9xxx.display took a long time for an operation (0.13 s). [23:53:16][W][component:205]: Components should block for at most 20-30ms.

font:

  - file: "gfonts://Roboto"

    id: my_font

    size: 20

color:

  - id: my_red

    red: 100%

spi:

 clk_pin: 18 #display SCK/touch T_CLK

 mosi_pin: 23 #display SDI(MOSI)/touch T_DIN

 miso_pin: 19 #touch T_DO

touchscreen:

  platform: xpt2046

  id: my_touchscreen

  cs_pin: 17 #touch T_CS

  interrupt_pin: 16 #touch T_IRQ

  update_interval: 50ms

  report_interval: 1s

  threshold: 400

  calibration_x_min: 283

  calibration_x_max: 3776

  calibration_y_min: 3870

  calibration_y_max: 392

  swap_x_y: False

display:

  - platform: ili9xxx

    model: TFT 2.4

    cs_pin: 13 #display CS

    dc_pin: 27 #display DC

    reset_pin: 33 #display RESET

    rotation: 90

    id: tft_ha_test

    lambda: |-

      it.fill(Color::BLACK);

      it.print(0, 0, id(my_font), id(my_red), TextAlign::TOP_LEFT, "Hello World!");

# Define a PWM output on the ESP32

output:

  - platform: ledc

    pin: 32 #display LED

    id: gpio_32_backlight_pwm

# Define a monochromatic, dimmable light for the backlight

light:

  - platform: monochromatic

    output: gpio_32_backlight_pwm

    name: "Display HA test podsvícení"

    id: back_light

    restore_mode: ALWAYS_ON


ignore the wires on this picture, but it folows these pins
image

What display do you have?
I use ILI9341 2.8" and 2.4"
Then in the yaml code

display:
  - platform: ili9xxx
    model: ili9341

The error you describe is known.
Just add it to yaml

logger:
  logs:
    component: ERROR

Thanks for helping out!
I use a 2.4 TFT 14P screen
https://nl.aliexpress.com/item/1005005916960027.html?spm=a2g0o.order_list.order_list_main.59.27d779d25mrl54&gatewayAdapt=glo2nld

I got it to work i think. It looks like the model isn’t TFT 2.4 but TFT 2.4R. At least the strange noise on the screen is gone

Try TFT 2.4R
You were faster :grinning:

1 Like

Appreciate your quick reply’s! Really helped me out a lot! Now the fun stuf can start of programming the screen. Goal is to show what music is playing on spotify/sonos, and later add in some controles to skip tracks and so on.

Have a great day!

1 Like

I’m working on something similar right now.
Touch control for squeezebox (LMS)

1 Like

Unfortunately my screen appears to not have touchscreen (it does have the headers) so maybe i order it but first want to see if it works with buttons and i will use the display. Will share the code if i get it to work

Thinking on printing one of the following cases. the first gives me room for physical buttons

2 Likes

So I have this weird issue.

This is my config

esphome:
  name: "lcd-with-touch-esp32"
  friendly_name: LCD-With-Touch-ESP32

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "0y8By35x9D8mqZSghntlGOja1GpZg6Fr8gFcNvjxXQg="

ota:
  password: "4e3b4930a3ff2672415c158b17331a0d"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip: 
    static_ip: 192.168.0.85
    gateway: 192.168.0.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lcd-Esp32 Fallback Hotspot"
    password: "vxLa8dfwFIL1"

captive_portal:

web_server:
  port: 80

spi:
 clk_pin: 18
 mosi_pin: 23
 miso_pin: 14

touchscreen:
  platform: xpt2046
  id: my_touchscreen
  cs_pin: 33
  interrupt_pin: 19
  update_interval: 20ms

  calibration_x_max: 3691
  calibration_x_min: 319
  calibration_y_max: 453
  calibration_y_min: 3873
  on_touch:
    - lambda: |-
          ESP_LOGI("cal", "x=%d, y=%d, x_raw=%d, y_raw=%d",
              id(my_touchscreen).x,
              id(my_touchscreen).y,
              id(my_touchscreen).x_raw,
              id(my_touchscreen).y_raw
              );

display:
  - platform: ili9xxx
    model: ILI9341
    cs_pin: 27
    dc_pin: 26
    reset_pin: 5
    rotation: 270
    lambda: |-
      auto red = Color(255, 0, 0);
      auto green = Color(0, 255, 0);
      auto blue = Color(0, 0, 255);
      auto white = Color(255, 255, 255);
      it.fill(green);
      if (id(my_touchscreen).touched)
        it.filled_circle(id(my_touchscreen).x, id(my_touchscreen).y, 10, red);

switch:
  - platform: gpio
    pin: 
      number: 4
    restore_mode: ALWAYS_ON

    name: "Living Room Dehumidifier"

Now if i start the esp32 with the display connected and try touching the screen. It does not work.

Then while the power is still connected and the ESP32 is on, If i disconnect the display and reconnect it, the touch starts to work( i can see in the logs the touch coordinates) , but the display doesn’t show any colors on the screen (Some blue color is displayed instead of Green).

Next, If i continues to install the same image over wireless option using OTA, the OTA finishes, it reboots the device and I can see both the display as well as the touch input.

If I follow these exact steps without turning off the ESP32, it works… As soon as I turn it off and turn it on again, the touch stops working.

Can someone let me know what am I doing wrong? Is it the pins I have chosen causing a problem?