ST7735R minimal LVGL chart example for PlatformIO
**Important:**In newer versions of PlatformIO, most ESP32 boards will crash with this example unless you create a custom lv_conf.h
config file specifying to use internal memory instead of PSRAM. See How to fix ESP32 LVGL crash / reboot in lv_tlsf_create() / lv_mem_init() for instructions how to fix that.
This example builds on our previous post Adafruit ST7735R display minimal LVGL example for PlatformIO
See Minimal ESP32 PlatformIO TFT display example using Adafruit ST7735 for the recommended hardware configuration.
Note that this example can easily be modified to work with other LCD controllers or displays.
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_LvGL_Glue.h>
#include <lvgl.h>
#include <string>
constexpr int Pin_LCD_CS = 27;
constexpr int Pin_LCD_DC = 23;
constexpr int Pin_LCD_RST = 22;
constexpr int Pin_LCD_SCLK = 14;
constexpr int Pin_LCD_MISO = 12;
constexpr int Pin_LCD_MOSI = 13;
Adafruit_ST7735 lcd(Pin_LCD_CS, Pin_LCD_DC, Pin_LCD_MOSI, Pin_LCD_SCLK,
Pin_LCD_RST);
Adafruit_LvGL_Glue glue;
void lvgl_chart_setup() {
/*Create a chart*/
lv_obj_t * chart;
chart = lv_chart_create(lv_scr_act());
lv_obj_set_size(chart, 160, 100);
lv_obj_align(chart, LV_ALIGN_BOTTOM_MID, 0, -3);
lv_chart_set_type(chart, LV_CHART_TYPE_LINE); /*Show lines and points too*/
/*Add two data series*/
lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_SECONDARY_Y);
/*Set the next points on 'ser1'*/
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 10);
lv_chart_set_next_value(chart, ser1, 30);
lv_chart_set_next_value(chart, ser1, 70);
lv_chart_set_next_value(chart, ser1, 90);
/*Directly set points on 'ser2'*/
ser2->y_points[0] = 90;
ser2->y_points[1] = 70;
ser2->y_points[2] = 65;
ser2->y_points[3] = 65;
ser2->y_points[4] = 65;
ser2->y_points[5] = 65;
ser2->y_points[6] = 65;
ser2->y_points[7] = 65;
ser2->y_points[8] = 65;
ser2->y_points[9] = 65;
lv_chart_refresh(chart); /*Required after direct set*/
}
void lvgl_setup(void) {
// Create simple label centered on screen
lv_obj_t *label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Temperature");
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 2);
lvgl_chart_setup();
}
void setup() {
lcd.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
lcd.setRotation(1);
LvGLStatus status = glue.begin(&lcd);
if(status != LVGL_OK) {
Serial.printf("Glue error %d\r\n", (int)status);
ESP.restart();
}
lvgl_setup(); // Call UI-building function above
}
void loop() {
lv_task_handler(); // Call LittleVGL task handler periodically
delay(5);
}
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit GFX Library@^1.11.5
adafruit/Adafruit ST7735 and ST7789 Library@^1.10.0
adafruit/Adafruit LittlevGL Glue Library@^2.1.4
adafruit/SdFat - Adafruit Fork@^2.2.1
lvgl/lvgl@^8.3.7