Displaying 100 BMPs from an SD card

Hey all,

I am using an Arduino UNO and the ELEGOO UNO R3 2.8 Inches TFT Touch Screen with SD Card Socket in an attempt to create a digital picture frame. I have hoped to be able to display at least 100 images on this device. Each image is 240x320, and I have been successful in displaying 20 images before running out of dynamic bytes. I am awful when it comes to code and circuitry, however, I can manage if there is something that I can go off of. The code I used is from the example code that comes with the 2.8 TFT touch screen called ShowBMP. I would add the attachment for this code, but I'm new. I was wondering there is a way that would grab an image from the SD card and then replace with the next image after 2 seconds so that it can go through at least 100 pictures on the SD card. I appreciate your time in helping me and I'm grateful for any help. Thanks.

What happens when you try to display the twenty-first image?

1 Like

So, I went ahead and added three more pictures so that it would display 23 images. It appears to display all 23 images just fine, but now it uses 80% dynamic memory. With 20 images, it used 73% dynamic memory. For me to display 100 images with my setup, then I would go well beyond the dynamic memory that is available in the Arduino UNO. That is the current issue I have with my current setup, and I do not know a path to the solution. I have tried to use PROGMAM in the code that would utilize storing the data in a flash memory, but I don't really know how to make it work. Would you think it is possible for me to cycle through the images on the SD card without storing them on the Arduino UNO? This is my first Arduino project and I'm learning a lot, so I appreciate your patience with me and your time. Thank you!

Why does your memory consumption depend on the number of images? Don't you show them one by one? - then the memory will be the same for one picture as for a thousand.

I think the discussion is pointless until your show your code.

1 Like

Yes. Reuse the same RAM memory location for each image and just replace the old image with the next one from the SD card. Did the display/SD card slot device also come with SD card examples?

1 Like

You're right, I should've started with the code. Here is my current code:

#include <Elegoo_GFX.h>    // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <SD.h>
#include <SPI.h>


// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define PIN_SD_CS 10 // Elegoo SD shields and modules: pin 10

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);


#define MAX_BMP         22                      // bmp file num
#define FILENAME_LEN    22                      // max file name length

const int __Gnbmp_height = 320;                 // bmp hight
const int __Gnbmp_width  = 240;                 // bmp width

unsigned char __Gnbmp_image_offset  = 0;        // offset

char __Gsbmp_files[MAX_BMP][FILENAME_LEN] = {
  "1.bmp", "2.bmp", "3.bmp", "4.bmp", "5.bmp", "6.bmp", "7.bmp", "8.bmp", "9.bmp", "10.bmp",
  "11.bmp", "12.bmp", "13.bmp", "14.bmp", "15.bmp", "16.bmp", "17.bmp", "18.bmp", "19.bmp", "20.bmp",
  "21.bmp", "22.bmp"
};
File bmpFile;
#define BUFFPIXEL       30                      // must be a divisor of 240 
#define BUFFPIXEL_X3    90                     // BUFFPIXELx3
void bmpdraw(File f, int x, int y)
{
    bmpFile.seek(__Gnbmp_image_offset);

    uint32_t time = millis();

    uint8_t sdbuffer[BUFFPIXEL_X3];                 // 3 * pixels to buffer

    for (int i=0; i< __Gnbmp_height; i++) {
        for(int j=0; j<(240/BUFFPIXEL); j++) {
            bmpFile.read(sdbuffer, BUFFPIXEL_X3);
            
            uint8_t buffidx = 0;
            int offset_x = j*BUFFPIXEL;
            unsigned int __color[BUFFPIXEL];
            
            for(int k=0; k<BUFFPIXEL; k++) {
                __color[k] = sdbuffer[buffidx+2]>>3;                        // read
                __color[k] = __color[k]<<6 | (sdbuffer[buffidx+1]>>2);      // green
                __color[k] = __color[k]<<5 | (sdbuffer[buffidx+0]>>3);      // blue
                
                buffidx += 3;
            }

      for (int m = 0; m < BUFFPIXEL; m ++) {
              tft.drawPixel(m+offset_x, i,__color[m]);
      }
        }
    }
    
    Serial.print(millis() - time, DEC);
    Serial.println(" ms");
}

