Files
ESP32-S3-LCD-1.3/.docs/BRIGHTNESS_CONTROL.md
T
2026-09-21 10:28:35 +02:00

5.9 KiB

Display Brightness Control Guide

Overview

The ESP32-S3-LCD-1.3 backlight brightness can be controlled via PWM on GPIO 20.

Brightness Range: 0-255

  • 0 = Backlight OFF (display is black)
  • 128 = 50% brightness (default)
  • 255 = Maximum brightness

How to Control Brightness

Method 1: Set Fixed Brightness

In main.cpp, call DisplayInit::setBrightness():

#include "modules/display_init.h"

void setup() {
    // ... after DisplayInit::init() ...

    // Set to 50% brightness
    DisplayInit::setBrightness(128);

    // Or set to 25% for dimmer display
    DisplayInit::setBrightness(64);

    // Or max brightness
    DisplayInit::setBrightness(255);
}

Method 2: Adjust Dynamically

void loop() {
    static bool is_dim = false;
    static uint32_t last_toggle = 0;

    // Toggle brightness every 5 seconds
    if (millis() - last_toggle > 5000) {
        last_toggle = millis();

        if (is_dim) {
            DisplayInit::setBrightness(255);  // Bright
            Serial.println("Brightness: MAX");
        } else {
            DisplayInit::setBrightness(64);   // Dim
            Serial.println("Brightness: LOW");
        }

        is_dim = !is_dim;
    }

    delay(10);
}

Method 3: Smooth Fade In/Out

void fadeOut() {
    for (int brightness = 255; brightness >= 0; brightness -= 5) {
        DisplayInit::setBrightness(brightness);
        delay(20);  // 20ms between steps
    }
}

void fadeIn() {
    for (int brightness = 0; brightness <= 255; brightness += 5) {
        DisplayInit::setBrightness(brightness);
        delay(20);
    }
}

Method 4: Auto-Dim After Inactivity

void loop() {
    static uint32_t last_activity = 0;
    const uint32_t DIM_TIMEOUT = 30000;  // 30 seconds
    const uint8_t DIM_LEVEL = 32;         // Dim to 12.5%
    const uint8_t BRIGHT_LEVEL = 255;     // Full brightness

    // Detect activity (button press, touch, etc.)
    if (activity_detected()) {
        last_activity = millis();
        DisplayInit::setBrightness(BRIGHT_LEVEL);
    }

    // Auto-dim after timeout
    if (millis() - last_activity > DIM_TIMEOUT) {
        uint8_t current = DisplayInit::getBrightness();
        if (current != DIM_LEVEL) {
            DisplayInit::setBrightness(DIM_LEVEL);
            Serial.println("Display dimmed due to inactivity");
        }
    }

    delay(100);
}

Method 5: Night Mode / Day Mode

enum DisplayMode {
    DAY_MODE,
    NIGHT_MODE
};

void setDisplayMode(DisplayMode mode) {
    if (mode == DAY_MODE) {
        DisplayInit::setBrightness(255);  // Full brightness
        Serial.println("Day mode: brightness 100%");
    } else {
        DisplayInit::setBrightness(64);   // 25% for night
        Serial.println("Night mode: brightness 25%");
    }
}

void loop() {
    // Example: switch mode based on time or ambient light sensor
    int hour = getCurrentHour();  // Your time function

    if (hour >= 6 && hour < 20) {
        setDisplayMode(DAY_MODE);
    } else {
        setDisplayMode(NIGHT_MODE);
    }

    delay(60000);  // Check every minute
}

Quick Examples

Example 1: Set Low Brightness at Startup

void setup() {
    Serial.begin(115200);

    if (!DisplayInit::init()) {
        Serial.println("Display init failed!");
        while(1);
    }

    // Set to low brightness (25%)
    DisplayInit::setBrightness(64);
    Serial.println("Brightness set to 25%");

    ui_init();
    // ... rest of setup ...
}

Example 2: Breathing Effect

void loop() {
    static int brightness = 0;
    static int direction = 1;

    brightness += direction * 5;

    if (brightness >= 255) {
        brightness = 255;
        direction = -1;
    } else if (brightness <= 50) {
        brightness = 50;
        direction = 1;
    }

    DisplayInit::setBrightness(brightness);
    delay(30);
}

Example 3: Get Current Brightness

void printBrightnessInfo() {
    uint8_t current = DisplayInit::getBrightness();
    int percentage = (current * 100) / 255;

    Serial.printf("Current brightness: %d/255 (%d%%)\n",
                  current, percentage);
}

Default Brightness

The default brightness is set to 128 (50%) in display_init.cpp:

static uint8_t current_brightness = 128;

You can change this default by editing line 88 in src/display_init.cpp.

Power Consumption

Brightness affects battery life:

  • 255 (100%): Maximum power draw (~150-200mA)
  • 128 (50%): Medium power (~100-120mA)
  • 64 (25%): Low power (~70-90mA)
  • 32 (12.5%): Very low power (~50-60mA)
  • 0 (OFF): Minimal power, display invisible

For battery-powered operation, consider:

  • Normal use: 128 (50%)
  • Idle/standby: 32 (12.5%)
  • Sleep mode: 0 (OFF)

Using with Automotive Dashboard (Phase 3)

void updateDashboard(const automotive_data_t* data) {
    // Dim display if engine is off (battery voltage low)
    if (data->battery_voltage_v < 12.0) {
        DisplayInit::setBrightness(64);  // Low brightness to save power
    } else {
        DisplayInit::setBrightness(200);  // Normal brightness
    }

    // Update UI with data...
}

API Reference

Functions

namespace DisplayInit {
    // Set backlight brightness (0-255)
    void setBrightness(uint8_t brightness);

    // Get current brightness level (0-255)
    uint8_t getBrightness();
}

Usage

#include "modules/display_init.h"

// Set brightness
DisplayInit::setBrightness(128);  // 50%

// Get current brightness
uint8_t level = DisplayInit::getBrightness();

Tips

  1. Don't update too frequently: Changing brightness constantly can cause flickering
  2. Use delays between changes: Add small delays when fading
  3. Save battery: Lower brightness when on battery power
  4. Night mode: Use 25-50% brightness for comfortable night viewing
  5. Minimum visible: Brightness below 20-30 may be too dim to read

Default: 128 (50% brightness) Range: 0-255 (0=off, 255=max) Control: Via GPIO 20 PWM (44.1kHz)