Skip to content

Connecting the LDR

Jonathas Barbosa edited this page Jun 11, 2023 · 1 revision

LDR connected in Trinity

Connect a LDR in your ESP32 like the image above and the display bright can be controlled automatically.

Configuring the Range

The two fields in the settings page are related to the values read by the LDR. With all lights turned off, the value read by LDR should be close to zero and, with all lights on, the maximum reading is 4095. In my case, it worked fine with Min Value = 0 and Max Value = 800 but it depends on each room/environment. There is a clickable link in the "LDR Pin" card on Settings Page that allows you read the current value of LDR, so you can check the amount of light is reaching in LDR when the lights are turned on and off. You can also uncomment the following line to follow up the value read by your LDR.

  • When Max value is set to zero, the feature is considered disabled and the bright will remain constant
  • When Min value is set to a value greater than zero and the ambient light read by LDR is lower than the Min value, the display will be turned off. For example, setting Min value to 1, when the LDR read is zero, the display is turned off.

Algorithm

About the bright control algorithm, I'm using the map function to reach the "correct" bright:

  // SETTINGS_MIN and SETTINGS_MAX are the values defined in settings page for automatic bright
  // MIN_BRIGHT is a constant in the code, it's the minimal bright value without turn the display off
  // DISPLAY_BRIGHT it's the bright defined in settings page
  uint8_t mapLDR = map(currentLDRValue, SETTINGS_MIN, SETTINGS_MAX, 1, 5);  //map in 5 slots
  uint8_t bright = map(mapLDR, 1, 5, MIN_BRIGHT, DISPLAY_BRIGHT);

Check the full implementation here

Clone this wiki locally