HX8357D and Teensy 3.2 touchscreen not responding

Status
Not open for further replies.

WWMD

Member
Hi all,
I am new to the whole arduino programming thing. I am trying to use an Adafruit 3.5" resistive touch screen (SPI mode) with my new Teensy 3.2. I have gotten the graphics portion to work with the default breakouttouchpaint, but the touchscreen doesn't respond at all. I looked through a few of the other posts on here for answers, but I am still unable to get the touchscreen to work. I have tested the screen on my Mega2560 and everything works as expected, including the touchscreen. Any help is much appreciated.

My wiring is as follows:
Screen Teensy 3.2
GND GND (from USB)
Vin +5v (from USB)
CLK 13
MISO 12
MOSI 11
CS 25 (I need the hardware serial ports so I reassigned this pin)
D/C 26
RST 24
Lite N/C
Y+ A12
X+ 32
Y- 33
X- A13

The IM2 jumper solder pad jumper is soldered closed to enable SPI

Here is the complete source:
/***************************************************
This is our touchscreen painting example for the Adafruit HX8357 Breakout
----> http://www.adafruit.com/products/2050

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 3.5" BREAKOUT! **/

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

// These are the four touchscreen analog pins
#define YP A12 // must be an analog pin, use "An" notation!
#define XM A13 // must be an analog pin, use "An" notation!
#define YM 33 // can be a digital pin
#define XP 32 // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 110
#define TS_MINY 80
#define TS_MAXX 900
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// The display uses hardware SPI, plus #9 & #10
#define TFT_RST 24 // dont use a reset pin, tie to arduino RST if you like
#define TFT_DC 26
#define TFT_CS 25

Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

// 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, 200);

// 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(115200);
Serial.println(F("Touch Paint!"));

tft.begin(HX8357D);
tft.fillScreen(HX8357_BLACK);

// make the color selection boxes
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, HX8357_RED);
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, HX8357_YELLOW);
tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, HX8357_GREEN);
tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, HX8357_CYAN);
tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, HX8357_BLUE);
tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, HX8357_MAGENTA);
tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, HX8357_BLACK);
tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);

// select the current color 'red'
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
currentcolor = HX8357_RED;
}


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

// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
return;
}

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

// 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 = HX8357_RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*2) {
currentcolor = HX8357_YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*3) {
currentcolor = HX8357_GREEN;
tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*4) {
currentcolor = HX8357_CYAN;
tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*5) {
currentcolor = HX8357_BLUE;
tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*6) {
currentcolor = HX8357_MAGENTA;
tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
} else if (p.x < BOXSIZE*7) {
currentcolor = HX8357_WHITE;
tft.drawRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, HX8357_RED);
} else if (p.x < BOXSIZE*8) {
currentcolor = HX8357_BLACK;
tft.drawRect(BOXSIZE*7, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
}

if (oldcolor != currentcolor) {
if (oldcolor == HX8357_RED)
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, HX8357_RED);
if (oldcolor == HX8357_YELLOW)
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, HX8357_YELLOW);
if (oldcolor == HX8357_GREEN)
tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, HX8357_GREEN);
if (oldcolor == HX8357_CYAN)
tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, HX8357_CYAN);
if (oldcolor == HX8357_BLUE)
tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, HX8357_BLUE);
if (oldcolor == HX8357_MAGENTA)
tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, HX8357_MAGENTA);
if (oldcolor == HX8357_WHITE)
tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, HX8357_WHITE);
if (oldcolor == HX8357_BLACK)
tft.fillRect(BOXSIZE*7, 0, BOXSIZE, BOXSIZE, HX8357_BLACK);
}
}
if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}
}
 
Last edited:
I have been doing a little more testing and I found that as soon as TSPoint p = ts.getPoint(); is called, that is where the program stops executing, I cannot write anything to Serial if this line is not commented out.
 
Hmmm, looks like we might have a problem with Adafruit's touchscreen lib.

Can you check a couple quick things for me?

Use File > Preferences to turn on verbose info while compiling. Then click Verify. Near the end is a summary of which libraries it actually used with the full pathnames.

Please also verify which version you're running. Click Help > About, or Arduino > About if using a Mac. Which version of Teensyduino is this?

These will really help to me to make sure I'm looking at exactly the same code you're really using. This detail is important because Adafruit sometimes changes things and we can get out of sync.
 
Hmmm, looks like we might have a problem with Adafruit's touchscreen lib.

Can you check a couple quick things for me?

Use File > Preferences to turn on verbose info while compiling. Then click Verify. Near the end is a summary of which libraries it actually used with the full pathnames.

Please also verify which version you're running. Click Help > About, or Arduino > About if using a Mac. Which version of Teensyduino is this?

These will really help to me to make sure I'm looking at exactly the same code you're really using. This detail is important because Adafruit sometimes changes things and we can get out of sync.

Thanks for the quick reply!

