Sunday, April 29, 2018

Displaying Images on 3.2 Inch TFT using Arduino

In this post, I will show you guys how to display images on 3.2 Inch TFT Using Arduino Mega as shown below.
Avengers Image Display on TFT
You have two choices either watch the following video tutorial or read the post given below.
Prerequisite 
Download the following three libraries which are required for this project.
* UTFT or Universal TFT Library
* SdFAT Library
* UTFT_SdRaw Library

UTFT Library
This library contains all the API's to initialize the TFT controller and display data on the TFT screen, this library also comes with a tool which is used to convert the images into *.c code and *.raw file. This tool can also resize the images according to the size of the TFT.
Reason for choosing *.raw image file format as compared to other *.bmp, *.jpeg, *.png etc format is to speed-up the process of displaying images, as other formats requires converter or decoder which increases the code size and processing time on Arduino devices.
This tool is available under the following path.
\Arduino\libraries\UTFT\Tools\ImageConverter565.exe
Click Here to download the library.

SdFat Library
This library is used to read and write data/files on the Sd Card.

UTFT_SdRaw
This library is used to display raw format images from Sd Card on TFT.

NOTE:
One more important thing i want to tell you guys, that do not use tinyFAT library, it is really good but only for FAT16 formatted SD card, and in today's world we usually have 8GB/16GB/32GB/64GB/128GB memory cards, and this is the reason it doesn't work on Sd card cards above 2GB size.

I have downloaded posters of most of the characters from the movie Avengers:Infinity War and as a first step these all images will be converted into *.raw format using the tool described above. The size I am going to use is 320X240 as the TFT I have with me is of 3.2 Inch.
Image to RAW File
Once all the files are converted, copy all the *.raw format files onto your memory card, and now it's time to start writing the Arduino code to display images on 3.2 Inch TFT.

#include <SPI.h>
#include <SdFat.h>
#include <UTFT.h>
#include <UTFT_SdRaw.h>

#define SD_CHIP_SELECT  53  // SD chip select pin(Arduino Mega)
SdFat sd;

UTFT myGLCD(ILI9341_16, 38, 39, 40, 41);
UTFT_SdRaw myFiles(&myGLCD);

void setup()
{
  Serial.begin(115200);
  delay(100);
  bool mysd = 0;
  while (!mysd)
  {
    if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED))
    {
      Serial.println(F("Card failed, or not present"));
      Serial.println(F("Retrying...."));
    }
    else
    {
      mysd = 1;
      Serial.println(F("Card initialised."));
    }
  }
  Serial.println(F("Initialising LCD."));
  myGLCD.InitLCD();
  myGLCD.clrScr();
}

void loop()
{
  myFiles.load(0, 0, 320, 240, "inifinity_war.RAW", 1, 0);
  while(1);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "black_panther.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "black_widow.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "bucky.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "captain_america.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "doctor_strange.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "drax.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "falcon.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "gamora.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "groot_rocket.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "hulk.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "iron_man.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "mantis.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "scarlet_witch.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "shuri.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "spider_man.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "star_lord.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "thor.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "vision.RAW", 1, 0);
  delay(4000);
  myFiles.load(0, 0, 320, 240, "war_machine.RAW", 1, 0);
  delay(4000);
}

The above program is really simple and is divided into three main parts.
* SD Card Initialization
* TFT Initialization (TFT I am using is based on ILI9341 controller and is operated using 16-bit mode, this is the reason I used ILI9341_16 while creating object for TFT, make sure to update it as per your TFT controller).
* The last step is to display the images from SD card using function load.

After running the following program, we see the following output on TFT display.

Click Here to download the complete image set and Arduino Program.

3 comments:

  1. hey hii thanks for your brief information I am using 3.5" FTF screen with UNO board but after uploading the code to UNO nothing will display

    could you please help me out in this regards

    ReplyDelete
    Replies
    1. This code will not work unless you change your lcd UTFT myGLCD.
      The tft I am using is working in 16 bit mode but from your message it looks like that you are using tft with spi mode.
      Please select your tft wisely.

      Delete
  2. Dear Embedded Laboratory!

    This code works. Thank you!
    You have been trying to play picture and sound at once on an sd card. I don't success, the picture break down. Can you help me?

    Péter

    ReplyDelete