Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of ambient light sensor #74

Open
kohlsalem opened this issue Dec 6, 2023 · 6 comments
Open

Support of ambient light sensor #74

kohlsalem opened this issue Dec 6, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@kohlsalem
Copy link
Contributor

Not my idea, but mentioned in the german CT make and on the homepage of the author (http://blog.digital-image.de/2023/05/31/x-clock/):

With a cheap LDR it would be possible to measure the actual ambient light - and adjust the LED brightness.

I believe this is reasonably simple (for people who dare to solder on a 100€device), cheap, and far superior to any time base adjustment.

It could be an option to be compiled into the firmware.

Best
Michael

@kohlsalem
Copy link
Contributor Author

kohlsalem commented Dec 10, 2023

i hacked this at the end of my main, works great

// Define a tuple structure to hold sensor values and corresponding brightness levels
  typedef struct {
  int sensorValue;
  int brightnessLevel;
  } SensorBrightnessTuple;


  // Define the vector of tuples
  std::vector<SensorBrightnessTuple> sensorBrightnessValues = {
    {    0, 3},
    {1000, 100},
    {4095, 255}
    // Add more tuples as needed

  };
  const int analogInputPin = 34;  // GPIO15 / ADC13
  static unsigned long previousMillis;

  unsigned long currentMillis = millis();

  // Führe den Code nur alle 5 Sekunden aus
  if (currentMillis - previousMillis >= 1000) {//every Second
    previousMillis = currentMillis;
    int sensorValue = analogRead(analogInputPin);
    uint8_t newbrightness;  
    // Check if the sensor value is within the given range
    if (sensorValue <= sensorBrightnessValues.front().sensorValue) {
      newbrightness = sensorBrightnessValues.front().brightnessLevel;
    } else if (sensorValue >= sensorBrightnessValues.back().sensorValue) {
      newbrightness = sensorBrightnessValues.back().brightnessLevel;
    } else {
      // Perform linear interpolation for intermediate values
      for (size_t i = 0; i < sensorBrightnessValues.size() - 1; ++i) {
        if (sensorValue >= sensorBrightnessValues[i].sensorValue && sensorValue <= sensorBrightnessValues[i + 1].sensorValue) {
          // Linear interpolation formula
          newbrightness =  map(sensorValue, sensorBrightnessValues[i].sensorValue, sensorBrightnessValues[i + 1].sensorValue,
                          sensorBrightnessValues[i].brightnessLevel, sensorBrightnessValues[i + 1].brightnessLevel);
          break;
        }
      }
    }     
    Screen.setBrightness(newbrightness);
   // Screen.scrollText(std::to_string(sensorValue).append(">").append(std::to_string(newbrightness)));
  }

I made the mistake to develop my text pull request in the main branch, so i have no clue how to address a second feature meanwhile. to develop in main and a feature seams to be a broblem, at least GH allows now new bransch with open PR on feature...

@ph1p ph1p added the enhancement New feature or request label Dec 11, 2023
@WRedux
Copy link

WRedux commented May 5, 2024

Hello,

i want to add the LDR-Featue to my OBEGRÄNSAD, but it wont work.

Dont know, wether its a Software or a Hardware-Thing

I wired like some Examples in the Web with a LDR at 3V3, a 10k Resistor to GND and GPIO15.
http://www.esp32learning.com/code/esp32-and-ldr-example.php

To fix the Software-Side would you be so kind and post your Code?

Just putting it to the End of main.cpp gives me some Errors while compiling....

Thanx!

@kohlsalem
Copy link
Contributor Author

It should be just somewhere within the loop…

@WRedux
Copy link

WRedux commented May 7, 2024

OK. Did that. Compiles, but seems to have still no Effect.

Tested with old LDR, changed LDR now:

I use a Type 5537 LDR now. Depends something in the Code of the LDR-Type?
Dunno which LDR-Type I was using before (a old one I found) but the Behaviour is different now,
not fully dimmed as before, but still shows no Reaction...

Thanx for reading!

@kohlsalem
Copy link
Contributor Author

Just uncomment

// Screen.scrollText(std::to_string(sensorValue).append(">").append(std::to_string(newbrightness)));

This should show you what the reading was you got from the sensor and to which brightness it translated.

I'm not so much a electronic expert, but from what i got, the LDR and the "normal" resistor will just make a voltage divider.

The analog input measures the current left. So you should be able to see the changes with a multimeter (sensor vs. ground) or as changing sensor values.

My coding above just offeres a "profile" for what reading which brightness shall be set.

@WRedux
Copy link

WRedux commented May 16, 2024

Works for me too, now. Used the wrong Pinning. GPIO15 (as stated the Comments) has also an ADC, but its used
by the Wifi. Switched to 34, so its OK now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants