Getting Started with the Arduino TFT Screen

The first steps to setting up the Arduino TFT Screen

This is a retired product.

The Arduino TFT screen is a backlit TFT LCD screen with a micro SD card slot in the back. You can draw text, images, and shapes to the screen with the TFT library.

ImageOnTFT

The screen's pin layout is designed to easily fit into the socket of an Arduino Esplora and Arduino Robot, but it can be used with any Arduino board.

The TFT library is included with Arduino IDE 1.0.5 or later.

Library

The Arduino TFT library extends the Adafruit GFX, and Adafruit ST7735 libraries that it is based on. The GFX library is responsible for the drawing routines, while the ST7735 library is specific to the screen on the Arduino screen. The Arduino specific additions were designed to work as similarly to the Processing API as possible.

The library is backwards compatible, which means you can still use the Adafruit functions described here.

The TFT library relies on the SPI library, which must be included in any sketch that uses the scree. If you wish to use the SD card, you need to include the SD library as well.

Screen layout

By default, the screen is oriented so it is wider than it is tall. The top of the screen is the same side as the text 'SD CARD''. In this orientation, the screen is 160 pixels wide and 128 pixels high.

When thinking about coordinates on the screen, imagine a grid. Each square in the grid is a pixel. You can identify the placement of pixels with specific coordinates. A dot in the top left corner would have coordinates of 0,0. If this dot were to move to the top right of the screen, its coordinates would be 0, 159; in the bottom left corner, the coordinates would be 127,0, and in the bottom right it would be 127,159.

It is possible to use the screen in a vertical, (also called "portrait") orientation, by calling

setRotation(0)
. When you call this, the x and y-axes change accordingly, and calls to
screen.width()
or
screen.height()
change as well.

Colors

The screen has the ability to show 16-bit color. The red and blue have 5-bits of resolution each (32 levels of red and blue), the green has 6-bits of resolution (64 different levels). For consistency with other applications, the library deals with color in 8-bit values for the red, green, and blue channels (0-255), and scales the colors appropriately.

Hardware vs software SPI interface

The screen can be configured for use in two ways. One is to use an Arduino's hardware SPI interface. The other is to declare all the pins manually. There is no difference in the functionality of the screen between the two methods, but using hardware SPI is significantly faster when drawing.

If you plan on using the SD card on the TFT module, you must use hardware SPI.

All the examples are written for hardware SPI use.

Connecting the screen

GLCD pinUse

Connecting to the Esplora

There is a socket on the front of the Esplora for the screen. Insert the screen into the socket with the blue tab that says "SD Card" closest to the USB port.

Esplora GTFT

Connecting to other Arduino boards

To connect the screen to other Arduino boards, read the tutorial on this link.

Write your first program

To get started with the screen, first write a program that will draw a line, then 2 rectangles horizontally across the screen in different colors.

The first set of instructions are for the Uno, Leonardo, and similar boards. For use with the Esplora, see below.

First, declare the pins to use, import the necessary libraries, and instantiate a named instance of the TFT library. :

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3
4#define CS 10
5#define DC 9
6#define RESET 8
7
8// pin definition for the Leonardo
9// #define CS 7
10// #define DC 0
11// #define RESET 1
12
13TFT myScreen = TFT(CS, DC, RESET);

In

setup()
, you need to start the library with
begin()
and clear the screen by filling it in black with
background()
.

1void setup(){
2
3 myScreen.begin();
4
5 myScreen.background(0,0,0); // clear the screen with black
6
7 delay(1000); // pause for dramatic effect
8}

in

loop()
, to draw a line across the screen, call
line()
.
line()
takes four arguments, the the starting x and y coordinates, and the ending x and y coordinates. For drawing a box, use
rect()
.
rect()
take four arguments as well : the x and y coordinates of the top left corner, followed by the width in pixels, and the height in pixels. Between each of these calls, change the color with
stroke()
or
fill()
.
stroke()
will change the color of a line, or the outline around a shape.
fill()
changes the internal color of a shape. Calling
noStroke()
will stop the library from drawing an outline around any shapes that follow it. If you call
stroke()
after
noStroke()
, the screen will again draw lines.

