Skip to content

Instantly share code, notes, and snippets.

@altmind
Created October 8, 2025 06:15
Show Gist options
  • Select an option

  • Save altmind/d9ea540a89301615e13a5e5ace1ca320 to your computer and use it in GitHub Desktop.

Select an option

Save altmind/d9ea540a89301615e13a5e5ace1ca320 to your computer and use it in GitHub Desktop.
M5Stack Basic LCD init for LVGL
import lvgl as lv
import lcd_bus
import ili9341
from machine import SPI, Pin
import machine
import network
import task_handler
import asyncio
import utime
import ntptime
spi_bus = SPI.Bus(host=1, mosi=23, miso=19, sck=18)
display_bus = lcd_bus.SPIBus(spi_bus=spi_bus, dc=27, cs=14, freq=40000000)
display = ili9341.ILI9341(
data_bus=display_bus,
display_width=320,
display_height=240,
reset_pin=33,
reset_state=ili9341.STATE_LOW,
backlight_pin = 32,
backlight_on_state=ili9341.STATE_LOW,
color_space=lv.COLOR_FORMAT.RGB565,
rgb565_byte_swap=True,
color_byte_order=ili9341.BYTE_ORDER_BGR
)
display._ORIENTATION_TABLE = (0,0x20|0x40,0x80|0x40,0x20|0x80)
display.set_rotation(lv.DISPLAY_ROTATION._0)
display.set_backlight(100)
display.init(1)
dispp = lv.display_get_default()
theme = lv.theme_default_init(dispp, lv.palette_main(lv.PALETTE.RED), lv.palette_main(lv.PALETTE.YELLOW), True, lv.font_get_default())
dispp.set_theme(theme)
th = task_handler.TaskHandler()
scrn = lv.screen_active()
#scrn.set_style_bg_color(lv.color_hex(0x0000ff), 0)
label = lv.label(scrn)
label.set_text('TopLeft')
slider = lv.slider(scrn)
slider.set_size(200, 30)
slider.center()
label = lv.label(scrn)
label.set_text('Center')
label.align(lv.ALIGN.CENTER, 0, -50)
async def main():
print("Starting LVGL Async Loop...")
while True:
lv.task_handler()
await asyncio.sleep_ms(5)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
finally:
display.set_power(False)
print("Display powered down.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment