Examples > TFT

Esplora TFT Bitmap Logo

This example for the Esplora with an Arduino 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 Esplora
  • Arduino TFT screen
  • micro-SD card
  • image file

Circuit

Attach the TFT screen to the socket on your Esplora, with the label "SD Card" facing up. Insert the SD card with the "logo.bmp" file into the slot.

Code

To use the screen you must first include the SPI and TFT libraries. Don't forget to include the Esplora library and the SD library to read the image from the card.

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

You need to define the pin for the SD reader's chip select (CS). On the Esplora, it is mapped to pin 8.

#define SD_CS    8

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

PImage logo;

In setup(), you're going to initialize the serial port and wait for it to become active before starting up. This means that you will need to turn on the serial monitor once the sketch has loaded. 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, turn the Esplora's LED red.

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    Esplora.writeRed(255);
    return;
  }
  Serial.println("OK!");

Initialize and clear the screen

EsploraTFT.begin();
  EsploraTFT.background(255, 255, 255);

Try reading the image file into the PImage you named earlier with loadimage(). Check that it is a valid image, and turn the Esplora's LED green if everything is OK, red if there is an error.

Esplora.writeRGB(0, 0, 0);
  logo = EsploraTFT.loadImage("logo.bmp");
  if (logo.isValid()) {
    Esplora.writeGreen(255);
  }
  else
    Esplora.writeRed(255);

}

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

void loop() {
  if (logo.isValid() == false) {
    return;
  }

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.

int x = random(EsploraTFT.width() - logo.width());
  int y = random(EsploraTFT.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()

EsploraTFT.image(logo, x, y);

  delay(1500);
}

The complete sketch is below :

SORRY, There is an error at our code repository, please inform to web@arduino.cc