Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Cant get touch working on ILI9341 display with Teensy 4.1

Salchicha

New Member
I have my ILI9341 display working ok display wise but I cant get the touch functionality working, I've tried a few of the example sketches but none work.

Here is one of the examples I've modified (touchpaint_xpt2046), I've triple checked all the physical connections match up so as far as I can tell it should work.

I'm using Teensy 4.1, Arduino IDE


C++:
// uncomment to try Touch on SPI1 pins
#define TOUCH_SPI1
// FlexSPI on SPI1 pins
#define TOUCH_FLEXSPI


//****************************************************************************
// Includes
//****************************************************************************
#include <XPT2046_Touchscreen.h>
#include <ILI9341_t3n.h>

#ifdef TOUCH_FLEXSPI
#include <FlexIOSPI.h>
#include <FlexIO_t4.h>
#endif


//****************************************************************************
// This is calibration data for the raw touch data to the screen coordinates
//****************************************************************************
// Warning, These are
#define TS_MINX 337
#define TS_MINY 529
#define TS_MAXX 3729
#define TS_MAXY 3711

//****************************************************************************
// Settings and objects
//****************************************************************************
#if defined(KURTS_FLEXI)
#define TFT_DC 22
#define TFT_CS 15
#define TFT_RST -1
#define TFT_SCK 14
#define TFT_MISO 12
#define TFT_MOSI 7
#define DEBUG_PIN 13
#define TOUCH_CS  8
#else
// *************** Change to your Pin numbers ***************
#define TFT_DC  9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
#define TOUCH_CS  8
#endif

// If using SPI1 you can optionally setup to use other MISO pin on T4.1

#ifdef TOUCH_SPI1
#define TOUCH_MOSI 11
#define TOUCH_MISO 12
#define TOUCH_SCK  13
#undef TOUCH_CS
#define TOUCH_CS  8
#endif

// Setup flexSPI on same pins as SPI1
#ifdef TOUCH_FLEXSPI
#define TOUCH_MOSI 11
#define TOUCH_MISO 12
#define TOUCH_SCK  13
#undef TOUCH_CS
#define TOUCH_CS  8
FlexIOSPI flexspi(TOUCH_MOSI, TOUCH_MISO, TOUCH_SCK);
#endif

XPT2046_Touchscreen ts(TOUCH_CS);
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);

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

//****************************************************************************
// Setup
//****************************************************************************
void setup(void) {
while (!Serial && (millis() <= 1000));

Serial.begin(9600);
Serial.println(F("Touch Paint!"));

tft.begin();

#if defined (TOUCH_SPI1)
Serial.println("Using SPI1 for Touch\n");
// Can setup to use SPI1
#ifdef TOUCH_MISO
SPI1.setMISO(TOUCH_MISO);
#endif
if (!ts.begin(SPI1)) {
#elif defined(TOUCH_FLEXSPI)
Serial.println("Using flexSPI for Touch\n");
if (!ts.begin(flexspi)) {
#else
// Or by default use SPI
if (!ts.begin()) {
#endif
Serial.println("Couldn't start touchscreen controller");
while (1);
}
Serial.println("Touchscreen started");

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()
{
// See if there's any  touch data for us
if (ts.bufferEmpty()) {
return;
}

// You can also wait for a touch
/*
if (! ts.touched()) {
return;
}
*/



// Retrieve a point
TS_Point p = ts.getPoint();

// p is in ILI9341_t3 setOrientation 1 settings. so we need to map x and y differently.

// Serial.println("X = "); Serial.print(p.x);
// Serial.println("\tY = "); Serial.print(p.y);
// Serial.println("\tPressure = "); Serial.print(p.z);


// Scale from ~0->4000 to tft.width using the calibration #'s
#ifdef SCREEN_ORIENTATION_1
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
#else

uint16_t px = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
p.y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
p.x = px;
#endif
/*
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);
}
}
 
Note the first line says,
Code:
// uncomment to try Touch on SPI1 pins
That sounds like the code is experimental or not robust for all versions of the display.
 
We use the xpt2046 library with that display on both the Arduino and ESP32 boards and they work ok.
Are you sure you have wired it correctly ?

Have only used the Adafruit libraries as below .

Have you tried running the Touch Examples from the xpt2046 library ?


Code:
#include "SPI.h"
#include <Adafruit_GFX.h>
#include "Adafruit_ILI9341.h"
#include "XPT2046_Touchscreen.h"
/CODE]
 
Ive got it working now using the original pin numbers in the example sketch, which are different to the ones listed on the PJRC page, which says to use the same MOSI, MISO and SDK pins for the display and touch screen, now that I'm seperate pins for everyhing its fine.

C++:
#define TFT_DC  9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
#define TOUCH_CS  6
#endif

// If using SPI1 you can optionally setup to use other MISO pin on T4.1

#ifdef TOUCH_SPI1
#define TOUCH_MOSI 26
#define TOUCH_MISO 39
#define TOUCH_SCK  27
#undef TOUCH_CS
#define TOUCH_CS  38
#endif

// Setup flexSPI on same pins as SPI1
#ifdef TOUCH_FLEXSPI
#define TOUCH_MOSI 26
#define TOUCH_MISO 39
#define TOUCH_SCK  27
#undef TOUCH_CS
#define TOUCH_CS  38
FlexIOSPI  flexspi(TOUCH_MOSI, TOUCH_MISO, TOUCH_SCK);
#endif
 
Good you have got it working, but wonder why you cannot use common lines as in the diagram below which we use ok.


Do not have the Teensy, but wonder if using the standard libraries we mentioned earlier would work for you ?
 

Attachments

  • 001615.jpg
    001615.jpg
    75.1 KB · Views: 79
I tried a few other examples that didn't work either, looking at that diagram I could have been wiring it up wrong, I was using 2 separate wires for the shared connections rather than joining the pins on the display together as in the diagram.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top