Example Code for Arduino-Rainbow Gradient Display
The Rainbow.ino code shows us the color display effect of this display, with a gradient color from red to blue displayed on the screen.
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 "DFRobot_GDL.h"
/*M0*/
#if defined ARDUINO_SAM_ZERO
#define TFT_DC 7
#define TFT_CS 5
#define TFT_RST 6
/*ESP32 ESP8266*/
#elif defined(ESP32)
#define TFT_DC D2
#define TFT_CS D6
#define TFT_RST D3
/*ESP8266*/
#elif defined(ESP8266)
#define TFT_DC D4
#define TFT_CS D6
#define TFT_RST D5
/* AVR series mainboard */
#else
#define TFT_DC 2
#define TFT_CS 3
#define TFT_RST 4
#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);
byte red = 29;
byte green = 0;
byte blue = 0;
byte state = 0;
boolean initial = 1;
void setup() {
Serial.begin(115200);
screen.begin();
screen.fillScreen(COLOR_RGB565_LGRAY);
for(uint16_t i=0;i<screen.height();i++){
screen.drawFastHLine(0,i,screen.width(),rainbow());
}
}
void loop() {
screen.setTextSize(1);
screen.setFont(&FreeMono9pt7b);
screen.setCursor(/*x=*/10,/*y=*/screen.height()-108);
screen.setTextColor(COLOR_RGB565_BLACK);
screen.setTextWrap(true);
screen.print("DFRobot");
delay(500);
screen.setFont(&FreeMonoBoldOblique9pt7b);
screen.setCursor(10,screen.height()-72);
screen.setTextColor(COLOR_RGB565_RED);
screen.setTextWrap(true);
screen.print("GDL");
delay(500);
screen.setFont(&FreeSansBold9pt7b);
screen.setCursor(10,screen.height()-38);
screen.setTextColor(COLOR_RGB565_YELLOW);
screen.setTextWrap(true);
screen.print("fonts test");
delay(500);
screen.setFont(&FreeSerif9pt7b);
screen.setCursor(10,screen.height()-6);
screen.setTextColor(COLOR_RGB565_BLUE);
screen.setTextWrap(true);
screen.print("hello,world!");
delay(500);
}
unsigned int rainbow()
{
switch (state) {
case 0:
green ++;
if (green == 64) {
green = 63;
state = 1;
}
break;
case 1:
red--;
if (red == 255) {
red = 0;
state = 2;
}
break;
case 2:
blue ++;
if (blue == 32) {
blue = 31;
state = 3;
}
break;
case 3:
green --;
if (green == 255) {
green = 0;
state = 4;
}
break;
case 4:
red ++;
if (red == 32) {
red = 31;
state = 5;
}
break;
case 5:
blue --;
if (blue == 255) {
blue = 0;
state = 0;
}
break;
}
return red << 11 | green << 5 | blue;
}
Result

Was this article helpful?