I am using Arduino IDE 1.8.1
Teensy Loader 1.35
Teensy 3.2 board
I am running the board at 24MHz

After the libraries updated, I can upload the sketch, but the display is pure white. Graphicstest still works when uploaded, however colors are different on the test text at different clock speeds.

Correction: I have to reload the serial monitor a few times for the breakouttouchpaint to display on screen.

I updated the libraries because the Arduino IDE told me there were new files available, this is the new output using verbose:
In file included from C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:14:0:

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.h:51:12: error: 'RwReg' does not name a type

volatile RwReg *xp_port, *yp_port, *xm_port, *ym_port;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.h:52:3: error: 'RwReg' does not name a type

RwReg xp_pin, xm_pin, yp_pin, ym_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp: In member function 'TSPoint TouchScreen::getPoint()':

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:69:4: error: 'xp_port' was not declared in this scope

*xp_port |= xp_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:69:15: error: 'xp_pin' was not declared in this scope

*xp_port |= xp_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:70:4: error: 'xm_port' was not declared in this scope

*xm_port &= ~xm_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:70:16: error: 'xm_pin' was not declared in this scope

*xm_port &= ~xm_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:105:5: error: 'ym_port' was not declared in this scope

*ym_port &= ~ym_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:105:17: error: 'ym_pin' was not declared in this scope

*ym_port &= ~ym_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:106:5: error: 'yp_port' was not declared in this scope

*yp_port |= yp_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:106:16: error: 'yp_pin' was not declared in this scope

*yp_port |= yp_pin;

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp: In constructor 'TouchScreen::TouchScreen(uint8_t, uint8_t, uint8_t, uint8_t, uint16_t)':

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:184:3: error: 'xp_port' was not declared in this scope

xp_port = portOutputRegister(digitalPinToPort(_xp));

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:185:3: error: 'yp_port' was not declared in this scope

yp_port = portOutputRegister(digitalPinToPort(_yp));

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:186:3: error: 'xm_port' was not declared in this scope

xm_port = portOutputRegister(digitalPinToPort(_xm));

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:187:3: error: 'ym_port' was not declared in this scope

ym_port = portOutputRegister(digitalPinToPort(_ym));

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:189:3: error: 'xp_pin' was not declared in this scope

xp_pin = digitalPinToBitMask(_xp);

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:190:3: error: 'yp_pin' was not declared in this scope

yp_pin = digitalPinToBitMask(_yp);

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:191:3: error: 'xm_pin' was not declared in this scope

xm_pin = digitalPinToBitMask(_xm);

^

C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master\TouchScreen.cpp:192:3: error: 'ym_pin' was not declared in this scope

ym_pin = digitalPinToBitMask(_ym);

^

Multiple libraries were found for "Adafruit_GFX.h"
Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Adafruit_GFX
Not used: C:\Users\USER\Documents\Arduino\libraries\Adafruit-GFX-Library-master
Using library Adafruit_GFX at version 1.1.5 in folder: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Adafruit_GFX
Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\SPI
Using library Adafruit_HX8357_Library-master at version 1.0.2 in folder: C:\Users\USER\Documents\Arduino\libraries\Adafruit_HX8357_Library-master
Using library Touch-Screen-Library-master in folder: C:\Users\USER\Documents\Arduino\libraries\Touch-Screen-Library-master (legacy)
Error compiling for board Teensy 3.2 / 3.1.
 
Last edited:
Hi Paul,
I was able to make the touchscreen work finally. I had to use my preferred Linux environment to do so. I am using Arduino 1.6.12 under Linux with TeensyDuino 1.33 Do you have a link to download TeensyDuino 1.33 for Windows so I can try this on my Windows machine, it seems that it did the trick.

Thank You
 
Paul,

I just tested the libraries you provided above
https://github.com/adafruit/Adafruit_HX8357_Library
https://github.com/adafruit/Touch-Screen-Library

and I CAN get them working with a 3.5" TFT display HX8357D chip.

I can run in hardwareSPI or softwareSPI (hardware is noticeably faster).

