Try out LVGL Pro - A complete toolkit to build, test, share, and ship UIs efficiently!
LVGL
Tutorial

Using Hardware Buttons with LVGL on STM32

Learn how to integrate physical hardware buttons with LVGL on the STM32F429I-DISC1 Discovery kit to control UI elements and LEDs.

SeyyahSeyyah3 min read

This tutorial demonstrates how to use the STM32F429I-DISC1 Discovery kit to test LVGL's hardware button capability. The board has two push-buttons: USER and RESET [1]. We'll use the USER push-button, which is connected to I/O PA0 of the STM32F429ZIT6 [2]. The board also has six LEDs, and we'll use LD3 (green LED), which is connected to I/O PG13 of the STM32F429ZIT6 [2].

Our goal is to toggle the LD3 LED when both the hardware button (USER push-button) and the UI button are clicked.

Hardware Button Driver Registration#

Our hardware button driver register function is:

void my_button_init(void)
{
  static lv_indev_t *indev;
  lv_indev_drv_t indev_drv;
 
  lv_indev_drv_init(&indev_drv);
 
  indev_drv.read = my_input_read;
  indev_drv.type = LV_INDEV_TYPE_BUTTON;
  indev = lv_indev_drv_register(&indev_drv);
 
  static lv_point_t points_array[] = { { 20, 20 } };
  lv_indev_set_button_points(indev, points_array);
}
c

Where:

  • type: LV_INDEV_TYPE_BUTTON - External (hardware button) which is assigned to a specific point on the screen
  • read: my_input_read - Function that reads the hardware button status (press/release)
  • points_array: These points will be assigned to the buttons to press a specific point on the screen

Creating the UI Button#

We create a button on the screen. The points_array's first element (20px, 20px) is selected at any point within the UI button's area (x: 0-40px, y: 0-40px).

static lv_obj_t *btn = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_size(btn, 40, 40);
lv_obj_set_pos(btn, 0, 0);
lv_btn_set_action(btn, LV_BTN_ACTION_CLICK, btn_click_action);
c

Button Click Action#

Our btn_click_action toggles only the LD3 LED:

static lv_res_t btn_click_action(lv_obj_t *btn) {
  HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_13);
}
c

Reading Hardware Button Data#

The my_btn_read function reads the hardware button data and returns the hardware button ID:

uint8_t my_btn_read() {
  if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) {
    return 1;
  } else {
    return 0;
  }
}
c

Input Read Function#

The my_input_read function returns true if there is still data to be read (buffered), otherwise false:

bool my_input_read(lv_indev_data_t *data){
    static int8_t last_btn = 0;     /* Store the last pressed button */
    int8_t btn_pr = my_btn_read();  /* Get the ID (0,1,2...) of the pressed button */
 
    if(btn_pr > 0) {                /* Is there a button press? */
       last_btn = btn_pr;           /* Save the ID of the pressed button */
       data->state = LV_INDEV_STATE_PR;  /* Set the pressed state */
    } else {
       data->state = LV_INDEV_STATE_REL; /* Set the released state */
    }
 
    data->btn = last_btn;            /* Set the last button */
 
    return false;                    /* No buffering so no more data read */
}
c

Initialization in Main#

In the last step, we call button_init:

main() {
  // GPIO Init: LED, Button
  // LittlevGL
  button_init();
 
  while(1) {
    // ...
  }
}
c
Complete Project

A complete project example will be shared in a future update.

Acknowledgment#

We prepared this tutorial with Orhan Gunduz.

References#

  1. STM32F429I-DISC1 website
  2. UM1670 User manual Discovery kit with STM32F429ZI MCU
  3. LVGL issue discussion
  4. YouTube video demonstration

About the author

Seyyah
Seyyah

Community Contributor

Software developer and LVGL community contributor, co-authored hardware button integration guides with Ogunduz.

Meet the people behind the blog

Discover the talented writers sharing their knowledge about LVGL

View Authors

Subscribe to our newsletter to not miss any news about LVGL. We will send maximum of 2 mails per month.

LVGL

LVGL is the most popular free and open source embedded graphics library targeting any MCU, MPU and display type to build beautiful UIs.

We also do services like UI design, implementation and consulting.

© 2026 LVGL. All rights reserved.
YouTubeGitHubLinkedIn