Skip to content

Conversation

@kasperaitis
Copy link

Add configurable LDR auto-brightness for ESP32-2432S028R/2USB

Overview

Adds user-configurable LDR (Light Dependent Resistor) auto-brightness feature for ESP32-2432S028R and ESP32-2432S028_2USB boards, allowing users to choose between automatic ambient light adjustment or fixed brightness via WiFi configuration portal.

Features

  • Toggle via WiFi portal: Simple checkbox to enable/disable auto-brightness
  • Dual-mode brightness control:
    • Auto mode: Screen brightness adjusts from user-defined maximum down to minimum (20) based on ambient light
    • Manual mode: Fixed brightness at user-defined value (0-255)
  • Smooth transitions: Brightness changes gradually (2-step increments every 20ms) to avoid jarring jumps
  • Hardware-calibrated: Optimized for ESP32-2432S028R/2USB's 1MΩ pull-up resistor voltage divider circuit (GPIO 34)
  • Persistent storage: Settings saved to both SPIFFS and SD card with full backward compatibility

Changes

New Settings

  • useLDR (boolean): Toggle auto-brightness (default: false)
  • Brightness (0-255): Maximum when auto, fixed when manual (default: 250)

Modified Files

  • storage.h: Added DEFAULT_USELDR, JSON keys, and useLDR field to TSettings
  • nvMemory.cpp: Save/load useLDR with backward-compatible defaults
  • SDCard.cpp: Added useLDR loading from SD card config
  • wManager.cpp:
    • Added auto-brightness checkbox to WiFi configuration portal
    • Fixed checkbox handling bug (uses strlen() instead of string comparison)
    • Removed duplicate parameter reading code that was overwriting Settings with stale values
  • esp23_2432s028r.cpp:
    • Implemented LDR-based brightness adjustment with smooth transitions
    • Brightness respects Settings.useLDR flag at init and runtime
  • esp322432s028r.h: Documented LDR_PIN 34 assignment

Bug Fixes

  • WiFiManager checkbox handling: WiFiManager returns empty string for unchecked checkboxes, not "F" - fixed validation using strlen(getValue()) > 0
  • Duplicate code removal: Removed redundant 35-line parameter reading block that executed after WiFi.status() == WL_CONNECTED
    • Root cause: Code read WiFiManager form values twice - once in shouldSaveConfig block and once unconditionally after WiFi connection
    • Issue: Form parameters retain previous values in memory, causing Settings to be overwritten with stale data
    • Impact: Checkbox states and other parameters wouldn't persist correctly
    • Fix: Parameters now read only once when shouldSaveConfig == true

Technical Details

Hardware Calibration

  • LDR Circuit: R15 (1 MΩ) pulls the node up to 3.3 V, and R19 (1 MΩ) in series with the LDR pulls it to GND. The node goes to GPIO34 for ADC reading.
  • ADC: 12-bit resolution, ADC_0db attenuation (0-1.1V range)
  • Calibrated Range:
    • LDR_MIN = 0: Brightest (direct light, LDR low resistance pulls voltage low)
    • LDR_MAX = 500: Near complete darkness (LDR high resistance, voltage stays high)
  • Brightness Range: 20 (minimum) to user-configured maximum (default 250)

Performance

  • Read Interval: 500ms
  • Transition: 2-step increments every 20ms
  • Memory: +3 bytes in Settings struct

Configuration Examples

SD Card /config.json:

{
  "useLDR": true,
  "Brightness": 200
}

SPIFFS /config.json:

{
  "useLDR": false,
  "Brightness": 150
}

Backward Compatibility

  • Existing configs without useLDR default to false
  • Existing configs without Brightness default to 250
  • No breaking changes

Testing

  • ✅ Auto mode: Screen dims/brightens with ambient light
  • ✅ Manual mode: Fixed brightness maintained
  • ✅ WiFi portal: Settings persist correctly
  • ✅ SD card: First boot config loading works
  • ✅ SPIFFS: Settings survive reboots
  • ✅ Backward compatibility: Legacy configs work with defaults
  • ✅ No regression from duplicate code removal

Supported Boards

  • ESP32-2432S028R (CYD - Cheap Yellow Display)
  • ESP32-2432S028_2USB (CYD variant with dual USB ports)

Both boards share identical LDR hardware configuration.

User Experience

  1. Access WiFi configuration portal (192.168.4.1 in AP mode)
  2. Check "Auto-brightness" for ambient light adjustment
  3. Set brightness value (0-255):
    • Auto mode: Acts as maximum ceiling
    • Manual mode: Acts as fixed value
  4. Save - settings persist across reboots

Add configurable LDR auto-brightness for ESP32-2432S028R and ESP32-2432S028_2USB boards:

- Add useLDR boolean setting (default: false)
- Settings.Brightness acts as max when LDR enabled, fixed when disabled
- Smooth transitions (2-step/20ms) prevent jarring brightness changes
- Calibrated for 1MΩ pull-up resistor circuit (GPIO 34, ADC range 0-500)
- Persistent in SPIFFS/SD card with backward compatibility
- Fix WiFiManager checkbox bug (empty string when unchecked)
- Remove duplicate parameter reading code that overwrote Settings

Technical: LDR 0-500 range, brightness 20-255, 500ms read interval
@elbriga
Copy link

elbriga commented Dec 10, 2025

Hi! Nice feature, I have one question, why at the end of the diff there is two big chunks of lines being removed from the wManager.cpp file, where it reads "// Lets deal with the user config values" ? These lines seams to deal with all the wifimanager configs

@kasperaitis
Copy link
Author

Hi! Nice feature, I have one question, why at the end of the diff there is two big chunks of lines being removed from the wManager.cpp file, where it reads "// Lets deal with the user config values" ? These lines seams to deal with all the wifimanager configs

Duplicate code removal: Removed redundant 35-line parameter reading block that executed after WiFi.status() == WL_CONNECTED
Root cause: Code read WiFiManager form values twice - once in shouldSaveConfig block and once unconditionally after WiFi connection
Issue: Form parameters retain previous values in memory, causing Settings to be overwritten with stale data
Impact: Checkbox states and other parameters wouldn't persist correctly
Fix: Parameters now read only once when shouldSaveConfig == true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants