3.5 TFT LCD Display Image from SD Card HELP!

Hello there..

I am using an Arduino Uno and a 3.5 TFT LCD with SD card.

The only problem i have is i am using the SD card on my Display and i want a simple code to set my image as an background so i can use the image with toutchscreen.. The point is after toutching it it will go to a screen with an other image..

Can some one please help so i can display an image in back ground

Here is the code so far:

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Define some TFT readable colour codes to human readable names
#define BLACK 0x0000 /* 0, 0, 0 /
#define BLUE 0x001F /
0, 0, 255 /
#define RED 0xF800 /
255, 0, 0 /
#define GREEN 0x07E0 /
0, 255, 0 /
#define CYAN 0x07FF /
0, 255, 255 /
#define MAGENTA 0xF81F /
255, 0, 255 /
#define YELLOW 0xFFE0 /
255, 255, 0 /
#define WHITE 0xFFFF /
255, 255, 255 /
#define NAVY 0x000F /
0, 0, 128 /
#define DARKGREEN 0x03E0 /
0, 128, 0 /
#define DARKCYAN 0x03EF /
0, 128, 128 /
#define MAROON 0x7800 /
128, 0, 0 /
#define PURPLE 0x780F /
128, 0, 128 /
#define OLIVE 0x7BE0 /
128, 128, 0 /
#define LIGHTGREY 0xC618 /
192, 192, 192 /
#define DARKGREY 0x7BEF /
128, 128, 128 /
#define ORANGE 0xFD20 /
255, 165, 0 /
#define GREENYELLOW 0xAFE5 /
173, 255, 47 /
#define PINK 0xF81F /
255, 0, 255 */

#define REDBAR_MINX 80
#define GREENBAR_MINX 130
#define BLUEBAR_MINX 180
#define BAR_MINY 30
#define BAR_HEIGHT 250
#define BAR_WIDTH 30

#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin

// from Kuman_SC3A
#define TS_MINX 85
#define TS_MAXX 925
#define TS_MINY 135
#define TS_MAXY 910

#define MINPRESSURE 10
#define MAXPRESSURE 1000

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define PALETTEDEPTH 0 // do not support Palette modes

void setup(void) {
Serial.begin(9600);
Serial.print(F("Finding out the type of TFT LCD... ID is : "));
tft.reset();
uint16_t identifier = tft.readID();
Serial.print(identifier, HEX);
tft.begin(identifier);
Serial.print(". TFT size is : "); Serial.print(tft.width()); Serial.print(" x "); Serial.println(tft.height());
tft.setRotation(1);
//tft.invertDisplay(1); // UNCOMMENT THIS IF COLORS get inverted
tft.fillScreen(BLACK);

Serial.println("Drawing Init Screen...");
drawInitScreen();
delay(100);

}

void loop(void) {

// i want a single image displayed from the sd card that is attached to my TFT screen

}

P.S::

Can you make the image fullscreen: 480x320?

There is an example that reads .BMP files from SD. (it handles several styles of .BMP format)

The function will draw a pcture by filename at coordinates X, Y.

The sketch finds all the filenames in a directory. But you know which filenames you want.

Quite honestly, if you know the filenames in advance you probably know the size and shape too.
So you might just as well create a RAW image. This requires a very simple function to display.

David.

Yeah i got svral RAW images that i named 001.RAW and 002.RAW,

But to be Honest i diden't get them to work with my Arduino uno and 3.5 TFT LCD Screen.. If you could help: the image size is 320x480.

But when i tried the same tutorial on youtube for this code.. it diden't work for me..

So if you could help me out with an Example code it would be great :slight_smile:

You can use the example showBMP() function e.g. showBMP("001.BMP", 0, 0);

Or use a showRAW() function e.g. showRAW(0, 0, "001.RAW", 480, 320);

The .BMP file contains information about colours, width, height etc. and can display the image at any X, Y position on the screen.
The .RAW file does not contain any information. You have to tell showRAW() the width and height. You have to be sure that it will fit on your screen at X, Y.

const int RAWPIXELS = 128;

uint8_t showRAW(int x, int y, const char *nm, int w, int h)
{
    uint8_t first = 1;
    int32_t n = (int32_t)w * h;  //size of RAW image in pixels
    uint8_t buf[RAWPIXELS * 2];      // size in bytes
    File inFile = SD.open(nm);
    if (!inFile) return 1;    //can't open
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    while (n > 0) {
        int cnt = RAWPIXELS;
        if (n < cnt) cnt = n;
        n -= cnt;
        inFile.read(buf, cnt * 2);
        tft.pushColors(buf, cnt, first);
        first = 0;
    }
    inFile.close();
    return 0;
}

David.

Edit. I had omitted the setAddrWindow() statement

The image won't show up on my 3.5 tft screen.
I added an yellow rectangle to see if i got the right size for the screen and it shows up..

maybe the RAW files diden't work for me so i tried using the showBMP function..
but it declared it wasen't in scope...

Here is what i got so far

#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Define some TFT readable colour codes to human readable names
#define BLACK 0x0000 /* 0, 0, 0 /
#define BLUE 0x001F /
0, 0, 255 /
#define RED 0xF800 /
255, 0, 0 /
#define GREEN 0x07E0 /
0, 255, 0 /
#define CYAN 0x07FF /
0, 255, 255 /
#define MAGENTA 0xF81F /
255, 0, 255 /
#define YELLOW 0xFFE0 /
255, 255, 0 /
#define WHITE 0xFFFF /
255, 255, 255 /
#define NAVY 0x000F /
0, 0, 128 /
#define DARKGREEN 0x03E0 /
0, 128, 0 /
#define DARKCYAN 0x03EF /
0, 128, 128 /
#define MAROON 0x7800 /
128, 0, 0 /
#define PURPLE 0x780F /
128, 0, 128 /
#define OLIVE 0x7BE0 /
128, 128, 0 /
#define LIGHTGREY 0xC618 /
192, 192, 192 /
#define DARKGREY 0x7BEF /
128, 128, 128 /
#define ORANGE 0xFD20 /
255, 165, 0 /
#define GREENYELLOW 0xAFE5 /
173, 255, 47 /
#define PINK 0xF81F /
255, 0, 255 */

#define REDBAR_MINX 80
#define GREENBAR_MINX 130
#define BLUEBAR_MINX 180
#define BAR_MINY 30
#define BAR_HEIGHT 250
#define BAR_WIDTH 30

#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin

// from Kuman_SC3A
#define TS_MINX 85
#define TS_MAXX 925
#define TS_MINY 135
#define TS_MAXY 910

#define MINPRESSURE 10
#define MAXPRESSURE 1000

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define PALETTEDEPTH 0 // do not support Palette modes

const int RAWPIXELS = 128;

uint8_t showRAW(int x, int y, const char *nm, int w, int h)
{
uint8_t first = 1;
int32_t n = (int32_t)w * h; //size of RAW image in pixels
uint8_t buf[RAWPIXELS * 2]; // size in bytes
File inFile = SD.open(nm);
if (!inFile) return 1; //can't open
while (n > 0) {
int cnt = RAWPIXELS;
if (n < cnt) cnt = n;
n -= cnt;
inFile.read(buf, cnt * 2);
tft.pushColors(buf, cnt, first);
first = 0;
}
inFile.close();
return 0;
}

void setup(void) {
Serial.begin(9600);
Serial.print(F("Finding out the type of TFT LCD... ID is : "));
tft.reset();
uint16_t identifier = tft.readID();
Serial.print(identifier, HEX);
tft.begin(identifier);
Serial.print(". TFT size is : "); Serial.print(tft.width()); Serial.print(" x "); Serial.println(tft.height());
tft.setRotation(1);
//tft.invertDisplay(1); // UNCOMMENT THIS IF COLORS get inverted
tft.fillScreen(RED);
Serial.println("Drawing Init Screen...");

delay(100);

}

void loop(void) {

tft.setCursor(0,0);
tft.fillRoundRect(0, 0, 480, 320, 8, ORANGE);
showRAW(0, 0, "001.RAW", 480, 320);
delay(500);
showRAW(0, 0, "003.raw", 480, 320);
showBMP("01.BMP", 0, 0);
}

Have you run the drawBitmap_kbv example?

It shows how to make a .RAW file e.g. via online tool Rinky-Dink Electronics

The example shows it as a C file. But you can make a RAW file too.
You store RAW files on SD card. And display with the function I posted earlier.
You store C files as "source code" in a separate sketch tab

It is difficult to know what to put in examples.
I could make each example a "one-off". i.e. one image, one function.

Or I can show several images with different ways to display them. This is more comp[lex.
That particular example is intended to show that Adafruit_GFX has lots of useful methods.
You just need to try them for yourself.

David.

Okay.. So here is what i have done..

I converted my image to an RAW File... Than i made a second tab and stored the image as an C-File..
then i added a new tab called 001. and paste the code in the source file..

After i posted in the code.. But when i try to upload the code to arduino it says:

Unexpected unqualified-id before nummeric constraint..

at the following line

const unsigned short 001[153600] PROGMEM={

also do i need to define this in the main source code as well?

This is what i put in the second tab called 001

// Generated by : ImageConverter 565 Online
// Generated from : 001.png
// Time generated : Tue, 10 Sep 19 18:16:34 +0200 (Server timezone: CET)
// Image Size : 320x480 pixels
// Memory usage : 307200 bytes

#if defined(AVR)
#include <avr/pgmspace.h>
#elif defined(PIC32MX)
#define PROGMEM
#elif defined(arm)
#define PROGMEM
#endif

const unsigned short 001[153600] PROGMEM={
0x19E7, 0x1165, 0x1165, 0x1165, 0x1165, 0x11C7, 0x1186, 0x0945, 0x0945, 0x0945, 0x11C7, 0x11C7, 0x0965, 0x0945, 0x0945, 0x11A6, // 0x0010 (16) pixels
0x11C7, 0x0945, 0x0965, 0x0965, 0x1186, 0x19E7, 0x1165, 0x0945, 0x1165, 0x1165, 0x19E7, 0x1186, 0x1145, 0x1165, 0x0965, 0x11C7, // 0x0020 (32) pixels
0x11A6, 0x0945, 0x0945, 0x0945, 0x11A6, 0x19E7, 0x1165, 0x1165, 0x0945, 0x1186, 0x2249, 0x1186, 0x0945, 0x0965, 0x0965, 0x2249, // 0x0030 (48) pixels
0x19E7, 0x0945, 0x0965, 0x0945, 0x1A07, 0x1A28, 0x0945, 0x0945, 0x0945, 0x11C6, 0x2269, 0x1185, 0x0945, 0x0945, 0x1165, 0x2269, // 0x0040 (64) pixels
0x11E7, 0x0945, 0x0965, 0x0945, 0x1A08, 0x1A28, 0x0965, 0x1165, 0x0945, 0x1186, 0x1A28, 0x1186, 0x0945, 0x0944, 0x0945, 0x1A28, // 0x0050 (80) pixels
0x2249, 0x1165, 0x0945, 0x0945, 0x11C7, 0x2269, 0x1186, 0x0945, 0x0945, 0x1165, 0x2249, 0x1A08, 0x1165, 0x1145, 0x1165, 0x1A07, // 0x0060 (96) pixels
0x2249, 0x1186, 0x0945, 0x1165, 0x11A6, 0x1A28, 0x11C7, 0x1165, 0x1145, 0x1165, 0x1A07, 0x1A28, 0x11A6, 0x1165, 0x1165, 0x1185, // 0x0070 (112) pixels
0x19E7, 0x11C7, 0x0965, 0x0965, 0x0945, 0x11A6, 0x1A08, 0x11C7, 0x1165, 0x0945, 0x1185, 0x11C7, 0x11E7, 0x1165, 0x0945, 0x1165, // 0x0080 (128) pixels
0x11A6, 0x19E7, 0x11C7, 0x1165, 0x0945, 0x1165, 0x11C7, 0x19E7, 0x1186, 0x1145, 0x1165, 0x11A6, 0x11C7, 0x11C7, 0x1165, 0x0965, // 0x0090 (144) pixels
0x1185, 0x11C6, 0x19E7, 0x11A6, 0x1165, 0x1165, 0x1165, 0x1186, 0x11C6, 0x1186, 0x0945, 0x0965, 0x1165, 0x11A6, 0x11C7, 0x1165, // 0x00A0 (160) pixels
0x0945, 0x0965, 0x1186, 0x11A6, 0x11A6, 0x1165, 0x1165, 0x1165, 0x1186, 0x11A6, 0x1186, 0x0945, 0x0945, 0x1186, 0x1186, 0x11A6, // 0x00B0 (176) pixels
0x1186, 0x0945, 0x0965, 0x1186, 0x11A6, 0x11C7, 0x1165, 0x0965, 0x1185, 0x11A6, 0x11C7, 0x1186, 0x1165, 0x0945, 0x0965, 0x11A6, // 0x00C0 (192) pixels
0x1186, 0x1165, 0x0965, 0x0965, 0x1185, 0x1185, 0x1165, 0x0945, 0x1165, 0x1186, 0x1186, 0x1186, 0x1165, 0x0945, 0x1165, 0x1185, // 0x00D0 (208) pixels
0x1165, 0x1186, 0x1165, 0x1165, 0x1186, 0x0965, 0x1165, 0x1185, 0x0965, 0x11C7, 0x1165, 0x0945, 0x1185, 0x1185, 0x11A6, 0x1186, // 0x00E0 (224) pixels
0x0945, 0x0945, 0x1165, 0x11A6, 0x11C7, 0x1165, 0x0945, 0x0945, 0x0965, 0x11C7, 0x1185, 0x0965, 0x0965, 0x0945, 0x11A6, 0x11A6, // 0x00F0 (240) pixels
0x0945, 0x0945, 0x0945, 0x1186, 0x11C6, 0x0965, 0x0965, 0x0965, 0x1165, 0x11C6, 0x1165,

Ect.

First off. Please read the Forum instructions. Use Code Tags or attach files.

A RAW file stored on your SD will be 307200 bytes (153600 pixels)
A C file will be about over 1 million bytes.

You can only fit a smallish bitmap in a C array. The AVR compiler only allows arrays < 32768 bytes.

You should be storing RAW files on your PC. And then copying to SD disk. They will always be 307200 bytes.

David.

Yes, i stored the 001.RAW file on my SD card.. i will send u the image as an attachment so you could edit my code for an working one? because i only get an black screen.. maybe the image too big? or did i do something wrong in the code i showed you.. I deleted the C-file.. tab

i will send you the png image i converted to a RAW file with the link you gave me and uploaded it to sd card..

001.png

Aussies might expect upside-down images.

I don't know anyone who can read mirrored text.

Is that the image you want to display on your TFT ?

You can put your 001.RAW file into a ZIP. Then attach the ZIP file.

David.

Alright just as you mentiont i attached the zip file containing the .raw file i edit the name to 002.. but that shouldent be a problem.

001.zip (154 KB)

Thanks for your ZIP. It displays fine on a Uno (as 002.raw )

Note that your 002.raw is stored as a 320x480 i.e. Portrait.
My 001.raw and 003.raw are Portrait and Landscape respectively.

I suggest that you create a few RAW files for yourself. You can rotate your JPG or PNG images on the PC. Most "convert565" programs will just use the shape and size of PNG that you give it.

#include <SPI.h>            // f.k. for Arduino-1.5.2
#include <SD.h>             // Use the official SD library on hardware pins

#include <Adafruit_GFX.h>   // Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define SD_CS 10

void setup()
{
    uint16_t ID;
    Serial.begin(9600);
    Serial.print("Show RAW files on TFT with ID:0x");
    ID = tft.readID();
    Serial.println(ID, HEX);
    tft.begin(ID);
    bool good = SD.begin(SD_CS);
    if (!good) {
        Serial.print(F("cannot start SD"));
        while (1);
    }
}

void loop()
{
    tft.setRotation(2);
    showRAW(0, 0, "001.raw", 320, 480);
    tft.setRotation(0);
    showRAW(0, 0, "002.raw", 320, 480);
    tft.setRotation(1);
    showRAW(0, 0, "003.raw", 480, 320);
}


const int RAWPIXELS = 256;

uint8_t showRAW(int x, int y, const char *nm, int w, int h)
{
    uint8_t first = 1;
    int32_t n = (int32_t)w * h;  //size of RAW image in pixels
    uint8_t buf[RAWPIXELS * 2];      // size in bytes
    File inFile = SD.open(nm);
    if (!inFile) return 1;    //can't open
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    while (n > 0) {
        int cnt = RAWPIXELS;
        if (n < cnt) cnt = n;
        n -= cnt;
        inFile.read(buf, cnt * 2);
        tft.pushColors(buf, cnt, first);
        first = 0;
    }
    inFile.close();
    return 0;
}

Thank you soo much :o

It works just fine.. and a lot better than i expected.. :slight_smile:

Now i see the code and understand much more...

My bows to your complexity that simplified it for me xD :slight_smile:

How can i adjust the speed of the image loading? so the image is loaded in instantly. kinda like no transistion of the image on the next screen