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

TFTBitmapLogo

Read an image file from a micro-SD card and draw it at random locations.

GLCDBitmapLogo

This example for the Arduino TFT screen reads a bitmap file from a SD card and displays it on screen in a random location.

For this example to work, you need to save an image named "logo.bmp" to the root of the SD card. The SD card needs to be FAT16 and FAT32 formatted. See the SD library documentation for more information on working with SD cards.

Hardware Required

  • Arduino Uno

  • Arduino TFT screen

  • micro-SD card

  • image file

Circuit

Insert the SD card with the "logo.bmp" file into the SD card slot on the back of your screen.

Connect power and ground to the breadboard.

GLCD logo1

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 logo2

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, MISO to pin 12, SD_CS to pin 4 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.

TFT logo large

Click the image for a larger version

Code

To use the screen you must first include the SPI and TFT libraries. You also need to include the SD library to read the image from the card.

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

Define the pins you're going to use for controlling the screen, and a pin for the SD chip select. Create an instance of the TFT library named

TFTscreen
.

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

There is a special datatype called PImage for holding image information. Create a named version of PImage

1PImage logo;

In

setup()
, you're going to initialize the serial port and wait for it to become active before starting up. If you want to ignore the status information, comment out the
while()
loop.

Once serial communication has started, initialize the SD library. If there is an error, send a message to the serial monitor.

1void setup() {
2
3 Serial.begin(9600);
4
5 while (!Serial) {
6
7 }
8
9 Serial.print("Initializing SD card...");
10
11 if (!SD.begin(SD_CS)) {
12
13 Serial.println("failed!");
14
15 return;
16
17 }
18
19 Serial.println("OK!");

Initialize and clear the screen

1TFTscreen.begin();
2
3 TFTscreen.background(255, 255, 255);

Read the image file into the PImage you named earlier with

loadimage()
.
loadImage()
prints out some useful information about the image to the serial monitor.

1logo = TFTscreen.loadImage("logo.bmp");
2
3 if (!logo.isValid()) {
4
5 Serial.println("error while loading arduino.bmp");
6
7 }
8}

If the image wasn't loaded correctly, stop the sketch before going any further.

1void loop() {
2
3 if (logo.isValid() == false) {
4
5 return;
6
7 }

If the image information is valid, pick a random spot on the screen to display the image. To make sure all the image is drawn onscreen, take the dimensions of the image and subtract that from the screen's dimensions.

1int x = random(TFTscreen.width() - logo.width());
2
3 int y = random(TFTscreen.height() - logo.height());

Draw the image onscreen starting at the random coordinates from the previous step, and wait for a little bit before entering the next

loop()

1TFTscreen.image(logo, x, y);
2
3 delay(1500);
4}

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.