1void loop(){
2
3 myScreen.stroke(255, 0, 0); // set the stroke color to red
4
5 myScreen.line(0, 10, myScreen.width(), 10); // draw a line across the screen
6
7 delay(1000);
8
9 myScreen.noStroke(); // don't draw a line around the next rectangle
10
11 myScreen.fill(0,255,0); // set the fill color to green
12
13 myScreen.rect(0,20,myScreen.width(),10); //draw a rectangle across the screen
14
15 delay(1000);
16
17 myScreen.fill(0,0,255); // set the fill color to blue
18
19 myScreen.stroke(255,255,255); // outline the rectangle with a white line
20
21 myScreen.rect(0,45,myScreen.width(),45); // draw a fat rectangle
22
23 delay(1000);
24
25 myScreen.background(0,0,0); // clear the screen before starting again
26
27 delay(1000);
28}

If you are using an Esplora, the structure of the program is the exact same. As the Esplora has a socket designed for the screen, and the pins for using the screen are fixed, an Esplora only object is created when targeting sketches for that board. You can reference the screen attached to an Esplora through

EsploraTFT
.

You do not need to declare any pins in your sketch; the object is instantiated for you automatically :

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3#include <Esplora.h>
4
5void setup(){
6
7 EsploraTFT.begin();
8
9 EsploraTFT.background(0,0,0); // clear the screen with black
10
11 delay(1000); // pause for dramatic effect
12}
13
14void loop(){
15
16 EsploraTFT.stroke(255, 0, 0); // set the stroke color to red
17
18 EsploraTFT.line(0, 10, EsploraLCD.width(), 10); // draw a line across the screen
19
20 delay(1000);
21
22 EsploraTFT.noStroke(); // don't draw a line around the next rectangle
23
24 EsploraTFT.fill(0,255,0); // set the fill color to green
25
26 EsploraTFT.rect(0,20,EsploraTFT.width(),20); //draw a rectangle across the screen
27
28 delay(1000);
29
30 EsploraTFT.fill(0,0,255); // set the fill color to blue
31
32 EsploraTFT.stroke(255,255,255); // outline the rectangle with a white line
33
34 EsploraTFT.rect(0,45,EsploraTFT.width(),50); // draw a fat rectangle
35
36 delay(1000);
37
38 EsploraTFT.background(0,0,0); // clear the screen before starting again
39
40 delay(1000);
41}

Movement across the screen

To give the illusion of motion, you need to quickly erase and draw images on the screen. When using Processing on a powerful computer, you can call

background()
every time through your
draw()
function to erase the window contests and dra objects in their new positions. The Arduino is not as fast, is it takes a little time to clear the screen when calling
background()
with the TFT library.

To create the illusion of motion, it's usually best to check if an object has moved each time through

loop()
. If it has, then you should draw over the object with your background color, then redraw the object in its new location. Because you're not updating all the pixels on the screen, it helps maintain the illusion of motion.

This example draws a single point, and has it bounce around on the screen. You'll set up the program in the same way you did previously, adding some variables to keep track of the point's current and previous locations, as well as the velocity and direction of the point.

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3
4#define CS 10
5#define DC 9
6#define RESET 8
7
8// pin definition for the Leonardo
9// #define CS 7
10// #define DC 0
11// #define RESET 1
12
13TFT myScreen = TFT(CS, DC, RESET);
14
15// initial position of the point is the middle of the screen
16// initial position of the point is the middle of the screen
17int xPos = 80;
18int yPos = 64;
19
20// direction and speed
21int xDir = 1;
22int yDir = 1;
23
24// variables to keep track of the point's location
25int xPrev = xPos;
26int yPrev = yPos;
27
28void setup(){
29
30 myScreen.begin();
31
32 myScreen.background(0,0,0); // clear the screen
33}

In @@loop()@ you'll first update the position of the dot by adding the direction to the x and y position variables. After that, check to see if there is a difference between the current and the previous locations of the point. If there is a difference, erase the previous location by filling in the dot the same color as the background, then drawing a new dot in the updated location. If the point happens to run into the boundaries of the screen, have it reverse direction.

