Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
100 min
Share

Display the distance value on a TFT Display with arduino

In this tutorial, we will learn how to use an ultrasonic sensor to measure distance and display the resulting values on a TFT Display. You can modify this project to make a self driving car.

Project Video

Overview

If an object’s position is constantly changing, measuring its distance from you can be a challenging task. However, one potential solution to this problem is to use an ultrasonic sensor.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
2.8 inch Touch Screen Module
Get Item
Ultrasonic Sensor (HC-SR04)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

It is important to connect resistors with a value of 10 kilo ohms to the pins of the TFT Display, as shown in the image.

Connect the wires between the TFT Display and the Arduino and the ultrasonic sensor as shown in the image below.

Connections from the Arduino to the breadboard:

  • Arduino GND pin → Breadboard ground line

   • Arduino 5V pin → Breadboard 5V line

 

Connections from the Arduino to the ultrasonic sensor:

 

   • Arduino pin 3 → ultrasonic sensor Trig pin

  • Arduino pin 2 → ultrasonic sensor Echo pin

 

Connections from the ultrasonic sensor to the breadboard:

 

   • ultrasonic sensor VCC pin → Breadboard 5V line

   • ultrasonic sensor GND pin → Breadboard ground line

 

Connections from the TFT Display to the breadboard:

 

    • TFT Display VCC pin → Breadboard 5V line

   • TFT Display GND pin → Breadboard ground line

    • TFT Display LED pin → Breadboard 5V line

 

Connections from the l298n motor driver to arduino :

 

   • TFT Display CS pin → Arduino pin 10 

   • TFT Display RST pin → Arduino pin 8

   • TFT Display DC pin → Arduino pin 9

  • TFT Display MOSI pin → Arduino pin 11

   • TFT Display SCK pin → Arduino pin 13

   • TFT Display MISO pin → Arduino pin 12

Coding


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:

Tutorial: Display the distance value on a TFT Display with arduino!
Wire Library:
Adafruit_GFX Library:
Wire Library:


The purpose of this sketch is to display the distance values obtained from the ultrasonic sensor on the TFT Display.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line

Connections from the ultrasonic sensor:

• ultrasonic sensor VCC pin → Breadboard 5V line

• ultrasonic sensor GND pin → Breadboard ground line

• ultrasonic sensor Trig pin → Arduino pin 3

• ultrasonic sensor Echo pin → Arduino pin 2

Connections from the TFT Display to the breadboard:

• TFT Display VCC pin → Breadboard 5V line

• TFT Display GND pin → Breadboard ground line

• TFT Display LED pin → Breadboard 5V line

Connections from the l298n motor driver to arduino :

• TFT Display CS pin → Arduino pin 10

• TFT Display RST pin → Arduino pin 8

• TFT Display DC pin → Arduino pin 9

• TFT Display MOSI pin → Arduino pin 11

• TFT Display SCK pin → Arduino pin 13

• TFT Display MISO pin → Arduino pin 12

*/

#include "Adafruit_GFX.h"    
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include  "SPI.h"

//Define pin numbers
#define TFT_DC 9              
#define TFT_CS 10            
#define TFT_RST 8            
#define TFT_MISO 12          
#define TFT_MOSI 11          
#define TFT_CLK 13          

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

char DISTANCEChar[10];

//Define pin numbers
#define trig 3
#define echo 2
//Defines variables and initialize their values
float distance =0;
int t =0;
 
void setup(){
 
Serial.begin(9600);
tft.begin();                      
tft.setRotation(0);            
tft.fillScreen(ILI9341_BLACK);
//Sets the trig Pin as an output
pinMode(trig,OUTPUT);
//Sets the echo Pin as an intput
pinMode(echo,INPUT);
Wire.begin();
printText("DISTANCE", ILI9341_GREEN,20,130,3);

}

void loop()
{
//Clears the trig pin
digitalWrite(trig,LOW);
//delay for 5 micro seconds
delayMicroseconds(5);
//Sets the trig Pin HIGH for 10 micro seconds
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
//Reads the echoPin, returns the sound wave travel time in microseconds
t =pulseIn(echo,HIGH);
//Calculating the distance
distance =(t /57);
//Convert float to string
String distanceString = String(distance,1);
distanceString.toCharArray(DISTANCEChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the Distance value on the TFT Display
printText(DISTANCEChar, ILI9341_WHITE,50,180,4);
printText("Cm", ILI9341_WHITE,180,180,4);
delay(500);

}
//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
 tft.setCursor(x, y);
 tft.setTextColor(color);
 tft.setTextSize(textSize);
 tft.setTextWrap(true);
 tft.print(text);
}

Testing it Out

Once you’ve uploaded the code to the Arduino board, you will find that the TFT Display displays the distance value as shown in the GIF below.

Resources

No items found.