Can't work in parallel with U8g2 and TFT_eSPI

I have an ESP32 connected to two displays:

  1. 1.28" round color display 240x240 pixel using GC9A01 driver for which I'm using TFT_eSPI library.
  2. 2.08" 256x64 monochrome display using SH1122 driver for which I'm using the U8G2 library.

I have managed to work with two 1.28" round displays but now I'm trying with 2 different type of displays and it doesn't work.

Code
#include <Arduino.h>
#include <U8g2lib.h>
#include "SPI.h"
#include <TFT_eSPI.h>
#include "NotoSansBold15.h"

U8G2_SH1122_256X64_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=SCL=*/ 18, /* data=SDA=*/ 23, /* cs=*/ 32, /* dc=*/ 13, /* reset=*/ 4);       
TFT_eSPI tft = TFT_eSPI();
#define roundTft 33 //roundTft 33  // CS pin for round TFT
#define squareTft 32 // CS pin for monochrome display


void setup(void) {
  
  /*pinMode(squareTft, OUTPUT);
  digitalWrite(squareTft, LOW); // Activate monochrome display
  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_t0_17_mf);
  u8g2.setCursor(00,10);
  u8g2.print("Hello World");
  u8g2.sendBuffer();
	digitalWrite(squareTft, HIGH); // Deactivate monochrome display

  delay(1000);
*/
  pinMode(roundTft, OUTPUT);
  tft.begin();
  digitalWrite(roundTft, LOW); // Activate round TFT
  tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
  tft.loadFont(NotoSansBold15);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(30, 40);
  tft.print("Hello Round TFT");
  delay(1000);
  digitalWrite(roundTft, HIGH); // Deactivate round TFT
}


void loop(void) {
}

If I run the code as is, the round display will work
If I uncomment the first part and comment the second one, the monochrome display will work.

If I leave the code as is and I uncomment even just the line: pinMode(squareTft, OUTPUT);
The round display will not display anything.

How can I solve this?

A schematic of all the connections between the displays and the ESP32 would be useful to us maybe.

Why are you manually controlling the CS pin for the displays, the display library normally does that for you ?

TFT wiring
DC --> 13
RES --> 4
SDA --> 23
SCL --> 18
CS 2.08" display--> 32
CS 1.28" display-->33

Because I want to display different things on each display and I found that this is the way to achieve that.
By the way it did require to change some definitions in the TFT_eSPI setup file.

You appear to be using a software SPI for the U8G2 and I assume hardware SPI on the same pins for the TFT_eSPI. I would be surprised if that would work.

Try using the same hardware SPI pins, SCK and MOSI, for each display.

No need, the library for each display knows the CS pin for its display and should control it automatically.

I had some problems working with the HW SPI for the U8G2, it worked well for simple objects but when I started adding images, it wasn't stable and after a few seconds the rendering on the screen was full of lines.

Anyway, I tried changing the code, to the below and now it works better BUT
The monochrome display is only showing the text for 3 seconds, afterward it darkens.

Code
#include <Arduino.h>
#include <U8g2lib.h>
#include "SPI.h"
#include <TFT_eSPI.h>
#include "NotoSansBold15.h"

//U8G2_SH1122_256X64_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=SCL=*/ 18, /* data=SDA=*/ 23, /* cs=*/ 32, /* dc=*/ 13, /* reset=*/ 4);       
U8G2_SH1122_256X64_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 32, /* dc=*/ 13, /* reset=*/ 4);
TFT_eSPI tft = TFT_eSPI();
#define roundTft 33  // CS pin for round TFT
#define squareTft 32 // CS pin for monochrome display


void setup(void) {
  
  pinMode(squareTft, OUTPUT);
  
  digitalWrite(squareTft, LOW); // Activate monochrome display
  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_t0_17_mf);
  u8g2.setCursor(00,10);
  u8g2.print("Hello World");
  u8g2.sendBuffer();
	digitalWrite(squareTft, HIGH); // Deactivate monochrome display

  delay(3000);

  pinMode(roundTft, OUTPUT);
  tft.begin();
  digitalWrite(roundTft, LOW); // Activate round TFT
  tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
  tft.loadFont(NotoSansBold15);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(30, 40);
  tft.print("Hello Round TFT");
  delay(1000);
  digitalWrite(roundTft, HIGH); // Deactivate round TFT
}


void loop(void) {
}

I removed the CS control for the monochrome display, and the screen still darkens after 3 seconds.

New Code
#include <Arduino.h>
#include <U8g2lib.h>
#include "SPI.h"
#include <TFT_eSPI.h>
#include "NotoSansBold15.h"

//U8G2_SH1122_256X64_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=SCL=*/ 18, /* data=SDA=*/ 23, /* cs=*/ 32, /* dc=*/ 13, /* reset=*/ 4);       
U8G2_SH1122_256X64_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 32, /* dc=*/ 13, /* reset=*/ 4);
TFT_eSPI tft = TFT_eSPI();
#define roundTft 33  // CS pin for round TFT
//#define squareTft 32 // CS pin for monochrome display


void setup(void) {
  
  //pinMode(squareTft, OUTPUT);
  
  //digitalWrite(squareTft, LOW); // Activate monochrome display
  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_t0_17_mf);
  u8g2.setCursor(00,10);
  u8g2.print("Hello World");
  u8g2.sendBuffer();
	//digitalWrite(squareTft, HIGH); // Deactivate monochrome display

  delay(3000);

  pinMode(roundTft, OUTPUT);
  tft.begin();
  digitalWrite(roundTft, LOW); // Activate round TFT
  tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
  tft.loadFont(NotoSansBold15);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(30, 40);
  tft.print("Hello Round TFT");
  delay(1000);
  digitalWrite(roundTft, HIGH); // Deactivate round TFT
}


void loop(void) {
}

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