AZ Touch ESP example

I got AZ Touch up and running with Homeassistant and ESPHome.
You get a description, schematic an some sample applications here on zihatec.
With ESPHome i followed
ILI9341 TFT LCD
XPT2046 Touch Screen Controller
among other things.

esphome:
  name: touchmod01

esp32:
  board: esp-wrover-kit
  framework:
    type: arduino


The buzzer is connectet to GPIO21. Use it as rttl. Play a melody as described here.

output:
  # buzzer
  - platform: ledc
    pin: GPIO21
    id: rtttl_out
# component buzzer 
rtttl:
  output: rtttl_out
  on_finished_playback:
    - logger.log: 'Song ended!'
# to play from HA
api:
  services:
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'

Define fonts, images and colors

font:
  - file: '/config/esphome/font/slkscr.ttf'
    id: font1
    size: 8
image:
    # for this example the display is in portrait orientation.
    # It is dividet into a 12 squares grid 
    # that is why the image is resized to 80x80 
  - file: "/config/esphome/img/steckdose-color.png"
    id: steckdose
    resize: 80x80
    type: RGB24
color:
  - id: my_red
    red: 100%
    green: 0%

Setup your display
In this case it is a 2.4" touch-display. Connected over SPI.

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23
  miso_pin: GPIO19

Backlight is connected to GPIO15
Add to output section

output:
  # backlight
  - platform: ledc
    pin: 15
    id: gpio_15_backlight_pwm
    inverted: true

and add a light for HA user interface

light:
  # backlight for HA
  - platform: monochromatic
    output: gpio_15_backlight_pwm
    name: "ILI9341 Display Backlight"
    id: back_light
    restore_mode: ALWAYS_ON

Now, set up the display itself

display:
  - platform: ili9341
    model: TFT 2.4
    id: touch_display
    cs_pin: GPIO5
    dc_pin: GPIO4
    # if backlight is not controlled by pwm uncomment this 
    #led_pin:
    #  number: 15
    #  inverted: true
    reset_pin: GPIO22

    # play a little, 
    # draw colors, text, images
    # to show the state of an entity, change the color or write some text 
    # in this example the image ist swapped depend from my device 
    # extended_color_light_3 
    # it is imported later as binary sensor
    lambda: |-
      it.fill(COLOR_BLACK);
      // it.print(0, 0, id(font1), id(my_red), TextAlign::TOP_LEFT, "Hello World!");
      // it.fill(my_red);
      // it.image( 80,  80, id(steckdose));
      // it.image(160, 160, id(tablelamp), id(my_red), id(my_yellow));
      if (id(extended_color_light_3).state) {
        it.image( 80,  80, id(steckdose_on));
      } else {
        it.image( 80,  80, id(steckdose));
      }
      // 3 x 4 grid
      it.line( 80,   0,  80, 319);
      it.line(160,   0, 160, 319);
      it.line(  0,  80, 239,  80);
      it.line(  0, 160, 239, 160);
      it.line(  0, 240, 239, 240);

Set up the touch screen

:
  id: touchscreen
  cs_pin: 14
  irq_pin: 27
  update_interval: 50ms
  report_interval: 1s
  threshold: 400
  dimension_x: 240
  dimension_y: 320
  calibration_x_min: 3860
  calibration_x_max: 280
  calibration_y_min: 340
  calibration_y_max: 3860
  swap_x_y: false

Set the touch actions

# my screen is divided in to 12 squares 
# there are 12 touch areas named as touch_key0 to touch_key11
binary_sensor:
  - platform: xpt2046
    xpt2046_id: touchscreen
    id: touch_key0
    x_min: 0
    x_max: 79
    y_min: 0
    y_max: 79
    on_press:
    - logger.log: "Key0 was touched"

  - platform: xpt2046
    xpt2046_id: touchscreen
    id: touch_key4
    x_min: 80
    x_max: 159
    y_min: 80
    y_max: 159
    # some light should be toggled
    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key4
        then: 
        - homeassistant.service:
            service: light.toggle
            data: 
              entity_id: light.extended_color_light_3
 
  - platform: xpt2046
    xpt2046_id: touchscreen
    id: touch_key11
    x_min: 160
    x_max: 239
    y_min: 240
    y_max: 319
    on_state:
      - lambda: 'ESP_LOGI("main", "key11: %s", (x ? "touch" : "release"));'

