Home > Arduino Sketch, Driving Displays, Exploring Ideas, Hardware testing > Simple Graph With Arduino NANO and TFT Library

Simple Graph With Arduino NANO and TFT Library

This is a simple plotting example to experiment with the TFT Library and the LCD Display with the ST7735 controller (128×160 pixels).


First, a clean wiring diagram to connect the Arduino NANO and the Display. The SD Card adapter was wired  (CS and MISO) but not used in this Sketch.


Then, a wiring plan to put them together in the breadboard:





The Sketch:


/*TFT_Graph.ino      Arduining 7 AUG 2016

-Arduino NANO and ST7735 LCD Display 128×160
-using TFT library
-Brighness control in pin 6
-offset is used to adjust vertical position of the graph.

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

//pin definitions:
#define CS          10
#define DC           9
#define RST          8
#define BKLIGHT      6              // Backlight control pin

#define BRIGHTNESS 204              // Backlight intensity (0-255)
#define RANGE      100              // Vertical size of the graph in pixels.
#define WIDTH      128              // Horizontal size of Display in pixels.
#define HEIGHT     160              // Vertical size of Display in pixels.
#define PERSIST    500              // persistence of the graph (milliseconds).

TFT TFTscreen = TFT(CS, DC, RST);

int value ;
int last_value;
int x_pos= 0;                       // cursor position
int offset= 40;                     // vertical graph displacement in pixels.

void setup(){
TFTscreen.begin();                // initialize the display
TFTscreen.background(0,128,0);    // dark green background
TFTscreen.stroke(255,255,255);    // white stroke
analogWrite(BKLIGHT,BRIGHTNESS);  //Set brightness.

void drawFrame(){
TFTscreen.stroke(255,255,255);      // white stroke
TFTscreen.fill( 255, 255, 255 );    // White fill
TFTscreen.rect(0, 0, WIDTH, 21);
TFTscreen.stroke(0,0,0);            // black text
TFTscreen.text(“Volts: 1.0V/Div”,2,2);  // Example text.
TFTscreen.text(“Time:  10mS/Div”,2,12); // Exanple text
void clearGraph(){
TFTscreen.stroke(127,127,127);      // grey for the border
TFTscreen.fill( 180, 0, 0 );        // dark Blue for the background
TFTscreen.rect(0, 22, 128, 128);
TFTscreen.line(65, 23, 65, 151);    // vertical line
TFTscreen.line(1, 85,127, 85);      // horizontal line
TFTscreen.stroke(0,255,255);        // yellow stroke for plotting.

void plotLine(int last,int actual){
if(x_pos==0)last= actual;                     // first value is a dot.
TFTscreen.line(x_pos-1, last, x_pos, actual); // draw line.
if(x_pos > WIDTH){

void loop(){
last_value= value;
value= analogRead(A0);
value= map(value,0,1023,149,23);
value -= offset;
if(value <= 23) value= 23;         //truncate off screen values.
plotLine(last_value ,value );


Thanks for visiting…

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: