Note: this page refers to a product that is retired.

TFTDisplayText

Read the value of a sensor and print it on the screen.

TFT Display Text

GLCD DisplayTextDemo

This example demonstrates how to draw text on the Arduino GLCD screen when connected to an Arduino. The Arduino reads the value of an analog sensor attached to pin A0, and writes the value to the LCD screen, updating every quarter second.

Hardware Required

  • Arduino Uno

  • Arduino TFT screen

  • breadboard

  • hookup wire

  • one 10-kilohm potentiometer

Circuit

Connect power and ground to the breadboard.

GLCD logo1

Place the potentiometer on the breadboard. Connect one side to ground, and the other to power. Connect the middle pin to A0.

GLCD text2

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.

GLCD text3

Connect the BL and +5V pins to power, and GND to ground. Connect CS-LD to pin 10, DC to pin 9, RESET to pin 8, MOSI to pin 11, and SCK to pin 13. If you're using a Leonardo, you'll be using different pins. see the getting started page for more details.

GTFT text4 small Click the image for a larger version

Code

To use the screen you must first include the SPI and TFT libraries.

1#include <SPI.h>
2#include <TFT.h>

Define the pins you're going to use for controlling the screen, and create an instance of the TFT library named

TFTscreen
. You'll reference that object whenever you're working with the screen.

1#define cs 10
2#define dc 9
3#define rst 8
4
5TFT TFTscreen = TFT(cs, dc, rst);

To update the screen with text, you'll need to store dynamic content in a char array.

char sensorPrintout[4];

In

setup()
, initialize the screen and clear the background. Set the color for the font with
stroke()
, and write any static text to the screen. In this case, you'll write "Sensor Value :". This will stay at the top of the screen and not change as long as the sketch runs. Before entering the
loop()
, set the text size so you can really see the sensor values stand out.

1void setup() {
2
3 TFTscreen.begin();
4
5 TFTscreen.background(0, 0, 0);
6
7 TFTscreen.stroke(255,255,255);
8
9 TFTscreen.setTextSize(2);
10
11 TFTscreen.text("Sensor Value : ",0,0);
12
13 TFTscreen.setTextSize(5);
14}

In

loop()
, read the value from the potentiometer and store it in a string. Convert the string content to a char array, storing it in the global array you declared int he beginning of your program.

1void loop() {
2
3 String sensorVal = String(analogRead(A0));
4
5 sensorVal.toCharArray(sensorPrintout, 4);

Set the text color (this would be a good place to change the color of the text depending on the value from the potentiometer), and print it to the screen below the static text.

1TFTscreen.stroke(255,255,255);
2
3 TFTscreen.text(sensorPrintout, 0, 20);

Wait a quarter of a second, then erase the text you just wrote, so you can update it in the next run through

loop()
.

1delay(250);
2
3 TFTscreen.stroke(0,0,0);
4
5 TFTscreen.text(sensorPrintout, 0, 20);
6}

The complete sketch is below :

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.