Close

Screen Brightness

A project log for SeaGL 2025 Badge (Unofficial)

An interactive badge for the Seattle GNU/Linux Conference with live hashtag updates for the event.

johnsonfarmsusJohnsonFarms.us 10/18/2025 at 04:120 Comments

Since there are a couple of buttons on the T-Display I thought we could add a brightness adjustment. Just click the button and cycle through 4 brightness levels. 

How It Works 

  1. PWM Control: Uses ESP32's LED Control (LEDC) peripheral to generate PWM signal on the backlight pin 4 
  2. Brightness Levels: 10%, 40%, 70%, and 100% mapped to PWM values 26, 102, 179, and 255 Button 
  3. Detection: Both buttons (GPIO 0 and GPIO 14) are configured with internal pullups (active LOW) 
  4. Debouncing: 200ms delay prevents accidental multiple presses 
  5. Cycling: Each button press cycles to the next brightness level and wraps around

Code Snippets for Button Press Backlight Control

1. Pin Definitions

Add these button pin definitions at the top with your other pins:

// Pin definitions
#define PIN_LCD_BL 38     // Backlight pin
#define PIN_BUTTON_1 0    // Boot button
#define PIN_BUTTON_2 14   // Second button

2. Global Variables

Define brightness levels and state tracking:

// Brightness control
const uint8_t brightnessLevels[] = {26, 102, 179, 255};  // 10%, 40%, 70%, 100%
const char* brightnessLabels[] = {"10%", "40%", "70%", "100%"};
int currentBrightnessIndex = 3;  // Start at 100%
unsigned long lastButtonPress = 0;
const unsigned long buttonDebounce = 200;  // 200ms debounce

3. Forward Declarations

Add function declarations:

void setBrightness(int index);
void checkButtons();

4. Setup - Initialize PWM Backlight

Replace the simple digitalWrite() backlight control with PWM in your setup():

void setup() {    // ... other setup code ...        // Initialize backlight with PWM    ledcSetup(0, 5000, 8);  // Channel 0, 5kHz, 8-bit resolution    ledcAttachPin(PIN_LCD_BL, 0);    setBrightness(currentBrightnessIndex);  // Set to initial brightness (100%)    Serial.print("Backlight initialized at ");    Serial.print(brightnessLabels[currentBrightnessIndex]);    Serial.println(" brightness");        // Initialize buttons    pinMode(PIN_BUTTON_1, INPUT_PULLUP);    pinMode(PIN_BUTTON_2, INPUT_PULLUP);    Serial.println("Buttons initialized");        // ... rest of setup ...
}

5. Loop - Check Buttons

Add button checking in your main loop():

void loop() {    // ... other loop code ...        // Check for button presses to adjust brightness    checkButtons();        // ... rest of loop ...
}

6. setBrightness() Function

Set the backlight brightness using PWM:

void setBrightness(int index) {    if (index < 0 || index >= 4) return;    ledcWrite(0, brightnessLevels[index]);    currentBrightnessIndex = index;
}

7. checkButtons() Function

Handle button presses with debouncing:

// Check for button presses and cycle brightness
void checkButtons() {    unsigned long now = millis();        // Debounce check    if (now - lastButtonPress < buttonDebounce) {        return;    }        // Check if either button is pressed (active LOW with pullup)    bool button1Pressed = digitalRead(PIN_BUTTON_1) == LOW;    bool button2Pressed = digitalRead(PIN_BUTTON_2) == LOW;        if (button1Pressed || button2Pressed) {        lastButtonPress = now;                // Cycle to next brightness level        currentBrightnessIndex = (currentBrightnessIndex + 1) % 4;        setBrightness(currentBrightnessIndex);                Serial.print("Brightness changed to: ");        Serial.println(brightnessLabels[currentBrightnessIndex]);    }
}

Discussions