This is a 4-wire resistive touch screen library for ESP32 based on Adafruit's Arduino implementation.
Added modification to allow the 4-wire resistive touchscreen of LCDs with parallel data interfaces to be used with ESP32 UNO style boards whilst WiFi is enabled.
As the ADC in the ESP32 has some problems for accuracy the library also includes a function for error correction and mapping. It can be configured to use polinomial aproximation or linear interpolation between known points.
- All 4 pins for the resistive touchscreens must be able to work as digital outputs
- If WiFi is enabled ADC1 cannot be used
- ADC2 maps to pins from 32 to 39
- Pins from 34 to 39 are input-only
The panel is driven by regular digital pins but is then switched to an analog pin (in the range 32-39) to perform the reading. However this requires additional wiring. Any pin from 32 to 39 may be used for analog read but pins from 34 to 39 are recomended. Pins 34 to 39 are input only so they always present a high impedance, thus avoiding the risk of two outputs shorting.
Connect the touch panel as follows
- XP -> Any free pin from 2 to 33 (both included).
- XM -> Any free pin from 2 to 33 (both included).
- YP -> Any free pin from 2 to 33 (both included).
- YM -> Any free pin from 2 to 33 (both included).
Additionally make the following connections
- XM -> Any free pin from 32 to 39 (pins from 34 to 39 recomended)
- YP -> Any free pin from 32 to 39 (pins from 34 to 39 recomended)
Example
If using Arduino UNO style board with TFT_eSPI, make the following connections:
Download and install the library using your IDE, eg Arduino or PlatformIO. If using Arduino IDE navigate to your library folder, open TouchSetup.h and set your preferences. Defaults should work for most common configurations.
If using PlatformIO we recomment using "-build_flags" in your platformio.ini file.
Arduino Basic setup
- Find the file called
TouchSetup.h
in your library folder. - Open the file in any text editor and locate the following lines:
- #define aXM xx
- #define aYP yy
- Change the values "xx" and "yy" to the analog pins connected to XM and YP(must be between 32 and 39).
Optional
5. Select mapping method: Polynomial or Grid. Comment the #define POLYNOMIAL
line for Grid method.
6. Run the calibration example to get the calibration values
7. Paste the output values into TouchSetup.h
Platform IO Basic Setup
- Open platform.ini file
- Add the following lines
-D TOUCH_SETUP_LOADED=1
-D aYP=xx
-D aXM=yy
- Change the values "xx" and "yy" to the analog pins connected to XM and YP(must be between 32 and 39). Optional
- Select mapping method: Polynomial or Grid. Default mode is Grid. Add
-D POLYNOMIAL=1
to enable polynomial method. - Run the calibration example to get the calibration values
- Paste the output values into
TouchSetup.h
Check the provided examples for information about how to use the library.
Compatible with both TFT_eSPI and MCUFRIEND_kbv libraries
Touchscreen needs to be calibrated before use! We recomment using included touch calibration sketch