Absolute Beginner’s Guide to TFT LCD Displays by Arduino

Overview

In this article, you will learn how to use TFT LCDs by Arduino boards. From basic commands to professional designs and technics are all explained here. At the end of this article, you can :

  • Write texts and numbers with your desired font.
  • Draw shapes like circle, triangle, square, etc.
  • Display .bmp images on the screen.
  • Change screen parameters such as rotating and inverting color.
  • Display an animation by Arduino.

Presenting Ideas on Displays

In electronic’s projects, creating an interface between user and system is very important. This interface could be created by displaying useful data, a menu, and ease of access. A beautiful design is also very important.

There are several components to achieve this. LEDs,  7-segments, Character and Graphic displays, and full-color TFT LCDs. The right component for your projects depends on the amount of data to be displayed, type of user interaction, and processor capacity.

TFT LCD is a variant of a liquid-crystal display (LCD) that uses thin-film-transistor (TFT) technology to improve image qualities such as addressability and contrast. A TFT LCD is an active matrix LCD, in contrast to passive matrix LCDs or simple, direct-driven LCDs with a few segments.

In Arduino-based projects, the processor frequency is low. So it is not possible to display complex, high definition images and high-speed motions. Therefore, full-color TFT LCDs can only be used to display simple data and commands.

In this article, we have used libraries and advanced technics to display data, charts, menu, etc. with a professional design. This can move your project presentation to a higher level.

In electronic’s projects, creating an interface between user and system is very important. This interface could be created by displaying useful data, a menu, and ease of access. A beautiful design is also very important.

There are several components to achieve this. LEDs,  7-segments, Character and Graphic displays, and full-color TFT LCDs. The right component for your projects depends on the amount of data to be displayed, type of user interaction, and processor capacity.

TFT LCD is a variant of a liquid-crystal display (LCD) that uses thin-film-transistor (TFT) technology to improve image qualities such as addressability and contrast. A TFT LCD is an active matrix LCD, in contrast to passive matrix LCDs or simple, direct-driven LCDs with a few segments.

In Arduino-based projects, the processor frequency is low. So it is not possible to display complex, high definition images and high-speed motions. Therefore, full-color TFT LCDs can only be used to display simple data and commands.

In this article, we have used libraries and advanced technics to display data, charts, menu, etc. with a professional design. This can move your project presentation to a higher level.

Which Size? Which Controller?

Size of displays affects your project parameters. Bigger Display is not always better. if you want to display high-resolution images and signs, you should choose a big size display with higher resolution. But it decreases the speed of your processing, needs more space and also needs more current to run.

So, First, you should check the resolution, speed of motion, details of color and size of your project’s images, texts, and numbers.

We suggest popular size of Arduino displays such as 3.5 inch 480×320 , 2.8 inch 400×240 , 2.4 inch 320×240 and 1.8 inch  220×176.

After choosing the right display, It’s time to choose the right controller. If you want to display characters, tests, numbers and static images and the speed of display is not important, the Atmega328 Arduino boards (such as Arduino UNO) are a proper choice. If the size of your code is big, The UNO board may not be enough. You can use Arduino Mega2560 instead. And if you want to show high resolution images and motions with high speed, you should use the ARM core Arduino boards such as Arduino DUE.

Required Materials

Hardware Components

3.5" TFT Color Display Screen Module×1
2.4" TFT LCD Display Shield×1
Arduino UNO R3×1
Arduino Mega 2560×1
Arduino DUE×1

Software Apps

Arduino IDE

Drivers & Libraries

In electronics/computer hardware a display driver is usually a semiconductor integrated circuit (but may alternatively comprise a state machine made of discrete logic and other components) which provides an interface function between a microprocessor, microcontroller, ASIC or general-purpose peripheral interface and a particular type of display device, e.g. LCD, LED, OLED, ePaper, CRT, Vacuum fluorescent or Nixie.

The display driver will typically accept commands and data using an industry-standard general-purpose serial or parallel interface, such as TTL, CMOS, RS232, SPI, I2C, etc. and generate signals with suitable voltage, current, timing and demultiplexing to make the display show the desired text or image.

