MantisIDE

Lesson 13: Battery gauge · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Lesson 13 - A needle gauge (lv.scale) for the battery Goal: build an instrument dial with a scale, coloured sections and a needle, fed by the ADC on GPIO1 (battery through a 3:1 divider on this board).

You’ll learn: lv.scale sections, needle, ADC

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 4 · Sensors and data. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Lesson 13 - A needle gauge (lv.scale) for the battery
# Goal: build an instrument dial with a scale, coloured sections and a needle,
# fed by the ADC on GPIO1 (battery through a 3:1 divider on this board).
import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
AMBER = 0xF2B84B
RED = 0xFF6B6B
keep = []                             # styles, buffers and callbacks LVGL uses must stay referenced

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb


def dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
    """A filled circle: a plain lv.obj with every theme style removed."""
    o = lv.obj(parent)
    o.remove_style_all()
    size = max(1, int(size))
    o.set_size(size, size)
    o.set_style_radius(lv.RADIUS_CIRCLE, 0)
    o.set_style_bg_color(lv.color_hex(color), 0)
    o.set_style_bg_opa(lv.OPA.COVER, 0)
    o.remove_flag(lv.obj.FLAG.CLICKABLE)
    o.align(align, int(x), int(y))
    return o


def battery_volts():
    """Battery voltage: BAT_ADC is GPIO1 behind a 3:1 divider on this board."""
    from machine import ADC, Pin

    adc = ADC(Pin(1), atten=ADC.ATTN_11DB)
    return sum(adc.read_uv() for _ in range(8)) / 8 * 3 / 1_000_000


def battery_percent(v=None):
    v = battery_volts() if v is None else v
    return max(0, min(100, int((v - 3.3) / (4.2 - 3.3) * 100)))

scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

scale = lv.scale(scr)
scale.set_size(210, 210)
scale.center()
scale.set_mode(lv.scale.MODE.ROUND_INNER)
scale.set_range(30, 45)                    # 3.0 .. 4.5 V in tenths
scale.set_angle_range(240)
scale.set_rotation(150)
scale.set_total_tick_count(16)
scale.set_major_tick_every(5)
scale.set_label_show(True)
scale.set_text_src(["3.0V", "3.5V", "4.0V", "4.5V", None])
scale.set_style_line_color(lv.color_hex(MUTED), lv.PART.ITEMS)
scale.set_style_line_color(lv.color_hex(TEXT), lv.PART.INDICATOR)
scale.set_style_text_color(lv.color_hex(MUTED), lv.PART.INDICATOR)
scale.set_style_length(10, lv.PART.INDICATOR)
scale.set_style_length(5, lv.PART.ITEMS)


def section(lo, hi, color):
    st = lv.style_t()
    st.init()
    st.set_arc_color(lv.color_hex(color))
    st.set_arc_width(6)
    sec = scale.add_section()
    sec.set_range(lo, hi)
    sec.set_style(lv.PART.MAIN, st)
    keep.append(st)

section(30, 34, RED)
section(34, 37, AMBER)
section(37, 45, ACCENT)

needle = lv.line(scale)
needle.set_style_line_width(4, 0)
needle.set_style_line_color(lv.color_hex(TEXT), 0)
needle.set_style_line_rounded(True, 0)
dot(scr, 14, TEXT)
volts = label(scr, "", TEXT, 16, y=50)
pct = label(scr, "", MUTED, 12, y=72)


def update(_):
    v = battery_volts()
    scale.set_line_needle_value(needle, 80, max(30, min(45, int(v * 10))))
    volts.set_text("%.2f V" % v)
    pct.set_text("%d %%%s" % (battery_percent(v), "  (USB)" if v > 4.3 else ""))

update(None)
update_timer = lv.timer_create(update, 1000, None)
print("On USB power the reading is the charger voltage, not a battery.")
Loading post…

Dialog