LVGL and displaying data sent from COM Ports?

Hey there, I’m trying to send data via my COM port over my usb to my esp32-s3 and have that data be displayed on a LVGL button. Though when i send the data the display turns black. I know its the data sending that’s doing this because i can add functionality to change the buttons text when i trigger a defined piece of code. But this is not what i want. I want to get data from spotify api ( already got the spotify api data figured out ) and send it over my usb to my esp32 for it to be display. Ex: The title of the current playing song being displayed on a button.
Any help regarding why my display is turning off when i receive data on my esp32-s3 would be greatly appreciated. I haven’t configured the displaying of the data yet. Just trying to receive data to the esp32 without it blacking out. I’ll provide my code now.

ESP32-S3 CODE

import time
import os
import sys
import select
from machine import Pin
import lvgl as lv
from display_driver import disp, disp_drv

led = Pin(2, Pin.OUT)  # Adjust GPIO number if needed
MAX_LOG_SIZE = 10 * 1024  # 10 KB

# ====== Input loop setup ======
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)

# Create screen
scr = lv.obj()

# Colors
spotify_green = lv.color_hex(0x1DB954)
spotify_dark = lv.color_hex(0x191414)
spotify_light_gray = lv.color_hex(0xB3B3B3)
white = lv.color_hex(0xFFFFFF)

# Create styles
style_bg = lv.style_t()
style_bg.init()
style_bg.set_bg_color(spotify_dark)
style_bg.set_border_width(0)

style_green_bg = lv.style_t()
style_green_bg.init()
style_green_bg.set_bg_color(spotify_green)
style_green_bg.set_radius(8)

style_white_text = lv.style_t()
style_white_text.init()
style_white_text.set_text_color(white)

style_light_gray_text = lv.style_t()
style_light_gray_text.init()
style_light_gray_text.set_text_color(spotify_light_gray)

style_progress_bg = lv.style_t()
style_progress_bg.init()
style_progress_bg.set_bg_color(lv.color_hex(0x333333))
style_progress_bg.set_radius(4)

style_progress_indic = lv.style_t()
style_progress_indic.init()
style_progress_indic.set_bg_color(spotify_green)
style_progress_indic.set_radius(4)

# Apply screen background style
scr.add_style(style_bg, 0)

# Album art box (placeholder)
album_art = lv.obj(scr)
album_art.set_size(80, 80)
album_art.add_style(style_green_bg, 0)
album_art.align(lv.ALIGN.TOP_MID, 0, 20)

# Song title label
song_title_label = lv.label(scr)
song_title_label.set_text("Blinding Lights")
song_title_label.add_style(style_white_text, 0)
song_title_label.align_to(album_art, lv.ALIGN.OUT_BOTTOM_MID, 0, 10)

# Artist label
artist = lv.label(scr)
artist.set_text("The Weeknd")
artist.add_style(style_light_gray_text, 0)
artist.align_to(song_title_label, lv.ALIGN.OUT_BOTTOM_MID, 0, 5)

# Progress bar
progress = lv.bar(scr)
progress.set_size(200, 10)
progress.add_style(style_progress_bg, lv.PART.MAIN)
progress.add_style(style_progress_indic, lv.PART.INDICATOR)
progress.set_range(0, 100)
progress.align_to(artist, lv.ALIGN.OUT_BOTTOM_MID, 0, 15)

# Play button
btn = lv.button(scr)
btn.set_size(120, 40)
btn.align(lv.ALIGN.BOTTOM_MID, 0, -20)
btn.add_style(style_green_bg, 0)

btn_label = lv.label(btn)
btn_label.set_text("Next In Queue: ")
btn_label.add_style(style_white_text, 0)
btn_label.center()

# Load screen
lv.screen_load(scr)

# Logging function
def send_log(msg):
    try:
        if 'debug_log.txt' in os.listdir() and os.stat('debug_log.txt')[6] > MAX_LOG_SIZE:
            open('debug_log.txt', 'w').close()  # Clear if too large
        with open('debug_log.txt', 'a') as f:
            f.write(msg + '\n')
    except Exception as e:
        print("Logging error:", e)


# Main loop
while True:
    lv.task_handler()
    time.sleep_ms(10)
    try:
        events = poll.poll(100)  # 100ms timeout
        if events:
            line = sys.stdin.readline().strip()
            send_log(f"Received input: {line}")

            if line == "start":
                with open('Startlog.txt', 'a') as f:
                    f.write("ESP32 Received right data.\n")
                send_log("RIGHT")
            else:
                with open('Startlog1.txt', 'a') as f:
                    f.write("ESP32 Received wrong data.\n")
                send_log("WRONG")

            # Blink LED
            led.on()
            time.sleep(0.2)
            led.off()
        else:
            time.sleep(0.05)  

    except Exception as e:
        send_log(f"Input error: {e}")

PC Code to send data via usb

import serial
import time

# === CONFIGURATION ===
COM_PORT = 'COM9'          
BAUD_RATE = 115200
DATA_TO_SEND = "start\n"   

# === OPEN SERIAL CONNECTION ===
try:
    ser = serial.Serial(COM_PORT, BAUD_RATE, timeout=1)
    time.sleep(2)  # Give ESP32 time to boot and get ready

    print(f"Sending to ESP32: {DATA_TO_SEND.strip()}")
    ser.write(DATA_TO_SEND.encode())

    # Wait and read any response
    time.sleep(0.2)
    response = ser.read_all().decode().strip()
    if response:
        print("ESP32 says:", response)

    ser.close()
except Exception as e:
    print("Serial communication error:", e)

I do get a psram error when i start the program to test the lvgl UI on thonny but none-the-less it continues so it never really bothered me due to the fact that this is the only working firmware with lvgl i’ve found for my esp32-s3 . ( NOTE: I cannot send data while having thonny open so this is purely to test for lvgl errors, Explained at the end of this thread )

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3820,len:0x105c
load:0x403c9700,len:0x4
load:0x403c9704,len:0xbd8
load:0x403cc700,len:0x2e34
entry 0x403c989c
E (437) quad_psram: PSRAM ID read error: 0x00ffffff, PSRAM chip not found or not supported, or wrong PSRAM line mode
E (437) esp_psram: PSRAM enabled but initialization failed. Bailing out.

That error message could be getting displayed every time i receive data to the esp32-s3 which could be the root cause but no way of telling because i can’t send data over com9 while having thonny open on com9, Both get conflicted and one doesnt work. I have to have the script for the lvgl code on main.py so it runs without thonny open.
Thanks again for any help!

UPDATE: Out of some miracle i figured it out. I was taking a peek at my board and realized it has 2 usb hubs and just plugged 2 usbs in so i have 2 com ports. One is for lvgl i guess and the other is used for data! Now it doesnt crash and i can update my screen ui based on data i send!

Thanks a lot for sharing your solution! I was running into a very similar issue. my display would go black whenever I tried to receive data over USB, and I couldn’t figure out why. After reading your update about the two USB interfaces on the board and using one for LVGL and the other for data, I tried the same and it works perfectly now.

You really helped me out. Appreciate it! :raised_hands:

Oh! That’s great to hear! I struggled for days trying to figure it out! Glad i could help out!!!

Hi, I have kind of the inverse problem where when I use LVGL I can no longer send data out on the second USB port.

My issue

As you have been in this space for longer do you know if you can configure LVGL to use the other USB port

I’d start by checking your Device Manager (or lsusb/dmesg if you’re on Linux). When you plug both USB cables in, do you see two COM ports show up?

If you only see one, then the second USB is most likely power-only and won’t transfer data. That’s pretty common on some ESP32 boards.
If you do see two, then great, both are data capable. In that case, make sure you’re using the right one in your serial monitor.

If your board’s second USB really is power-only but you still need another data connection, you can use the ESP32’s UART pins with a cheap USB-to-serial adapter. That way you’ll get an extra COM port on your computer.

If it’s worked before see if you made any changes and revert them and try again. I’d take a look at your menuconfig for the firmware to see if you maybe have to enable something then flash it again. P.S double check to make sure your sending data over the right port to your PC. I don’t think i’ve ever had to send data from my esp32 to my PC but should be the same concept.

Thanks @Anixtzer, yeah it’s very strange. Can get it to work as a standalone keyboard using the second USB port with no issues. Very minor updates to the project with LVGL (see my linked “my issue” post above) to include the same libraries but port no longer works.

Battled hard on this one and have given up so have now changed approach and moved to using bluetooth which has worked, can send keyboard presses (inc media keys), paragraphs of text, key combos and call functions in the code.

Now rather than stopping and making use of it, trying to extend so that I can pair and switch between multiple computers.