To show the state of an entity, additionaly it has to be imported as binary sensor…

  - platform: homeassistant
    id: extended_color_light_3
    entity_id: light.extended_color_light_3
    internal: true

It works so far.

7 Likes

Thanks for the share, great design and setup :+1:

Nice, I just set up two of these myself a few weeks ago. Wish i seen this post earlier, would have saved me much trial and errors :grinning_face_with_smiling_eyes:

I did not get the buzzer working though…
The buzzer had fallen off because of bad soldering, so i resoldered it myself. Could be just a bad connection…
But i also added a a BME280 to the board, and used the PIN 21 as part of the I2C bus.

Is there perhaps something in the code that needs changing when its on that bus?

Been working on this also.

Have the touch areas mapped on a 2.8 inch screen.

1 Like

Hi, DarkWader,
on this page https://www.hwhardsoft.de you will find Schaltplan AZ-Touch MOD V01-03-01 There you see GPIO21 is named “BEEPER” and is hard wired to the transistor and buzzer.
At the PCB there are two areas with solder pads. Maybe you should try one of these.
It is:

  • A0, GPIO35, GPIO34, GPIO39
  • GPIO32, GPIO25, GPIO26, GPIO33

Good Luck!

This helped me, thanks a lot!

1 Like

Hi,

Anyone have issues with color banding when using images? On my display it looks really bad and if its not a faulty part i guess this display is usable for line and fills only.

Hi Alexandru,
I have tried bmp and very small png and glyphicons. That worked. I was able to turn and position. Looked good.
Will try some jpg.

Hi Christian, i also tried jpeg, png and bmp with the same results. I attached some examples. Also the viewing angle is very narrow, at 45 degrees you cant see anything.

My yaml is minimal:


spi:
 clk_pin: 18
 mosi_pin: 23
 miso_pin: 19


image:
  - file: "images/portrait.png"
    id: my_image
    type: RGB24
    
display:
  - platform: ili9341
    model: TFT 2.4
    rotation: 270
    cs_pin: 5
    dc_pin: 4
    reset_pin: 22

    lambda: |-
      it.image(0, 0, id(my_image));
1 Like

You are right. Some images are looking good, especially graphics. Some, with alot of colours, or brightness range not. View angle depends also on direction.
Means, for home automation touch panel it is ok. As digital picture frame it is not first choice.
Regards,
Chris

Hi guys,

I’m really struggeling with the touch. (ILI 9341 2.8 + ESP32-WROOM)
Using IRQ-Pin, I see strange touches in the logs, but no reaction to a “real” touch.

[11:25:51][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:52][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:53][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:54][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:55][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:56][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:57][D][xpt2046:099]: Raw [x, y] = [0, 0], transformed = [0, 0]
[11:25:58][D][xpt2046:099]: Raw [x, y] = [8191, 8191], transformed = [240, 320]
[11:25:58][D][binary_sensor:036]: ‘touch_key0’: Sending state OFF
[11:25:59][D][xpt2046:099]: Raw [x, y] = [8191, 8191], transformed = [240, 320]
[11:26:00][D][xpt2046:099]: Raw [x, y] = [8191, 8191], transformed = [240, 320]
[11:26:01][D][xpt2046:099]: Raw [x, y] = [8191, 8191], transformed = [240, 320]
[11:26:02][D][xpt2046:099]: Raw [x, y] = [8191, 8191], transformed = [240, 320]

If I don’t use IRQ Pin, I get no log entry at all.

