Arduino Portenta H7 touchscreen library
(c) 2021 Lennart Popkes.
Distributed under the MIT License.
Introduction
This is a very basic library for interfacing a 4-wire resistive touchscreen with the Arduino Portenta H7.
Due to some pin restrictions from the STM32H7 and a few Arduino API bugs, it is currently (Mar 2021) not possible to use a standard touchscreen driver like the Adafruit TouchScreen Library. You can read about the issue at the bottom of the document.
This solution uses the mbed HAL layer and does not suffer from the software problems listed below.
How to use
- Connect your 4-wire touchscreen to 4 pins of the arduino.
- The
X-
andY+
-wires need to be connected to analog I/O pins. The breakout pinA6
however is the only usable non-high-density analog pin on the portenta. For the second analog pin, @hpssjellis usedA7
on the high density connectors to get the example working. As I do not have the portenta breakout board as of now, I could not test this so for. - The
Y-
andX+
-wires can be connected to any I/O pin
- The
- Caution! Do not use any of A0-A3 pins, since these are analog input only! (See below)
- In your code: include
PortentaTouchScreen.h
- Instantiate a
PortentaTouchScreen
passing all 4 pins to the constructor. The library assumes a default resistance of 360 ohms across the x-Plate, which is around what most touchscreen will probably have. If needed, it can be passed as the 5th parameter. - When neccessary:
- Get the current touch point using
getPoint
. It contains the x- and y-coordinates and the pressure z. You can use the Arduino-methodmap
to scale the values to your screen size (like in the basicTouchScreen example). You might be able to usereadTouchX
,readTouchY
andpressure
, but they can be a little buggy and might return unwanted results. - A touch is sensed if the value returned is greater than
pressureThreshold
. This might have to be adjusted. - The values from
getPoint
have 11 bit accuracy (0 to 2047).
- Get the current touch point using
LVGL
PortentaTouchScreen andThe touchscreen can be used as an input device for LVGL. Write your own input device callback using the input type of LV_INDEV_TYPE_POINTER
.
The issues with the Portenta H7
- Pins A0-A3 are analogue input only (see chapter
Pin Descriptions
in the STM32H747xi datasheet, pinsPA0_C
,PA1_C
,PC2_C
,PC3_C
) - Once using
pinMode()
on any of the other analogue pins (A4-A6), noanalogRead()
will be possible anymore or at least return no sensible value - Pin A5 (
PC3
) is connected to Pin D8, which is also the defaultSPI1 MOSI
. This means that as long as you have any SPI devices (like an ILI9341 TFT/LCD) connected, pin A5 is basically useless - Pin A4 (
PC2
) is connected to Pin D10, which is also the defaultSPI MISO
. This means that as long as you have any SPI devices (like an ILI9341 TFT/LCD) connected, pin A4 is basically useless. - With Pins A0-A5 being practically useless for this scenario, it is essentially impossible to connect a TFT/LCD with a resistive 4-wire touchscreen to the portenta without some hacking.