0

Adafruit_TouchScreen not working with ESP32
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit_TouchScreen not working with ESP32

by Ckyiu on Tue Jun 01, 2021 9:56 pm

So I bought this 2.8in TFT (https://www.adafruit.com/product/1770) at Microcenter the other day and was having lots of fun with it and a generic ESP32 dev kit module. The display part worked great with my ESP32! But once I got to the touch screen part it stopped working. I wired up the touch connections to the ESP32 and touching did nothing! So I grabbed an Arduino Uno and the display and touch worked perfectly fine so the touch overlay wasn't faulty. I composed the following sketch to test the touch screen:
Code: Select all | TOGGLE FULL SIZE
#if defined(ESP32)
// These are the four touchscreen analog pins
#define YP A3  // must be an analog pin, use "An" notation!
#define XP 22  // can be any digital pin
#define YM 21  // can be any digital pin
#define XM A0  // must be an analog pin, use "An" notation!
#else  // Arduino Uno in my case
// These are the four touchscreen analog pins
#define YP A0  // must be an analog pin, use "An" notation!
#define XP A1   // can be any digital pin
#define YM A2   // can be any digital pin
#define XM A3  // must be an analog pin, use "An" notation!
#endif

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
#if defined(ESP32)
  analogReadResolution(10);
#endif
}

void loop() {
  // put your main code here, to run repeatedly:
  pinMode(XP, OUTPUT);
  pinMode(YP, INPUT);
  digitalWrite(XP, HIGH);
  Serial.print("X+ --> Y+: ");
  Serial.print(analogRead(YP));
  digitalWrite(XP, LOW);
  // -----------------------------------------
  pinMode(YM, OUTPUT);
  pinMode(XM, INPUT);
  digitalWrite(YM, HIGH);
  Serial.print("     Y- --> X-: ");
  Serial.println(analogRead(XM));
  digitalWrite(YM, LOW);
  delay(20);
}

Which resulted in similar results on both chips:
Code: Select all | TOGGLE FULL SIZE
No touching:
21:45:26.294 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:26.341 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:26.388 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:26.388 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:26.435 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:26.483 -> X+ --> Y+: 0     Y- --> X-: 0
Started at one corner and started "dragging" to the opposite corner:
21:45:26.530 -> X+ --> Y+: 39     Y- --> X-: 228
21:45:26.530 -> X+ --> Y+: 40     Y- --> X-: 240
21:45:26.577 -> X+ --> Y+: 44     Y- --> X-: 236
21:45:26.624 -> X+ --> Y+: 42     Y- --> X-: 224
21:45:26.671 -> X+ --> Y+: 57     Y- --> X-: 227
21:45:26.671 -> X+ --> Y+: 76     Y- --> X-: 222
21:45:26.718 -> X+ --> Y+: 95     Y- --> X-: 217
21:45:26.764 -> X+ --> Y+: 124     Y- --> X-: 202
21:45:26.810 -> X+ --> Y+: 155     Y- --> X-: 185
21:45:26.810 -> X+ --> Y+: 173     Y- --> X-: 177
21:45:26.857 -> X+ --> Y+: 204     Y- --> X-: 157
21:45:26.905 -> X+ --> Y+: 232     Y- --> X-: 140
21:45:26.950 -> X+ --> Y+: 246     Y- --> X-: 122
21:45:26.997 -> X+ --> Y+: 263     Y- --> X-: 109
21:45:26.997 -> X+ --> Y+: 284     Y- --> X-: 96
21:45:27.044 -> X+ --> Y+: 304     Y- --> X-: 84
21:45:27.089 -> X+ --> Y+: 303     Y- --> X-: 76
Released:
21:45:27.137 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.137 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.183 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.230 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.230 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.276 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.323 -> X+ --> Y+: 0     Y- --> X-: 0
21:45:27.323 -> X+ --> Y+: 0     Y- --> X-: 0


But when I use the Adafruit_Touchscreen library (https://github.com/adafruit/Adafruit_TouchScreen) the results get all wonky as such:
Code: Select all | TOGGLE FULL SIZE
/***************************************************
  This is our touchscreen painting example for the Adafruit ILI9341 Breakout
  ----> http://www.adafruit.com/products/1770

  Check out the links above for our tutorials and wiring diagrams
  These displays use SPI to communicate, 4 or 5 pins are required to
  interface (RST is optional)
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

/** NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT! **/

#include <Adafruit_GFX.h>    // Core graphics library
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include "TouchScreen.h"

// These are the four touchscreen analog pins
#define YP A3  // must be an analog pin, use "An" notation!
#define XP 22  // can be any digital pin
#define YM 21  // can be any digital pin
#define XM A0  // must be an analog pin, use "An" notation!

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// The display uses hardware SPI, plus #9 & #10
#define TFT_CS 5
#define TFT_DC 17
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_RST -1
#define TFT_MISO 19

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 125);

// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;

void setup(void) {
 // while (!Serial);     // used for leonardo debugging
 
  Serial.begin(9600);
  Serial.println(F("Touch Paint!"));
  analogReadResolution(10);
 
  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
 
  // make the color selection boxes
  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
 
  // select the current color 'red'
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  currentcolor = ILI9341_RED;
}


void loop()
{
  // Retrieve a point 
  TSPoint p = ts.getPoint();
 

  Serial.print("X: "); Serial.print(p.x);
  Serial.print("\tY: "); Serial.print(p.y);
  Serial.print("\tP: "); Serial.println(p.z); 

 
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
     return;
  }
 
  // Scale from ~0->1000 to tft.width using the calibration #'s
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());

 
//  Serial.print("("); Serial.print(p.x);
//  Serial.print(", "); Serial.print(p.y);
//  Serial.println(")");
 

   
  if (p.y < BOXSIZE) {
     oldcolor = currentcolor;

     if (p.x < BOXSIZE) {
       currentcolor = ILI9341_RED;
       tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     } else if (p.x < BOXSIZE*2) {
       currentcolor = ILI9341_YELLOW;
       tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     } else if (p.x < BOXSIZE*3) {
       currentcolor = ILI9341_GREEN;
       tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     } else if (p.x < BOXSIZE*4) {
       currentcolor = ILI9341_CYAN;
       tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     } else if (p.x < BOXSIZE*5) {
       currentcolor = ILI9341_BLUE;
       tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     } else if (p.x < BOXSIZE*6) {
       currentcolor = ILI9341_MAGENTA;
       tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
     }

     if (oldcolor != currentcolor) {
        if (oldcolor == ILI9341_RED)
          tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
        if (oldcolor == ILI9341_YELLOW)
          tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
        if (oldcolor == ILI9341_GREEN)
          tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
        if (oldcolor == ILI9341_CYAN)
          tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
        if (oldcolor == ILI9341_BLUE)
          tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
        if (oldcolor == ILI9341_MAGENTA)
          tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
     }
  }
  if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
    tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  }
}

Code: Select all | TOGGLE FULL SIZE
No touching:
21:43:00.494 -> X: 0   Y: 817   P: 0
21:43:00.494 -> X: 0   Y: 611   P: 0
21:43:00.494 -> X: 0   Y: 803   P: 0
21:43:00.540 -> X: 0   Y: 851   P: 0
21:43:00.540 -> X: 0   Y: 828   P: -1
21:43:00.586 -> X: 0   Y: 600   P: 0
21:43:00.586 -> X: 507   Y: 584   P: 0
21:43:00.586 -> X: 494   Y: 637   P: 0
21:43:00.634 -> X: 490   Y: 787   P: 0
21:43:00.634 -> X: 0   Y: 842   P: 0
21:43:00.681 -> X: 448   Y: 842   P: 0
21:43:00.681 -> X: 0   Y: 855   P: 0
Started touching:
21:43:00.730 -> X: 0   Y: 1023   P: 0
21:43:00.730 -> X: 0   Y: 1023   P: 0
21:43:00.730 -> X: 0   Y: 1023   P: 0
21:43:00.776 -> X: 0   Y: 1023   P: 0
21:43:00.776 -> X: 0   Y: 1023   P: 0
21:43:00.824 -> X: 0   Y: 1023   P: 0
21:43:00.824 -> X: 0   Y: 1023   P: 0
21:43:00.824 -> X: 0   Y: 1023   P: 0
21:43:00.870 -> X: 0   Y: 1023   P: 0
21:43:00.870 -> X: 0   Y: 1023   P: 0
21:43:00.918 -> X: 0   Y: 1023   P: 0
Released:
21:43:00.918 -> X: 495   Y: 697   P: 0
21:43:00.963 -> X: 312   Y: 847   P: 0
21:43:00.963 -> X: 0   Y: 859   P: 0
21:43:00.963 -> X: 283   Y: 587   P: 0
21:43:01.010 -> X: 499   Y: 605   P: 0
21:43:01.010 -> X: 488   Y: 795   P: 0
21:43:01.056 -> X: 0   Y: 835   P: 0
21:43:01.056 -> X: 475   Y: 802   P: 0
21:43:01.103 -> X: 0   Y: 831   P: -1
21:43:01.103 -> X: 436   Y: 608   P: 0
21:43:01.149 -> X: 480   Y: 611   P: 0
21:43:01.149 -> X: 0   Y: 611   P: 0

