Brightness Control is inconsistent


Using analogWrite to PWM the backlight LEDs, the results are inconsistent

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


What do you want to achieve?

I want the user to be able to push and hold on the touchscreen and have the brightness change to a “night” mode. Then, if they push and hold again, the screen switches back to “day” mode.

What have you tried so far?

I have it working…some of the time. I’m unclear why the results are inconsistent so i’m hoping someone here with a more technical background of the inner working processes of LVGL may be able to explain what’s occurring, and why, in a effort to reprogram my code to work 100% of the time.

Code to reproduce

void ui_event_screen1( lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);lv_obj_t * target = lv_event_get_target(e);
if ( event_code == LV_EVENT_LONG_PRESSED) {
      switchBrightness( e );
///// then the function below:

void switchBrightness(lv_event_t * e) {
    if (daytime == 1) {
        analogWrite(EXAMPLE_PIN_NUM_BK_LIGHT, nb);
    } else if (daytime == 0){
        analogWrite(EXAMPLE_PIN_NUM_BK_LIGHT, db);

/// in another function, I am saving the values of the sliders to the "nb" and "db" global variables, so they are not looked up each time from the sliders. Instead, the global variables retain the nb and db values constantly. 
    db = lv_slider_get_value(ui_dayBrightness);
    nb = lv_slider_get_value(ui_nightBrightness);

Screenshot and/or video

I will make a video if necessary, but what is occurring is that 99% of the time, the transition from daytime to nighttime functions appropriately. In my current scenario, the value of “nb” is 74 and the value of “db” is 255. However, when the switch occurs from nb to db, sometimes it fully brightens the LCD backlight. Other times, it brightens it a little bit, and other times it actually gets dimmer!

I’ve done Serial.println(db); before the analogWrite command, and each time, it always outputs 255, so I know the values are set correctly, but the analogWrite command is the one that seems to fail. From my understanding, there could be other interrupting processes that are happening in the background that cause the analogWrite command to fail or be incomplete? At least that’s my thought. I’m not writing this asking for someone to do it for me, I’m inquiring what could possibly cause this type of behavior and point me in the right direction such that I can overcome this obstacle in my code.

Thanks for your time!

1 Like