Initially the libraries would not work and i had a bunch of compile errors (using my original program that compiled on different 8357 and 9341 libs), But I copied these libraries to my Arduino\Libraries folder as well. I recompiled with no errors and display and touch screen work. Here's an image of what I'm displaying (small buttons at top left and right kick of some other screens (which work).

20170901_221903.jpg
 
This seems to have been recently broken. The latest code that adds sendCommand to the Adafruit_HX8357_library will not compile against Teensy 3.6. It does compile against an UNO.
 
Code:
C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp: In member function 'virtual void Adafruit_HX8357::begin(uint32_t)':

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp:273:24: error: 'sendCommand' was not declared in this scope

         sendCommand(cmd);

                        ^

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp:275:39: error: 'sendCommand' was not declared in this scope

         sendCommand(cmd, addr, numArgs);

                                       ^

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp: In member function 'virtual void Adafruit_HX8357::setRotation(uint8_t)':

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp:321:35: error: 'sendCommand' was not declared in this scope

   sendCommand(HX8357_MADCTL, &m, 1);

                                   ^

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp: In member function 'virtual void Adafruit_HX8357::invertDisplay(boolean)':

C:\Users\Ken\Documents\Arduino\libraries\Adafruit_HX8357_Library\Adafruit_HX8357.cpp:331:52: error: 'sendCommand' was not declared in this scope

   sendCommand(invert ? HX8357_INVON : HX8357_INVOFF);

                                                    ^

Multiple libraries were found for "Adafruit_GFX.h"
 Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Adafruit_GFX
 Not used: C:\Users\Ken\Documents\Arduino\libraries\Adafruit_GFX_Library
Error compiling for board Teensy 3.6.
 
Isn't sendCommand() something which triggers SPI or I2C communication under the hood? Did you include the required SPI or Wire library?
 
I also tried getting the copy of Adafruit_HX8357.cpp from the March 21, 2019 commit just before the sendCommand was implemented and replacing that. The graphicstest code compiled and I could see the code running through its paces in the serial monitor, but nothing displayed on the tft display.

I am pretty sure I am wired correct because my application that I am working on was running on a 1.8 inch tft prior to upgrading to the 3.5 inch tft. When I run that code using the Adafruit_GFX and Adafruit_ST7735 libraries, the display shows the 1.8 inch output in the upper right corner, it is just a reflected image of the output and very small.
 
Is there a later version of Adafruit_GFX for Teensy? I see my code is using the Teensy version.
Code:
Multiple libraries were found for "Adafruit_GFX.h"
 Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Adafruit_GFX
 Not used: C:\Users\Ken\Documents\Arduino\libraries\Adafruit_GFX_Library
 
I was able to get the graphics test to work by downloading the March 29, 2019 version of Adafruit_HX8357.cpp and manually copying the library. Then using the software SPI. The hardware SPI does not seem to work, but maybe I need to use specific pins for that, not sure. The pins I am using are

Code:
// These are 'flexible' lines that can be changed
#define SCK 36  
#define MOSI 35  
#define CS   37   
#define DC   34   
#define RST  33   
#define SDCS 38
#define TFT_CS 37
#define TFT_DC 34
#define TFT_RST 33 // RST can be set to -1 if you tie it to Arduino's reset

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

// SoftSPI - note that on some processors this might be *faster* than hardware SPI!
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO);
 
Try hardware SPI on the T3.6 with the default pins first, after getting this working, you might switch over to alternate pins. Software SPI (bit-banging stuff with GPIO) is highly inefficient on quick MCUs like the T3.6. MOSI 11, MISO 12, SCK13. Use whatever digital pin you want for the different CS, DC, and RST signals.

After setting everything up, get the display working, first. Use a logic analyzer to make sure that the assertion of DC, CS, and RST works correctly. Start with lower SPI speeds and increase step by step. The T3.6 can theoretically do up to 45MHz SPI, but the TFT won't follow.
Then, add the touch stuff. Check again with the logic analyzer. Do things systematically, step by step.
 
Try hardware SPI on the T3.6 with the default pins first, after getting this working, you might switch over to alternate pins. Software SPI (bit-banging stuff with GPIO) is highly inefficient on quick MCUs like the T3.6. MOSI 11, MISO 12, SCK13. Use whatever digital pin you want for the different CS, DC, and RST signals.

After setting everything up, get the display working, first. Use a logic analyzer to make sure that the assertion of DC, CS, and RST works correctly. Start with lower SPI speeds and increase step by step. The T3.6 can theoretically do up to 45MHz SPI, but the TFT won't follow.
Then, add the touch stuff. Check again with the logic analyzer. Do things systematically, step by step.

Theremingenieur,

Thanks a million for your help. You are correct that the software SPI is slow. I got it working on hardware SPI using the pins you suggested, but I believe it is interfering with the SPI for the audio card. I should be able to use MOSI 7, MISO 12, SCK 14 to match the sound card, but I am not sure how I tell the display to use those pins for hardware SPI. Could you point me in the right direction.

Thanks again.
 
Ah, new information... you have also an audio board connected...

Since the Adafruit library allows using an arbitrary SPI class in the constructor, you might first do the needed SPI settings, an then call the constructor with that class:
Code:
#include <SPI.h>
#define TFT_CS some_pin
#define TFT_DC other_pin
#define TFT_RST again_another_pin
SPI.setMOSI(7);
SPI.setMISO(12);
SPI.setSCK(14);
// Now, call the constructor with this interface:
Adafruit_HX8357 tft = Adafruit_HX8357(SPI,TFT_CS, TFT_DC, TFT_RST);

BTW: All the needed information is in the comments of the Adafruit_HX8357.cpp source code on gitHub. No black magic, no rocket science...
 
Status
Not open for further replies.
Back
Top