Example Code for Arduino-SD Card Picture Display
This article offers a comprehensive guide on displaying pictures using an SD card with Arduino, focusing on hardware setup, software requirements, and example code for ESP32 and similar boards.
Hardware Preparation
- FireBeetle ESP32-E (or similar) x 1
- 1.8Inch 128×160 TFT LCD Display with MicroSD Card Slot x1
- Wires
Software Preparation
- Arduino IDE
- DFRobot GDL Library
- About how to install the library?
- DFRobot_GDL API, click to learn more.
Wiring Diagram
The following figure shows two ways of connection: SPI and GDI.

Other Preparation Work
- For using GDI, please make sure your controller has GDI interface.
- It is recommended to use Arduino version 1.8.10 and above.
- If the SD card slot is not well connected, the initialization may fail, and it will succeed after unplugging and plugging again.
- Arduino UNO is not recommended for memory reasons.
- All the Demos of this product are stored in the file of DFRobot_GDL->ST7735_128x160-.
- Before burning the Demo, please open the corresponding substantiation function (DFRobot_ST7789_128x160_HW_SPI)

Sample Code
#include <SD.h>
#include <SPI.h>
#include "DFRobot_GDL.h"
#include "DFRobot_Picdecoder_SD.h"
DFRobot_Picdecoder_SD decoder;
/*M0*/
#if defined ARDUINO_SAM_ZERO
#define TFT_DC 7
#define TFT_CS 5
#define TFT_RST 6
#define TFT_SD 3
/*ESP32 ESP8266*/
#elif defined(ESP32)
#define TFT_DC D2
#define TFT_CS D6
#define TFT_RST D3
#define TFT_SD D7
/*ESP8266*/
#elif defined(ESP8266)
#define TFT_DC D4
#define TFT_CS D6
#define TFT_RST D5
#define TFT_SD D7
/* AVR series mainboard */
#else
#define TFT_DC 2
#define TFT_CS 3
#define TFT_RST 4
#define TFT_SD 6
#endif
DFRobot_ST7735_128x160_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
/* M0 mainboard DMA transfer */
//DFRobot_ST7735_128x160_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
void setup()
{
Serial.begin(115200);
screen.begin();
while(1)
{
#if defined ARDUINO_SAM_ZERO
if (SD.begin(/*sdcs=*/TFT_SD,/*type = */TYPE_NONBOARD_SD_MOUDLE)){
#else
if (SD.begin(/*sdcs=*/TFT_SD)){
#endif
Serial.println("initialization done.");
break;
}
Serial.println("initialization failed!");
}
}
void loop()
{
screen.fillScreen(COLOR_RGB565_WHITE);
decoder.drawPicture(/*filename=*/"picture/219x220.jpg",/*sx=*/0,/*sy=*/0,/*ex=*/screen.width(),/*ey=*/screen.height(),/*screenDrawPixel=*/screenDrawPixel);
screen.fillScreen(COLOR_RGB565_WHITE);
decoder.drawPicture(/*filename=*/"picture/RGB565.bmp",/*sx=*/0,/*sy=*/0,/*ex=*/screen.width(),/*ey=*/screen.height(),/*screenDrawPixel=*/screenDrawPixel);
screen.fillScreen(COLOR_RGB565_WHITE);
/*FireBeetle-M0,ESP32 and ESP8266*/
#if defined ARDUINO_SAM_ZERO || defined(ESP8266)
File myDir = SD.open(/*directory name=*/"picture/Icon/",/*mode=*/FILE_READ);
if(myDir)
{
char str[32];//Store file name of the icon in the read directory
for(uint16_t y = 10; y<screen.height()-32; y+=60)//y coordinate
{
for(uint16_t x = 10; x<screen.width()-32; x+=60)//X coordinate
{
File entry = myDir.openNextFile();
if (! entry)
{
goto quit;
}
strcpy(str,"picture/Icon/");
strcat(str,entry.name());
decoder.drawPicture(/*filename=*/str,/*sx=*/x,/*sy=*/y,/*ex=*/x+32,/*ey=*/y+32,/*screenDrawPixel=*/screenDrawPixel);
}
}
quit:
myDir.close();
}
else
{
Serial.println("dir open fail");
}
#else
decoder.drawPicture("picture/Icon/1.bmp",0,0,32,32,screenDrawPixel);
decoder.drawPicture("picture/Icon/2.bmp",32,32,64,64,screenDrawPixel);
decoder.drawPicture("picture/Icon/3.bmp",64,64,96,96,screenDrawPixel);
decoder.drawPicture("picture/Icon/4.bmp",96,96,128,128,screenDrawPixel);
#endif
delay(1000);
}
void screenDrawPixel(int16_t x, int16_t y, uint16_t color)
{
screen.writePixel(x,y,color);
}
Result

Was this article helpful?