1void loop(){
2
3 // update the location of the dot
4
5 xPos = xPos + xDir;
6
7 yPos = yPos + yDir;
8
9 // check if the current location is different than the previous
10
11 if(xPos != xPrev || yPos != yPrev){
12
13 myScreen.stroke(0,0,0); // set the stroke color to black
14
15 myScreen.point(xPrev, yPrev); // color in the previous point
16
17 }
18
19 // draw a point in the current location
20
21 myScreen.stroke(255,255,255);
22
23 myScreen.point(xPos, yPos);
24
25 // if the x or x position is at the screen edges, reverse direction
26
27 if(xPos >= 160 || xPos <= 0){
28
29 xDir = xDir*-1;
30
31 }
32
33 if(yPos >= 128 || yPos <= 0){
34
35 yDir = yDir*-1;
36
37 }
38
39 // update the point's previous location
40
41 xPrev=xPos;
42
43 yPrev=yPos;
44
45 // a 33ms delay means the screen updates 30 times a second
46
47 delay(33);
48
49}

The Esplora version is below :

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3#include <Esplora.h>
4
5// initial position of the point is the middle of the screen
6int xPos = 80;
7int yPos = 64;
8
9// direction and speed
10int xDir = 1;
11int yDir = 1;
12
13// variables to keep track of the point's location
14int xPrev, yPrev;
15
16void setup(){
17
18 EsploraTFT.begin();
19
20 EsploraTFT.background(0,0,0);
21}
22
23void loop(){
24
25 // update the location of the dot
26
27 xPos = xPos + xDir;
28
29 yPos = yPos + yDir;
30
31 // check if the current location is different than the previous
32
33 if(xPos != xPrev || yPos != yPrev){
34
35 EsploraTFT.stroke(0,0,0); // set the stroke color to black
36
37 EsploraTFT.point(xPrev, yPrev); // color in the previous point
38
39 }
40
41 // draw a point in the current location
42
43 EsploraTFT.stroke(255,255,255);
44
45 EsploraTFT.point(xPos, yPos);
46
47 // if the x or x position is at the screen edges, reverse direction
48
49 if(xPos >= 160 || xPos <= 0){
50
51 xDir = xDir*-1;
52
53 }
54
55 if(yPos >= 128 || yPos <= 0){
56
57 yDir = yDir*-1;
58
59 }
60
61 // update the point's previous location
62
63 xPrev=xPos;
64
65 yPrev=yPos;
66
67 // slight pause
68
69 delay(33);
70
71}

Draw some text

The TFT library includes a basic font for drawing text on screen. By default, characters are 5 pixels wide and 8 pixels tall. It is possible to change the font size to 10x16, 15x24, or 20x32. For additional information on the underlying font capabilities, see the Adafruit page on graphic primitives.

In this example, you'll create a basic counter that will update a number on screen every half second. As in the earlier examples, include the necessary libraries and variables before

setup()
.

In

setup()
send the static text that won't change to the screen. With
setTextSize()
you can increase the font size to make important parts stand out. Dynamic text for the screen should be stored in a char array. The String class makes it easy to update the text over time in the array.

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3
4#define CS 10
5#define DC 9
6#define RESET 8
7
8// pin definition for the Leonardo
9// #define CS 7
10// #define DC 0
11// #define RESET 1
12
13TFT myScreen = TFT(CS, DC, RESET);
14
15// variable to keep track of the elapsed time
16int counter = 0;
17// char array to print time
18char printout[4];
19
20void setup(){
21
22 myScreen.begin();
23
24 myScreen.background(0,0,0); // clear the screen
25
26 myScreen.stroke(255,0,255);
27
28 // static text
29
30 myScreen.text("Running for",0,0);
31
32 myScreen.text("seconds",0,30);
33
34 // increase font size for text in loop()
35
36 myScreen.setTextSize(3);
37}

In

loop()
, you'll get the current time, and store the number in a char array. Before each loop ends, erase the text you wrote earlier so it doesn't overwrite itself.

1void loop(){
2
3 // get elapsed time
4
5 counter = millis();
6
7 // convert to a string
8
9 String elapsedTime = String(counter/1000);
10
11 // add to an array
12
13 elapsedTime.toCharArray(printout,4);
14
15 // print out and erase
16
17 myScreen.stroke(255,255,255);
18
19 myScreen.text(printout,0,10);
20
21 delay(1000);
22
23 myScreen.stroke(0,0,0);
24
25 myScreen.text(printout,0,10);
26}

