How read BME280 sensor value on TFT display with LVGL library

Board: ESP32
Complier: Arduino IDE

Version: LVGL 8.3.11

Hello, I trying to send values from the temperature sensor to the display. BME280 sensor is reading values correctly what you can see on the Serial Monitor, but in display they don’t change, here is a problem. What i can do or change to fix that. Maybe different library?

Code:

#include <Wire.h>
#include <Adafruit_BME280.h>
#include <TFT_eSPI.h>
#include <lvgl.h>
Adafruit_BME280 bme;
TFT_eSPI tft = TFT_eSPI();
float temperature;
lv_obj_t *temp_label;

void update_display()
 {
    lv_label_set_text_fmt(temp_label, "Temperature: %.2f °C", temperature);
}

void my_disp_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p) {
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors((uint16_t*)&color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp_drv);
}

void setup() {
    Serial.begin(115200);
    
    tft.init();
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);

    if (!bme.begin(0x76)) {
        Serial.println("Can't found BME280!");
        while (1);
    }

    lv_init();
    static lv_disp_draw_buf_t draw_buf;
    static lv_color_t buf[320 * 240 / 10];
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, 320 * 240 / 10);

    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = 320;
    disp_drv.ver_res = 240;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);

    temp_label = lv_label_create(lv_scr_act());
    lv_label_set_text(temp_label, "Temperature: 0.00 °C");
    lv_obj_align(temp_label, LV_ALIGN_CENTER, 0, 0);
}

void loop() {
    temperature = bme.readTemperature();
    Serial.print("Temperature value from sensor: ");
    Serial.println(temperature);

   
    update_display();

    lv_task_handler();  
    delay(1000);        
}

Are you providing the tick to LVGL? Without that, the screen will not be updated by lv_task_handler() calls. I don’t see your lv_conf.h, but in case you haven’t set it up, here’s some information.

Take a look at - tick interface

If on Arduino, you don’t need to create an interrupt/event to call lv_tick_inc, you can edit this section of lv_conf.h like this:

#define LV_TICK_CUSTOM 1
#if LV_TICK_CUSTOM
    #define LV_TICK_CUSTOM_INCLUDE "Arduino.h"         /*Header for the system time function*/
    #define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis())    /*Expression evaluating to current system time in ms*/
    /*If using lvgl as ESP32 component*/
    // #define LV_TICK_CUSTOM_INCLUDE "esp_timer.h"
    // #define LV_TICK_CUSTOM_SYS_TIME_EXPR ((esp_timer_get_time() / 1000LL))
#endif   /*LV_TICK_CUSTOM*/
1 Like

lv_conf.h (35.5 KB)

Which LVGL version are you using? At the top of your post, you state 8.3.11, so I posted a potential solution for LVGL 8.x.x. But looking at the attached lv_conf.h, that looks like the LVGL 9.x.x config…

If you’re using LVGL 9.x.x, that solution above won’t work. Look here for LVGL 9.x.x:

Tick interface

You can implement option 1. Create a function:

static uint32_t my_tick_get_cb(void) { return millis(); }

Then in your code after lv_init():

lv_tick_set_cb(my_tick_get_cb);

I’m using 8.3.11 because i want use SquareLine to designe interface, but I would like to use 9.2 version. Have you any programs to create interface?
Now example program don’t work, i mixed up everything…

The lv_conf.h files are not interchangable between 8.x.x and 9.x.x, there are a lot of differences between both the configs and the library code.

I’d recommend starting with a clean install of LVGL 8.3.11, a clean lv_conf.h file from lv_conf_template.h, make the changes proposed above and any changes you need, and go from there. It’s hard to remote diagnose if you are trying to mix and match!

I updated lv_conf and still not work :frowning:
lv_conf.h (25.9 KB)
I have still on display Temperature: f *C

check format characters or start with %f only … When on display you have f *C some bad characters is in format… and update works ok

%.2f is an acceptable format for float, I use that myself.

Edit your lv_conf.h file.
On line 304 define LV_SPRINTF_USE_FLOAT to 1.