T_CLK, T_DN, and T_DO are shared with the TFT Pins. CS and IRQ are on GPIO17 and GPIO16. I tried other pins too)

xpt2046:
  id: touchscreen
  cs_pin: GPIO17
  irq_pin: GPIO16
  update_interval: 50ms
  report_interval: 1s
  threshold: 800
  dimension_x: 240
  dimension_y: 320
#  calibration_x_min: 3860
#  calibration_x_max: 280
#  calibration_y_min: 340
#  calibration_y_max: 3860
#  swap_x_y: true
  on_state:
    - lambda: |-
        ESP_LOGI("main", "args x=%d, y=%d, touched=%s", x, y, (touched ? "touch" : "release"));
        ESP_LOGI("main", "member x=%d, y=%d, touched=%d, x_raw=%d, y_raw=%d, z_raw=%d",
            id(touchscreen).x,
            id(touchscreen).y,
            (int) id(touchscreen).touched,
            id(touchscreen).x_raw,
            id(touchscreen).y_raw,
            id(touchscreen).z_raw
            );

Have you ever had a simmilar issue? Is the IRQ-Pin needed?
Any idea, what I can try next?

Thank you!

Hi Axel, inside that AZtouchmod, the wiring is fix determined. No choice.

xpt2046:
  id: touchscreen
  cs_pin: 14
  irq_pin: 27
  update_interval: 50ms
  report_interval: 1s
  threshold: 400
  dimension_x: 240
  dimension_y: 320
  calibration_x_min: 3860
  calibration_x_max: 280
  calibration_y_min: 340
  calibration_y_max: 3860
  swap_x_y: false

My touch area ist divided in to 12 sqares. I call them ‘key0’ to ‘key11’.
Inside the logs it looks this way:

[xpt2046:029]: Falling penirq edge, forcing update
[xpt2046:099]: Raw [x, y] = [1938, 532], transformed = [128, 17]
[binary_sensor:036]: 'touch_key1': Sending state ON
[main:215]: key1: touch
[xpt2046:112]: Released [128, 17]
[binary_sensor:036]: 'touch_key1': Sending state OFF
[main:215]: key1: release

[xpt2046:029]: Falling penirq edge, forcing update
[xpt2046:099]: Raw [x, y] = [2057, 1447], transformed = [120, 100]
[binary_sensor:036]: 'touch_key4': Sending state ON
[homeassistant.binary_sensor:026]: 'light.extended_color_light_3': Got state ON
[binary_sensor:036]: 'extended_color_light_3': Sending state ON
[xpt2046:112]: Released [120, 100]
[binary_sensor:036]: 'touch_key4': Sending state OFF

[xpt2046:029]: Falling penirq edge, forcing update
[xpt2046:099]: Raw [x, y] = [857, 3159], transformed = [201, 256]
[binary_sensor:036]: 'touch_key11': Sending state ON
[xpt2046:112]: Released [201, 256]
[binary_sensor:036]: 'touch_key11': Sending state OFF

Hope, it is useful somehow.

1 Like

Thank you very much, Christian. 14 and 27 are the pins of the ESP32, correct? Or am I missing something here?


It means GPIO14 and GPIO27
On schematic it is called T_IRQ and T_CS to tell it apart from Display CS on GPIO5

1 Like

Just found out, that my display was missing touch. I received the wrong version. No it works like a charm. :partying_face:

I had the same thing here with a touch display for the Raspberry. But there was not “touch”. Either its hard to differentiate for the shop to. Or it is a trick.

1 Like

Thank you very much for your inspiration. :wink:

4 Likes

hello,

nice work, can you share the esphome code please?

Thanks

Thanks.

Awesome Spotify Touch Control via ILI9341-Screen

Would you mind sharing your complete code? I do also have a 2.8 inch screen and I am just wondering how this works in the “display” section of esphome since on the homepage there is only considered the 2.4 inch screen.