The LCDs manufacturers use different drivers in their products. Some of them are more popular and some of them are very unknown. To run your display easily, you should use Arduino LCDs libraries and add them to your code. Otherwise running the display may be very difficult. There are many free libraries you can find on the internet but the important point about the libraries is their compatibility with the LCD’s driver. The driver of your LCD must be known by your library. In this article, we use the Adafruit GFX library and MCUFRIEND KBV library and example codes. You can download them from the following links.

Necessary Files and Downloads:


Unzip the MCUFRIEND KBV and open the MCUFRIEND_kbv.CPP. You can see the list of drivers that are supported by MCUFRIEND library.

Open Example folder. There are several example codes that you can run by Arduino. Hook up the LCD and test some of the examples.

Code

You must add the library and then upload the code. If it is the first time you run an Arduino board, don’t worry. Just follow these steps:

  1. Go to www.arduino.cc/en/Main/Software and download the software of your OS. Install the IDE software as instructed.
  2. Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
  3. Navigate to sketch and include the libraries . Now click add  ZIP library and add the libraries
  4. Choose the board in tools and boards, select your Arduino Board.
  5. Connect the Arduino to your PC and set the COM port in tools and port.
  6. Press the Upload (Arrow sign) button.
  7. You are all set!

Library


The first line adds core graphics library for displays (written by Adafruit).

The second adds a library that supports drivers of MCUFRIEND Arduino display shields.


These libraries are not necessary for now, but you can add them.

Basic Commands

Class & Object


This line makes an object named TFT from MCUFRIEND_kbv class and provides an SPI communication between LCD and Arduino.

Running the LCD


The tft.readID function reads ID from the display and put it in ID variable. Then tft.begin function gets ID and the LCD gets ready to work.

Resolution of the Display


By these two functions, You can find out the resolution of the display. Just add them to the code and put the outputs in a  variable. Then read it from the Serial port by . First add in .

Color of the Screen


 function change the color of screen to color. The should be a 16bit variable containing UTFT color code.


You can add these lines to the top of your code and just use the name of the color in the functions.

Filling Pixels


function fills a pixel in x and y location by t color.

 function read the color of a pixel in x and y location.

Drawing Lines


function draws a vertical line that starts in x, y location, and its length is h pixel and its color is t.

function draws a horizontal line that starts in x and y location and the length is w pixel and the color is t.

function draws a line that starts in xi and yi locationends is in xj and yj and the color is t.


These three blocks of code draw lines like the previous code with 5-pixel thickness.

Drawing Rectangles


function draws a filled rectangle in x and y location. w is width, h is height and t is color of the rectangle

 function draws a rectangle in x and y location with w width and h height and t color.

 function draws a filled Rectangle with r radius round corners in x and y location and w width and h height and t color.

function draws a Rectangle with r radius round corners in x and y location and w width and h height and t color.

Drawing Circles


function draws a circle in x and y location and r radius and t color.

function draws a filled circle in x and y location and r radius and t color.


By this code, you can draw an Arc. change the “for” between 0 to 4000.

Drawing Triangles


function draws a triangle with three corner location x, y and z, and t color.

function draws a filled triangle with three corner location x, y and z, and t color.

Displaying Text


This code sets the cursor position to of x and y


The first line sets the color of the text. Next line sets the color of text and its background.


This code sets the size of text by .  is a number between 1 and 5.


This code displays a character.


The first function displays a string and moves the cursor to the next line.

The second function just displays the string.


This function changes the font of the text. You should add this function and font libraries.


This function can fade your text. You should add it to your code.

Rotating the Screen


This code rotates the screen. 0=0 , 1=90, 2=180, 3=270 .

Inverting Screen Colors


This code inverts the colors of the screen.


This code give RGB code and get UTFT color code.

Scrolling the Screen


This code Scroll your screen. The Maxroll is the maximum height of your scrolling.

Reset


This code resets the screen.

Displaying Monochrome Images


First you should convert your image to hex code. Download the software from the following link. if you don’t want to change the settings of the software, you must invert the color of the image and make the image horizontally mirrored and rotate it 90 degrees counterclockwise. Now add it to the software and convert it. Open the exported file and copy the hex code to Arduino IDE. x and y are locations of the image. sx and sy are sizes of image. you can change the color of the image in the last input.

Necessary Files and Downloads:

RGB Color Image Displaying


First you should convert your image to code.Use this link to convert the image:

