Customizing Adafruit TFTLCD Library for Arduino Mega with Uno Shield

Hello,

I want to customize the Adafruit TFT LCD library for my project. I’m using a 2.8-inch TFT display with an ILI 9341 driver, which operates with an 8-bit data transfer. It works fine with the Adafruit TFT LCD library on an Arduino Uno. However, due to the limited SRAM, I need to switch to a Mega. Unfortunately, the display does not work with the library on the Mega.

The issue seems to be that the data pins on the Arduino Mega are mapped to pins 22 through 29 by default, which do not match the shield designed for the Arduino Uno. A different library works with the shield on the Mega, so it must be possible somehow. I prefer not to use that other library because it doesn’t offer as nice graphical representations.

Is there a way to modify the Adafruit TFT LCD library for my needs? I understand that changes might need to be made in the magic_pin.h file, but I’m not very familiar with what needs to be adjusted. Any guidance would be greatly appreciated.

I have a Mega with a 3.5" TFT screen shield. I use the Adafruit GFX and MCUFRIEND libraries.
This works for the screen. If it has an SD card slot, it won't without wiring changes.
Worth a try.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>

MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define WHITE   0xFFFF

void setup() {
  Serial.begin(38400);

  uint16_t ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Hello World!");
}

void loop() {
}
1 Like

Can you keep the information in EEPROM or FRAM memory? You could also use a SD card. On many of my systems I have replaced the EEPROM (electrically erasable programmable read-only memory) on the RTC (Real Time Clock ) with FRAM (Ferroelectric RAM) giving me 32Kx8 for just a few $$$.

Yes, I’ve tried that library, and it does work. That’s the one I mentioned earlier. However, I’d prefer not to use it because its capabilities for nice graphical visualizations are quite limited. Since it works, I’m fairly sure there must be a way to get the Adafruit TFT-LCD library to work as well.

I’m aware of the issue with the SD card. I’ll be using an SD card for buffering, but I’d like to use the Mega to take advantage of the extra memory, so I won’t need to store data too frequently. I’m thinking about carefully bending the pins to connect the SD card’s pins to the correct SPI pins on the Arduino Me

What capabilities are you lacking with the Adafruit_GFX library? The Adafruit TFT-LCD library uses it.

What do you mean ? I’m not missing anything in the Adafruit GFX lib, my problem is the MUFRIEND_kv.h. I tried this lib instead Adafruit TFTLCD and everything worked fine, but this lib does not come with options as for example setting the font. That’s my my displayed homescreen looks kinda old und ugly

From Adafruit_GFX.h
void setFont(const GFXfont *f = NULL);

Edit: Here is a link to the functions available to you with Adafruit_GFX.h

MCUFRIEND_kbv inherits from Adafruit_GFX.

class MCUFRIEND_kbv : public Adafruit_GFX {

I don’t know what you wanna say. I’m gonna use Adafruit GFX and with MUFriend it works fine. But I want to use the Adafruit TFTLCD lib instead MUFriend on my Mega. The problem is, that the Adafruit TFTLCD lib set the data pins to 22-29, as default for Mega. I want to use the default for Uno instead and therefore need to make changes in the lib

The same library (Adafruit_GFX) works with MCUFRIEND_kbv.h
The same library (Adafruit_GFX) does NOT work with Adafruit-TFTLCD.h
So it appears setting the data pins is what screws it up.

Yep your right. So I need to make some changes in the adafruit TFTLCD lib, so that the data pins are not 22-29 (as default for Mega). They must be the same as for Uno, because it’s a Uno Shield

What functions are you planning on using that you didn't find in the Adafruit_GFX.h library?
I use it to display bitmap graphics from the SD card.

Edit: This is the graphics library that AdaFruit_TFTLCD.h uses:
#include "Adafruit_GFX.h"

This is the graphics library that MCUFRIEND_kbv.h uses:
#include "Adafruit_GFX.h"

Do you see the difference? Look close!

I want to change the font to make me display look more modern. And I can’t do that when using MUFriend

If you can't change the font in the one that works, how do you expect to change it using the same graphics library in the other that doesn't?

ok, again. I built the whole thing with the Adafruit TFTLCD on an Aduino Uno, it works, including setting the font. The library has stored fonts that you can use. As I now need more SRAM, I have switched to a Mega. The problem now is that the Mega uses different pins for the data lines of the display by default, namely 22-29. Since the display is a Uno shield, the pins of the data lines are not on the standard pins of the Mega. For this reason, the Adafruit TFTLCD does not work on the Mega and the display.
Then I tested the MCUFRIEND and it works, it somehow recognizes that the data lines are on other pins. But here I can't set the font the way I want. That's why I want to change the Adafruit library so that the standard data pins of the Uno are still used when using the Mega.

Translated with DeepL.com (free version)

I know about those fonts. Want to change them with MCUFRIEND_kbv.h?

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

MCUFRIEND_kbv tft;

// include your fonts
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#define BLACK   0x0000
#define WHITE   0xFFFF
#define GREEN   0x07E0

void setup() {
  Serial.begin(38400);

  uint16_t ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);

// set your font
  tft.setFont(&FreeSans9pt7b);
  tft.setCursor(30, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Hello World!");

// set another font
  tft.setFont(&FreeSerif12pt7b);
  tft.setCursor(30, 90);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.print("Hello!");
}

void loop() {
}

wait what?.. i didnt know you can adjust the font type with MCUFRIEND lib. Tanks! You helped me a lot

That's what I've been trying to tell you. It's the same library. If you can do it with one, you can do it with the other.