Errors when trying to use my ILI9486 display with Uno R4 Minima with the Adafruit GFX and MCUFriend Libraries

I have been working on a digital dashboard with an ILI9486 display with my Uno R3. The code compiles and runs flawlessly on my Uno R3. As I have many plans for the dashboard, I was sure to exceed the program space available on the Uno R3 (the code had already used close to 60%). Moving to the Mega 2560 made the TFT run extremely slow.

I made the decision to purchase an Uno R4 Minima as it has a larger program storage. I assumed the TFT display would run as fast as the R3 due to the similar pinouts between the two, as well as handle any additional code I could throw at it.

However, compiling the code for the Uno R4 Minima produces errors that span around 50 pages when copied into word. Reading through the forums had me come to a conclusion that the issues are stemming from the incompatibility of some libraries for the R4 at this stage.

I come here requesting help in making the Adafruit GFX library work with the Uno R4 Minima. Any input would be received with extreme gratefulness.

Display used:
ILI9486 with SD Card: Bought here

Code used:

#include <DHT.h>

const byte dhtPin = 11;
#define DHTTYPE DHT11
DHT dht(dhtPin, DHTTYPE);


const int pingPin = 12; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 13; // Echo Pin of Ultrasonic Sensor



#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define RGB(r, g, b) (((r&0xF8)<<8)|((g&0xFC)<<3)|(b>>3))

#define GREY      RGB(127, 127, 127)
#define DARKGREY  RGB(64, 64, 64)
#define TURQUOISE RGB(0, 128, 128)
#define PINK      RGB(255, 128, 192)
#define OLIVE     RGB(128, 128, 0)
#define PURPLE    RGB(128, 0, 128)
#define AZURE     RGB(0, 128, 255)
#define ORANGE    RGB(255,128,64)
#include <stdio.h>
uint16_t ID;



void setup() {
  Serial.begin(9600); // Starting Serial Terminal
  tft.reset();
  ID = tft.readID();
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  tft.setTextSize(6);
  tft.setTextColor(BLACK);
  tft.fillRect(10,10,425,50,WHITE);
  tft.fillRect(10,170,425,50,WHITE); 
  

  tft.setCursor(10,15);
  tft.print("Distance:");

  tft.setCursor(10,175);
  tft.print("Temperature: ");
}

void loop() {
  int temp = dht.readTemperature();
  long dist = distance();

  tft.fillRect(150,90,150,50,BLUE);
  tft.fillRect(150,260,150,50,BLUE); 
  

  tft.setCursor(160,90);
  tft.println(dist);


  tft.setCursor(160,260);
  tft.print(temp);
  tft.println("C");


  delay(300);
}

long distance()
{
  long duration, inches, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  Serial.print("Duration: ");
  Serial.println(duration);
  cm = duration / 29 / 2;
  return cm;
}


Connections:

The TFT shield goes right on top of the Arduino using jumpers. The pins 10-13 have been left unconnected as I am not using the SD Card module.
In these pins I have attached my sensors, a DHT11 temperature sensor (pin 11) and a HC-SR04 ultrasonic sensor (pin 12 and 13) to read the temperature and the distance.

Errors obtained:
As the errors after compiling came up to almost 5000 words, I have taken snippets of some of the errors shown (a few seemed to be repeated) and added them below. If the entire thing is required, I shall add that as well.

c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\MCUFRIEND_kbv.cpp:121:5: note: in expansion of macro 'CS_ACTIVE'
     CS_ACTIVE;
     ^~~~~~~~~
c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\utility/mcufriend_shield.h:1215:20: error: 'PIN_LOW' was not declared in this scope
 #define CS_ACTIVE  PIN_LOW(CS_PORT, CS_PIN)
                 ^~~~~~~~~
c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\utility/mcufriend_shield.h:1212:28: error: 'CD_PORT' was not declared in this scope
 #define CD_COMMAND PIN_LOW(CD_PORT, CD_PIN)
                            ^
c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\utility/mcufriend_shield.h:1230:24: note: in expansion of macro 'CD_COMMAND'
 #define WriteCmd(x)  { CD_COMMAND; write16(x); CD_DATA; }
                        ^~~~~~~~~~
c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\utility/mcufriend_shield.h:1212:28: note: suggested alternative: 'R_PORT0'
 #define CD_COMMAND PIN_LOW(CD_PORT, CD_PIN)
                            ^
c:\Users\Jameel Ahamed\Documents\Arduino\libraries\MCUFRIEND_kbv\utility/mcufriend_shield.h:1213:20: error: 'PIN_HIGH' was not declared in this scope
 #define CD_DATA    PIN_HIGH(CD_PORT, CD_PIN)
                    ^


Note:
If this post belongs in the "Display" section of this forum, I will move it there. This is my first post so I wasn't sure.

It should run at exactly the same speed as it does on the Uno R3. I wonder what you were doing wrong.

I remember reading an answer by David Prentice (RIP) about why it is slower and how to get around it ([SOLVED] Shield TFT ILI9341 very slower on MEGA! - #12).

But I really like the compact dimensions of the Uno. My project would benefit from having smaller components used.

I would greatly appreciate it if there was a way to get this code working on the new R4, or at least a push in the right direction to work on a fix myself.

It may not help you much but I'm guessing that this macro is attempting to do the equivalent of digitalWrite() but using a (quicker) architecture dependent direct port manipulation method. Interestingly, the library was not written to abandon the compilation with an "unknown architecture" message.

Okay, thanks for the help.

Do you think I should move this thread to the Display section of this forum? I might get some other opinions as well.

I am currently having the same issue with my Uno R4 Wifi with the same display shield. Did you get any further? I currently have a thread active in the Display section for this problem.

You should edit a utility/mcufriend_special.h file of the library and add register support for Uno R4 Renesas controller.
Unfortunately< I don't have a R4 yet and can't help you much.

You could take a look at this post: https://forum.arduino.cc/t/uno-r4-wifi-with-3-5-tft-lcd-not-compiling/1150403/14

Thank you! I saw it and reviewed the code. Great work. It is just that I wanted to keep the original MCUFRIEND-kvb library I use because it is included in many projects. That’s why I patched the library instead of using your code directly. But I used the port macro code as suggested by you.