http://www.rinkydinkelectronics.com/t_imageconverter565.php

Upload your image and download the converted file that the UTFT libraries can process. Now copy the hex code to Arduino IDE. x and y are locations of the image. sx and sy are size of the image.

Predesigned Templates

In this template, We just used a string and 8 filled circles that change their colors in order. To draw circles around a static point ,You can use sin();  and cos(); functions. you should define the PI number . To change colors, you can use color565(); function and replace your RGB code.

In this template, We chose a classic font and used a function to fade the text.

In this template, We converted a .jpg image to .c file and added to the code, wrote a string and used the fade code to display. Then we used scroll code to move the screen left. Download the .h file and add it to the folder of the Arduino sketch.

Header File


In this template, We used draw lines, filled circles, and string display functions.

In this template, We used sin(); and cos(); functions to draw Arcs with our desired thickness and displayed number by text printing function. Then we converted an image to hex code and added them to the code and displayed the image by bitmap function. Then we used draw lines function to change the style of the image. Download the .h file and add it to the folder of the Arduino sketch.

Header File


In this template, We created a function which accepts numbers as input and displays them as a pie chart. We just use draw arc and filled circle functions.

In this template, We added a converted image to code and then used two black and white arcs to create the pointer of volumes.  Download the .h file and add it to the folder of the Arduino sketch.

Header File


In this template, We added a converted image and use the arc and print function to create this gauge.  Download the .h file and add it to folder of the Arduino sketch.

Header File


In this template, We display simple images one after each other very fast by bitmap function. So you can make your animation by this trick.  Download the .h file and add it to folder of the Arduino sketch.

Header File


In this template, We just display some images by RGBbitmap and bitmap functions. Just make a code for touchscreen and use this template.  Download the .h file and add it to folder of the Arduino sketch.

Header File


Final Remarks

  • The speed of playing all the GIF files are edited and we made them faster or slower for better understanding. The speed of motions depends on the speed of your processor or type of code or size and thickness of elements in the code.
  • You can add the image code in the main page but it fills all the main page. So you can make a .h file and add in the folder of the sketch.
  •  In this article, We just discussed about displaying elements on LCD. Follow our next tutorials to learn using touch screens and SD Cards.

Share this post

Comments (7)

  • Fabrizio Reply

    Best ever beginner’s guide. Thanks!

    August 17, 2019 at 3:02 pm
    • mohammad Reply

      You Are Welcome. We are happy to hear this 🙂

      August 17, 2019 at 5:23 pm
  • Petter Peper Reply

    Wonderful job!!!
    I have one question to add Volume.h in the project to Mega it says : too many initializers for ‘const uint16_t [0]

    To compile this project i must have an Arduino DUE?

    Thanks.

    August 20, 2019 at 2:11 pm
    • Saeed Hosseini Reply

      Hello.
      We are glad you are interested in this project.
      Yes you should use Arduino DUE.

      August 22, 2019 at 4:30 am
  • Darene Reply

    had a lot of troubles until I discovered this site, finally i got started with these TFT’s lol ==> kudos!!

    I still have a problem,

    tft methods work fine in the void setup part,but whenever I move one into void loop it says the tft was not declared in this scope.

    so for now i have to create a loop in the setup part …

    any suggestions?

    November 17, 2019 at 10:55 pm
    • Saeed Hosseini Reply

      make sure that “tft.begin(ID);” still is in setup.

      November 24, 2019 at 10:59 am
  • Tim Kern Reply

    Thank you for this tutorial. I feel that maybe I have a chance!
    All I ever see is a blank white screen. I have downloaded the libraries and extracted them to the defaults, and when I try to open any of the sketches, all I get is line after line of errors. It doesn’t matter what sketch I try — none will compile.
    I know it must be something simple, but could I please get just one example of any sketch that will work on my 3.5″ TFT / Mega 2560? I have no one to ask, and I have been trying one tutorial after another for more than six weeks.
    I am not asking for anyone’s troubleshooting help — I just want a successful sketch. I want anything that will work.
    This isn’t as fun as it used to be. Can anybody give me one sketch that works, that I can copy and paste?
    Thank you for your help!

    January 9, 2020 at 1:19 am

Leave a Reply

Your email address will not be published. Required fields are marked *