Trouble talking to Adafruit 3.5" TFT touchscreen (HX8357)

I am trying to get x and y position readings from this Adafruit 3.5" Display. In its little Adafruit video, it says it is easy to use with a ESP32. I have not found it easy and can only get this code to work on a Mega.

What I would like to do is get it to work with an ESP32 ideally, but my latest attempts have been with a Huzzah32 Feather because I found this pinout diagram that's attached. But any guidance on how to setup the X+, X-. Y+, Y- would be awesome. I am clearly not using the right pins and it seems like Y+ and X- placement are most important. The have to be analog I am told... any ideas on a pinout version for an ESP32 or this Feather would be much appreciated.

// Touch screen library with X and Y readings
// This modified code returns X and Y coordinates only when the screen is touched, public domain
// #include <Adafruit_HX8357.h>
#include <stdint.h>
#include "TouchScreen.h"

#define YP 2  // must be an analog pin, use "An" notation!
#define XM 4  // must be an analog pin, use "An" notation!
#define YM 16 // can be a digital pin
#define XP 17 // can be a digital pin

// Initialize the touch screen
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

void setup(void) {
  Serial.begin(115200);
}

void loop(void) {
  // A point object holds x, y, and z (pressure) coordinates
  TSPoint p = ts.getPoint();
  
  // We have some minimum pressure we consider 'valid'
  // Pressure of 0 means no pressing, so we'll use a small value as a threshold
  if (p.z > 50) {
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.println(p.y);
  }

  delay(100);
}

What I would like to do is get it to work with an ESP32

The Huzzah32 Feather is a "standard" ESP32 Wroom in Adafruit's Feather layout. Are you looking for something else?

This comment suggests that you can use any of the available analog pins, and the diagram you posted shows several, even if you are using WiFi (A03, A04, A07, A08). What have you tried?

#define YP 2  // must be an analog pin, use "An" notation!
#define XM 4  // must be an analog pin, use "An" notation!

I avoided all pins that mention "can't read with wifi on" because I need to use Wifi. Does that not mean that, if connecting via WiFi to my mac within "Audio MIDI Setup" app that I can't use those pins? It seems absurd. It leaves no pins to use?!?

The pin diagram is clear. Any pins that are marked "can't read with WIFI on" are NOT available to use, if WIFI is also being used.

That leaves some other pins that you can use. Did you not read post #2?

I did. I was confused because nothing worked when trying any of the Analog pins. Usually I get some sign of life like one direction will give some kind of reading but I haven't gotten anything. That's when I questioned how literally I should take the clear pin diagram. The commented out code is also clear to me. More at a stage where I am looking for someone that's used this in the same manner. That is hard to come by.

Then something else is wrong. You should have no problem getting the display and touch screen working with a standard library example, with nothing else (like WiFi) in use.

This is what I have now. I am having trouble connecting now to my mac through Audio MIDI Setup. The code walks through setting up a new device and I have done this before but it won't connect and I am pretty sure I know this because, within serial console, I am never seeing "Connected to Session" in serial monitor.... I'll have to figure that out... not sure what could be wrong. But in testing the pins, literally the 4 I have listed are the only ones that work. The one's that have a "no outlet" warning all go nuts when I try to use them.

So right now I am stuck trying to connect to my mac's wifi that I am creating... also don't know if this is a bad idea. Then I am still uncertain if the right MIDI commands will show up in "MIDI Monitor" but this is my progress... getting closer!

#include <MIDI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>

#define SerialMon Serial
#include <AppleMIDI_Debug.h>

#include <AppleMIDI.h>
#include <stdint.h>
#include "TouchScreen.h"

MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiInterface);  // Declare the midiInterface


char ssid[] = "ATT4K";        // your network SSID (name)
char pass[] = "passpasspass"; // your network password (use for WPA, or use as key for WEP)

const int DEBOUNCE_DELAY = 50;   // the debounce time; increase if the output flickers


#define YP 33
#define XM 32
#define YM 22
#define XP 23

// Initialize the touch screen
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// more variables
// -----------------------------------------------------------------------------
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long t0 = millis();
int8_t isConnected = 0;

APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();

void setup() {
  Serial.begin(115200);  
 
  
  DBG_SETUP(115200);
  DBG("Booting");
  
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    DBG("Establishing connection to WiFi..");
  }
  DBG("Connected to network");

  DBG(F("OK, now make sure you an rtpMIDI session that is Enabled"));
  DBG(F("Add device named Arduino with Host"), WiFi.localIP(), "Port", AppleMIDI.getPort(), "(Name", AppleMIDI.getName(), ")");
  DBG(F("Select and then press the Connect button"));
  DBG(F("Then open a MIDI listener and monitor incoming notes"));
  DBG(F("Listen to incoming MIDI commands"));


  AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {

    isConnected++;
    DBG(F("Connected to session"), ssrc, name);
  });
  AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
    isConnected--;
    DBG(F("Disconnected"), ssrc);
  });
}