Is the Adafruit Touchscreen library not suited for the ESP32? Are there any modifications I can make to the library? I am able to locate and edit the library code on my machine if the need arises.

Ckyiu
 
Posts: 5
Joined: Wed Aug 07, 2019 3:54 pm

Re: Adafruit_TouchScreen not working with ESP32

by blnkjns on Fri Jun 04, 2021 12:32 am

Do you have the 2 analog inputs on real analog pins on the ESP?
Sometimes the define-ifs don't work correctly. You can try to force the code for the ESP by removing all clutter around it:
Code: Select all | TOGGLE FULL SIZE
#if defined(ESP32)
// These are the four touchscreen analog pins
#define YP A3  // must be an analog pin, use "An" notation!
#define XP 22  // can be any digital pin
#define YM 21  // can be any digital pin
#define XM A0  // must be an analog pin, use "An" notation!
#else  // Arduino Uno in my case
// These are the four touchscreen analog pins
#define YP A0  // must be an analog pin, use "An" notation!
#define XP A1   // can be any digital pin
#define YM A2   // can be any digital pin
#define XM A3  // must be an analog pin, use "An" notation!
#endif

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
#if defined(ESP32)
  analogReadResolution(10);
#endif
}
Becomes:
Code: Select all | TOGGLE FULL SIZE
#define YP A3  // must be an analog pin, use "An" notation!
#define XP 22  // can be any digital pin
#define YM 21  // can be any digital pin
#define XM A0  // must be an analog pin, use "An" notation!

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  analogReadResolution(10);
}

blnkjns
 
Posts: 663
Joined: Fri Oct 02, 2020 3:33 am

Re: Adafruit_TouchScreen not working with ESP32

by Ckyiu on Fri Jun 04, 2021 7:45 am

I believe they are on "real" analog pins. The result is still the same even if did what you suggested. (removing the #if defined(ESP32))

I've also tried writing my own code to get the touch points, and it's hard to get it remotely accurate with what I'm doing.

Ckyiu
 
Posts: 5
Joined: Wed Aug 07, 2019 3:54 pm

Re: Adafruit_TouchScreen not working with ESP32

by Ckyiu on Sat Jun 05, 2021 2:39 pm

So I was gonna try using CircuitPython and a Feather M0 basic with the touchscreen and I noticed this code example in the library readme: (https://github.com/adafruit/Adafruit_Ci ... ge-example)
Code: Select all | TOGGLE FULL SIZE
import board
import adafruit_touchscreen

# These pins are used as both analog and digital!
# XR, XL and YU must be analog and digital capable.
# YD just needs to be digital.
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
                      board.TOUCH_YD, board.TOUCH_YU)

while True:
    p = ts.touch_point
    if p:
    print(p)

So is this a typo or this is true? That 3 of the pins need to be analog and digital?

Ckyiu
 
Posts: 5
Joined: Wed Aug 07, 2019 3:54 pm

Re: Adafruit_TouchScreen not working with ESP32

by Ckyiu on Sun Jun 06, 2021 2:42 pm

Looking through this "guide" I found:
https://www.thesmarthomehookup.com/node ... d-outputs/
There are many pins on the ESP32 which have restrictions on them. So just keep switching around pins cause lots of them can't have pull ups/downs or can't be input/output. After changing the pins and changing the ADC resolution (via https://github.com/adafruit/Adafruit_To ... /issues/15) it worked!

Ckyiu
 
Posts: 5
Joined: Wed Aug 07, 2019 3:54 pm

Re: Adafruit_TouchScreen not working with ESP32

by blnkjns on Mon Jun 07, 2021 12:32 am

So which pins to use?

blnkjns
 
Posts: 663
Joined: Fri Oct 02, 2020 3:33 am

Re: Adafruit_TouchScreen not working with ESP32

by Ckyiu on Mon Jun 07, 2021 11:13 am

These pins worked for me:
Code: Select all | TOGGLE FULL SIZE
const byte YP = A4;
const byte XP = A5;
const byte YM = A18;
const byte XM = A19;

Ckyiu
 
Posts: 5
Joined: Wed Aug 07, 2019 3:54 pm

Please be positive and constructive with your questions and comments.