The Esplora code :

1#include <TFT.h> // Hardware-specific library
2#include <SPI.h>
3#include <Esplora.h>
4
5// variable to keep track of the elapsed time
6long counter = 0;
7// char array to print time
8char printout[4];
9
10void setup(){
11
12 EsploraTFT.begin();
13
14 EsploraTFT.background(0,0,0); // clear the screen
15
16 EsploraTFT.stroke(255,0,255);
17
18 // static text
19
20 EsploraTFT.text("Running for",0,0);
21
22 EsploraTFT.text("seconds",0,30);
23
24 // increase font size for text in loop()
25
26 EsploraTFT.setTextSize(3);
27}
28
29void loop(){
30
31 // get elapsed time
32
33 counter = millis();
34
35 // convert to a string
36
37 String elapsedTime = String(counter/1000);
38
39 // add to an array
40
41 elapsedTime.toCharArray(printout,4);
42
43 // print out and erase
44
45 EsploraTFT.stroke(255,255,255);
46
47 EsploraTFT.text(printout,0,10);
48
49 delay(1000);
50
51 EsploraTFT.stroke(0,0,0);
52
53 EsploraTFT.text(printout,0,10);
54}

Draw an image from the SD card

The TFT library has the ability to read .bmp files off a SD card and display them on the screen. Images can be smaller or larger than the screen resolution (160x128), but there is no method on the Arduino for image manipulation. The images should be sized before you put them on the SD card.

In the following example, a bitmap that is 160x128 pixels named "arduino.bmp" is in the root directory of a SD card. When read by the library and drawn, the image will fill the screen.

In addition to the libraries you have been including to this point, you will also need to include the SD library. You'll also need to declare a CS pin for the SD slot.

The PImage class is used to load the image and can also check if the image is a valid file that the library can read.

Once read, the image will be rendered from the coordinates you decide. In this case, it starts drawing from the top left of the screen.

1// include the necessary libraries
2#include <SPI.h>
3#include <SD.h>
4#include <TFT.h> // Hardware-specific library
5
6// pin definition for the Uno
7#define SD_CS 11
8#define LCD_CS 10
9#define DC 9
10#define RESET 8
11
12// pin definition for the Leonardo
13// #define SD_CS 8
14// #define LCD_CS 7
15// #define DC 0
16// #define RESET 1
17
18TFT myScreen = TFT(LCD_CS, DC, RESET);
19
20// this variable represents the image to be drawn on screen
21
22PImage image;
23
24void setup() {
25
26 // initialize the serial port
27
28 Serial.begin(9600);
29
30 while (!Serial) {
31
32 // wait for serial line to be ready
33
34 // needed for the Leonardo
35
36 }
37
38 // try to access the SD card
39
40 Serial.print("Initializing SD card...");
41
42 if (!SD.begin(SD_CS)) {
43
44 Serial.println("failed!");
45
46 return;
47
48 }
49
50 Serial.println("OK!");
51
52 // initialize and clear the GLCD screen
53
54 myScreen.begin();
55
56 myScreen.background(255, 255, 255);
57
58 // load the image from the SD card
59
60 image = myScreen.loadImage("arduino.bmp");
61
62 // check if the image loaded properly
63
64 if (image.isValid() != true) {
65
66 Serial.println("error while loading arduino.bmp");
67
68 }
69
70 //write the image on screen
71
72 myScreen.image(image, 0, 0);
73}
74
75void loop(){
76// nothing happening here
77}

For the Esplora :

1// include the necessary libraries
2#include <SPI.h>
3#include <SD.h>
4#include <TFT.h> // Hardware-specific library
5#include <Esplora.h>
6
7// SD Chip Select pin
8#define SD_CS 8
9
10// this variable represents the image to be drawn on screen
11
12PImage image;
13
14void setup() {
15
16 // initialize the serial port
17
18 Serial.begin(9600);
19
20 while (!Serial) {
21
22 // wait for serial line to be ready
23
24 }
25
26 // try to access the SD card
27
28 Serial.print("Initializing SD card...");
29
30 if (!SD.begin(SD_CS)) {
31
32 Serial.println("failed!");
33
34 return;
35
36 }
37
38 Serial.println("OK!");
39
40 // initialize and clear the GLCD screen
41
42 EsploraTFT.begin();
43
44 EsploraTFT.background(255, 255, 255);
45
46 // load the image from the SD card
47
48 image = EsploraTFT.loadImage("arduino.bmp");
49
50 // check if the image loaded properly
51
52 if (image.isValid() != true) {
53
54 Serial.println("error while loading arduino.bmp");
55
56 }
57
58 //write the image on screen
59
60 EsploraTFT.image(image, 0, 0);
61}
62
63void loop(){
64// nothing happening here
65}