boolean bmpReadHeader(File f) 
{
    // read header
    uint32_t tmp;
    uint8_t bmpDepth;
    
    if (read16(f) != 0x4D42) {
        // magic bytes missing
        return false;
    }

    // read file size
    tmp = read32(f);
    Serial.print("size 0x");
    Serial.println(tmp, HEX);

    // read and ignore creator bytes
    read32(f);

    __Gnbmp_image_offset = read32(f);
    Serial.print("offset ");
    Serial.println(__Gnbmp_image_offset, DEC);

    // read DIB header
    tmp = read32(f);
    Serial.print("header size ");
    Serial.println(tmp, DEC);
    
    int bmp_width = read32(f);
    int bmp_height = read32(f);
    
    if(bmp_width != __Gnbmp_width || bmp_height != __Gnbmp_height)  {    // if image is not 320x240, return false
        return false;
    }

    if (read16(f) != 1)
    return false;

    bmpDepth = read16(f);
    Serial.print("bitdepth ");
    Serial.println(bmpDepth, DEC);

    if (read32(f) != 0) {
        // compression not supported!
        return false;
    }

    Serial.print("compression ");
    Serial.println(tmp, DEC);

    return true;
}

/*********************************************/
// These read data from the SD card file and convert them to big endian
// (the data is stored in little endian format!)

// LITTLE ENDIAN!
uint16_t read16(File f)
{
    uint16_t d;
    uint8_t b;
    b = f.read();
    d = f.read();
    d <<= 8;
    d |= b;
    return d;
}

// LITTLE ENDIAN!
uint32_t read32(File f)
{
    uint32_t d;
    uint16_t b;

    b = read16(f);
    d = read16(f);
    d <<= 16;
    d |= b;
    return d;
}

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

#ifdef USE_Elegoo_SHIELD_PINOUT
  Serial.println(F("Using Elegoo 2.4\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using Elegoo 2.4\" TFT Breakout Board Pinout"));
#endif

  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  tft.reset();

  uint16_t identifier = tft.readID();
   
  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x4535) {
    Serial.println(F("Found LGDP4535 LCD driver"));
  }else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if(identifier==0x0101)
  {     
      identifier=0x9341;
       Serial.println(F("Found 0x9341 LCD driver"));
  }else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    identifier=0x9341;
   
  }
  
  tft.begin(identifier);
  tft.fillScreen(BLUE);
  
  
  
  //Init SD_Card
  pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    tft.setCursor(0, 0);
    tft.setTextColor(WHITE);    
    tft.setTextSize(1);
    tft.println("SD Card Init fail.");   
  }else
  Serial.println("initialization done."); 
}

void loop(void) {
     for(unsigned char i=0; i<MAX_BMP; i++) {
        bmpFile = SD.open(__Gsbmp_files[i]);
        if (! bmpFile) {
            Serial.println("didnt find image");
            tft.setTextColor(WHITE);    tft.setTextSize(1);
            tft.println("didnt find BMPimage");
            while (1);
        }
   
        if(! bmpReadHeader(bmpFile)) {
            Serial.println("bad bmp");
            tft.setTextColor(WHITE);    tft.setTextSize(1);
            tft.println("bad bmp");
            return;
        }

        bmpdraw(bmpFile, 0, 0);
        bmpFile.close();
        delay(1000);
        delay(2000);
    }
    
}

Why do you need this array? As far I see, the names of all your files are quite trivial - the number and an extension ".bmp".

So why to store it in array - just contruct the name in place from number and extension:


 for(unsigned char i=0; i<MAX_BMP; i++) {
        String filename = String(i+1) + ".bmp";
        bmpFile = SD.open(filename.c_str());
        if (! bmpFile) {
            Serial.println("didnt find image");
            tft.setTextColor(WHITE);    tft.setTextSize(1);
            tft.println("didnt find BMPimage");
            while (1);
        }
1 Like

Now that I look at it, it does come with SD card examples. That is definitely something I should look into, thank you for the advice.

I'm pretty new to coding with arduino, so I just went off of an example that was given with the display called "ShowBMP." I am not great with coding, so I really appreciate your example and advice! Thank you so much!

Another tip about the array:

The filesystem on SD uses a "8.3" name format - it means that filename consists with 8 chars name and 3 chars extension. Therefore, using the FILENAME_LEN 13 would be enough for any filename, and the array would be reduced by half.

Is that a display on a shield? Are you aware that most of them do not include level shifters on the SD signals to convert the 5V signals from the Uno to 3.3V signals that the SD card can accept without being damaged?

1 Like

Take a look at this example sketch from the SD library.

It shows you how to list the files on the card one by one so you don't need to store them in an array, or even create them dynamically.

2 Likes

Right, I initially thought I would need to include some resistors in the circuit to avoid that problem. From my understanding, this display is designed to connect right on top of an Arduino Uno. With the SD card socket built into the display, I believe the display would ensure that it doesn't get damaged. I don't know about why necessarily, but the user manual did not mention I needed one.

I'll need to look into how to implement this into my project. My biggest issue so far is that I know very little about code, so I appreciate the advice!

I see. Well, you'll certainly find out for yourself.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.