void loop(void) {
  // A point object holds x, y, and z (pressure) coordinates
  TSPoint p = ts.getPoint();

  // Translate the x and y values to MIDI range (0-127)
  int midiX = map(p.x, -4000, 0, 0, 127);
  int midiY = map(p.y, 0, 1023, 0, 127);

  byte touchStatus;
  if (p.x > -3072 && p.x < 0 && p.y > 0 && p.y < 1023) { 
    touchStatus = 127;
  } else {
    touchStatus = 0;
  }

  byte channel = 0;   // MIDI channel (0 is Channel 1 in MIDI terms)
  byte ccX = 10;      // Continuous Controller number for X
  byte ccY = 11;      // Continuous Controller number for Y
  byte ccTouchStatus = 12; // Continuous Controller number for touch status

  if (touchStatus == 127) {
    Serial.print("X = "); Serial.print(midiX);
    midiInterface.sendControlChange(ccX, midiX, channel);
    Serial.print("\tY = "); Serial.println(midiY);
    midiInterface.sendControlChange(ccY, midiY, channel);
    Serial.println(touchStatus);
    midiInterface.sendControlChange(ccTouchStatus, touchStatus, channel);
  } else {
    // add anything that should shut off later
  }

  delay(100);
}

I got this to totally work. Here is my code. Now I am going to hopefully utilize the HX8357D Adafruit 3.5 TFT screen I am using to add some interesting graphic content. Hopefully it is as easy as it looks in the video product promo video 3.5 graphic tutorial page I'm referring to... but here's my code for a MIDI Screen that, for me, will turn on an effect, let me manipulate its parameters while touching the screen, then turn of as I let go:

// #include <MIDI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>

#define SerialMon Serial
#include <AppleMIDI_Debug.h>

#include <AppleMIDI.h>
#include <stdint.h>
#include "TouchScreen.h"

// MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiInterface);  // Declare the midiInterface


char ssid[] = "ATT4K";        // your network SSID (name)
char pass[] = "[secret]"; // your network password (use for WPA, or use as key for WEP)

const int DEBOUNCE_DELAY = 50;   // the debounce time; increase if the output flickers


#define YP 33
#define XM 32
#define YM 22
#define XP 23

// Initialize the touch screen
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// more variables
// -----------------------------------------------------------------------------
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long t0 = millis();
int8_t isConnected = 0;
int touchMemory = false;

APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();

void setup() {
  Serial.begin(115200);
  
  DBG_SETUP(115200);
  DBG("Booting");
  
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    DBG("Establishing connection to WiFi..");
  }
  DBG("Connected to network");

  DBG(F("OK, now make sure you an rtpMIDI session that is Enabled"));
  DBG(F("Add device named Arduino with Host"), WiFi.localIP(), "Port", AppleMIDI.getPort(), "(Name", AppleMIDI.getName(), ")");
  DBG(F("Select and then press the Connect button"));
  DBG(F("Then open a MIDI listener and monitor incoming notes"));
  DBG(F("Listen to incoming MIDI commands"));

  MIDI.begin();

  AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
    isConnected++;
    DBG(F("Connected to session"), ssrc, name);
  });
  AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
    isConnected--;
    DBG(F("Disconnected"), ssrc);
  });
}


void loop() {
  MIDI.read();
  
  // A point object holds x, y, and z (pressure) coordinates
  TSPoint p = ts.getPoint();

  // Translate the x and y values to MIDI range (0-127)
  int midiX = map(p.x, -3072, 0, 0, 127);
  int midiY = map(p.y, 0, 1023, 0, 127);

  byte touchStatus;
  if (p.x > -3072 && p.x < 0 && p.y > 0 && p.y < 1023) { 
    touchStatus = 127;
  } else {
    touchStatus = 0;
  }

  byte channel = 6;   // MIDI channel (0 is Channel 1 in MIDI terms)
  byte ccX = 10;      // Continuous Controller number for X
  byte ccY = 11;      // Continuous Controller number for Y
  byte ccTouchStatus = 12; // Continuous Controller number for touch status

  if (touchStatus == 127) {
    Serial.print("X = "); Serial.print(midiX);
    MIDI.sendControlChange(ccX, midiX, channel);
    Serial.print("\tY = "); Serial.println(midiY);
    MIDI.sendControlChange(ccY, midiY, channel);
    Serial.println(touchStatus);
    MIDI.sendControlChange(ccTouchStatus, touchStatus, channel);
    touchMemory = true;
  } else if (touchStatus == 0 && touchMemory == true ) {
    // add anything that should shut off later
    Serial.println(touchStatus);
    MIDI.sendControlChange(ccTouchStatus, touchStatus, channel);
    touchMemory = false;
  }

  delay(100);
}

Any hints on making my next step easier would be awesome. I have heard I might need to go into a header file of the HX8357D library and enable my screen?!?! We shall see.. but this works great for x/y touch and took a while to find pins that would work and also weren't ones that I wanted/need to use later.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.