Connecting to Other Arduino Boards

Even if the screen's headers are designed to fit into the socket on the front of the Arduino Esplora or the Arduino Robot but, this module is compatible with any AVR-based Arduino (UNO, Leonardo, etc...) or with the Arduino Due. If you want to use one these other boards, some slight changes on connections are required.

The pins on the screen.
The pins on the screen.

You can either connect the screen with hardware SPI pins, or define your own set of pins. Using the hardware SPI is faster when drawing to the screen.

Arduino Uno

Connect power and ground to the breadboard.

Connecting the breadboard to the board.
Connecting the breadboard to the board.

Connect the screen to the breadboard. The headers on the side of the screen with the small blue tab and arrow should be the ones that attach to the board. Pay attention to the orientation of the screen, in these images, it is upside down.

Connecting the screen to the breadboard and board.
Connecting the screen to the breadboard and board.

Connect the pins following this default configuration:

+5V:+5V
MISO:pin 12
SCK:pin 13
MOSI:pin 11
LCD CS:pin 10
SD CS:pin 4
D/C:pin 9
RESET:pin 8
BL:+5V
GND:GND

Circuit of the UNO and the TFT screen.
Circuit of the UNO and the TFT screen.

Connecting the pins in the proper way, you can see the lcd screen working with your Uno (or Duemilanove) just uploading the simple "TFTBitmapLogo" sketch.

Photo of the circuit of the UNO and the TFT screen.
Photo of the circuit of the UNO and the TFT screen.

Arduino Leonardo & Arduino Yún

The Arduino Leonardo & Arduino Yún use different pins to be compatible with the lcd screen. To set the pins MISO, MOSI and SCK, you have to use the ICSP terminals.

+5V:+5V
MISO:Miso pin (white wire on ICSP)
SCK:Sck pin (brown wire on ICSP)
MOSI:Mosi pin (yellow wire on ICSP)
LCD CS:pin 7
SD CS:pin 8
D/C:pin 0
RESET:pin 1
BL:+5V
GND:GND

The image below shows an Arduino Leonardo but it works for an Arduino Yún too.

Circuit of the Leonardo and the TFT screen.
Circuit of the Leonardo and the TFT screen.

The screen will show this message: "Arduino TFT Bitmap Example. Open serial monitor to run the sketch". Open the serial monitor to view the Arduino Logo.

Arduino Mega 2560 or Mega ADK

To connect the lcd screen to a Mega board, use this pin configuration:

+5V:+5V
MISO:50 on Mega 2560 (Miso on ADK)
SCK:52 on Mega 2560 (Sck on ADK)
MOSI:51 on Mega 2560 (Mosi on ADK)
LCD CS:pin 10
SD CS:pin 4
D/C:pin 9
RESET:pin 8
BL:+5V
GND:GND

Circuit of the Mega and the TFT screen.
Circuit of the Mega and the TFT screen.

Arduino Due

To connect the lcd screen to an Arduino Due, use this pin configuration and don't forget to set the right value for the variable "sd_cs" (

#define sd_cs 7
) in the sketch:

+5V:+3.3V
MISO:Miso pin (white wire on SPI)
SCK:Sck pin (brown wire on SPI)
MOSI:Mosi pin (yellow wire on SPI)
LCD CS:pin 10
SD CS:pin 7
D/C:pin 9
RESET:pin 8
BL:+3.3V
GND:GND

Circuit of the Due and the TFT screen.
Circuit of the Due and the TFT screen.

Next steps

Now that you have tested the basic functionality of the screen, see the TFT library pages for information about the library's API and additional examples. It's also recommended to visit the Adafruit graphics library page for additional information on functions not covered.

The text of the Arduino getting started guide is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the guide are released into the public domain.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.