ESP32 with ILI9341 flickering and random lines with dark colors

Hello,

I’m having problems with my LVGL library implementation on an ESP32 when using dark colors or tones. I’ve also noticed it slightly with light colors, and it’s not a display or hardware issue.


As you can see in the image, random lines begin to appear as the color gets darker. Additionally, there’s a constant flickering that increases in frequency when I increase the ESP32’s clock speed from 160MHz to 240MHz (leading me to believe it’s a configuration or software issue).

This is my code for the display initialization configuration:

static void spi_display_init(void)
{
ESP_LOGI(TAG, “SPI Display: SPI2_HOST, DMA”);
spi_bus_config_t buscfg = {
.mosi_io_num = PIN_LCD_MOSI,
.miso_io_num = PIN_LCD_MISO,
.sclk_io_num = PIN_LCD_CLK,
//.max_transfer_sz = LCD_H_RES * LCD_V_RES * 2 + 8,
.max_transfer_sz = LCD_H_RES * LCD_V_RES * sizeof(uint8_t) * 3,
.flags = SPICOMMON_BUSFLAG_MASTER,
.intr_flags = ESP_INTR_FLAG_IRAM
};
ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
}

static void lcd_ili9341_init(void)
{
ESP_LOGI(TAG, “ILI9341 init”);
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = PIN_LCD_DC,
.cs_gpio_num = PIN_LCD_CS,
.pclk_hz = 40 * 1000 * 1000,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.spi_mode = 3,
.trans_queue_depth = 10,
.on_color_trans_done = lcd_flush_ready_cb,
.user_ctx = &disp_drv,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));

esp_lcd_panel_dev_config_t panel_config = {
    .reset_gpio_num = PIN_LCD_RST,
    .rgb_endian = LCD_RGB_ENDIAN_BGR,   // Cambiar a BGR si los colores están invertidos
    .bits_per_pixel = 16,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));

ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));

// --- CORRECCIÓN DE ORIENTACIÓN: espejo horizontal ---
bool swap_xy = false;
bool mirror_x = false;
bool mirror_y = false;

#if DISPLAY_ROTATION == 0
swap_xy = false; mirror_x = true; mirror_y = false; // 0°
#elif DISPLAY_ROTATION == 1
swap_xy = true; mirror_x = false; mirror_y = false; // 90°
#elif DISPLAY_ROTATION == 2
swap_xy = false; mirror_x = false; mirror_y = true; // 180°
#elif DISPLAY_ROTATION == 3
swap_xy = true; mirror_x = true; mirror_y = true; // 270°
#endif

ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, swap_xy));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, mirror_x, mirror_y));

ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));

}