I'm currently drawing black boxes over old content before displaying constantly refreshing content on my display, so for instance to display temperature:

if (sensorTemp != nowTemp){
    nowTemp = sensorTemp;
    return nowTemp;

If I don't do this the display will just overlay content until it becomes a white blob.

However, this solution leaves me with a black flickering on the display and I can't control exactly what is refreshed where. So for instance when I want to display the current time or date it has to refresh the date all the time, even though nothing changed.

void loop() {
  tft.fillRect(144,0,199,15,ILI9341_BLACK); //Black box drawn over 'old' time data
  tft.print(rtc.getTimeStr());  //Draw new time
  tft.print(tempCall());  //call function to check on temperature and draw new data if necessary

Is there a more elegant solution to this problem?

  • What library are you using? You either want to render your string in an off-screen buffer (which takes lots of memory and library functions that support that kind of thing) or somehow make the characters print the background as well as the foreground colour - and that depends on what library you are using. The Uno is poorly equipped for working with TFT screens due to lack of memory. It certainly wouldn't be my first choice (it wouldn't even make it on my list at all...)
    – Majenko
    Mar 9, 2018 at 12:07
  • @Majenko I'm using a Mega with the Adafruit GFX library for ILI9341 tft screens. I am not aware of any other libraries that work with SPI screens, but I haven't done any extensive googling on that yet.
    – Streamline
    Mar 9, 2018 at 12:14
  • Ah, good. That library is the one I originally based DisplayCore (chipKIT display library suite). It can be done. I'll explain how...
    – Majenko
    Mar 9, 2018 at 12:15
  • @Majenko ah! I remember you! You're the guy who told me about SPI like a month ago :)
    – Streamline
    Mar 9, 2018 at 12:20

3 Answers 3


The Adafruit GFX library has two functions for setting the text colour:

setTextColor(uint16_t c)
setTextColor(uint16_t c, uint16_t bg)

The first sets both the foreground and background to the same colour, and the second to two different colours. You may ask why you would want the foreground and background to be the same - well, the library handles that situation as a special case: it makes the background transparent.

By setting the foreground and background to different colours you force the library to draw that background colour in over whatever is already on the screen.

tft.setTextColor(ILI93481_WHITE, ILI9341_BLACK);

With that any background pixels in the text string will be coloured black and the text itself white. As long as the string you are printing is the same length as the text that is already there it will be replaced completely.

  • Thanks again for your help, this made my whole code a lot easier and displaying data much more elegant.
    – Streamline
    Mar 9, 2018 at 12:30
  • 4
    Should be noted that this only works with the built-in GLCD font. Custom fonts can't be re-drawn in this manner. Jul 11, 2018 at 14:22

If you look in the "Adafruit_GFX.cpp" you will see the following.

void Adafruit_GFX::setTextColor(uint16_t c) {
 // For 'transparent' background, we'll set the bg 
 // to the same as fg instead of using a flag
 textcolor = textbgcolor = c;

void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
 textcolor   = c;
 textbgcolor = b; 

If you only want to Draw the Character with a transparent background meaning you only write the character setTextColor in the prescribed pixels changing none of the surrounding pixels in the character block defined by x, y, setTextSize.

TFT.setTextColor(GREEN); //With Transparent Text Background not updating surrounding pixels.

If you want to Draw the Character with your background color, replace app pixels in the character block defined by x, y, setTextSize with your chosen text color and your background to be the same as your chosen background. This will overwrite all pixels with old character that are no longer used with the background color all at the same time preventing mess of pixels, and character flashing.

TFT.setTextColor(GREEN, BLACK); //With Text Background Color updating surrounding pixels.

Hmm the solution is how the code is written...no fancy stuff...the KIWI way

//in setup function add tft.fillScreen(ST7735_GREEN);

void loop(){
  int lightIntensity = myBH1750.getLux();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.