[ESPHOME] LDR Lux shows the wrong way

Hello,

im using this Sensor here:

i added this in esphome:



# LDR Light Sensor
sensor:
  - platform: adc
    pin: A0
    name: "${device_name} Brightness"
    unit_of_measurement: lux
    update_interval: 5s
    filters:
      - lambda: |-
          return (x * 3.3 / 10000.0) * 2000000.0;

it seems to work, but when the sun goes up it shoes vales like 10-300 and when it gets dark it goes up like 600 (LUX)

is there a way to change that to the other way. Dark = values from 0-100 and Light values 100+++ ?

before i was using this function in arduini i found:

void sensorRead() {
    //Record a reading from the light sensor and add it to the array
    
    readings[readIndex] = analogRead(A0); //get an average light level from previouse set of samples
    readIndex = readIndex + 1; // advance to the next position in the array:

    // if we're at the end of the array move the index back around...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }

    //now work out the sum of all the values in the array
    sumBrightness = 0;
    for (int i = 0; i < numReadings; i++)
    {
      sumBrightness += readings[i];
    }

    // and calculate the average:
    lightSensorValue = sumBrightness / numReadings;

    //set the brightness based on ambiant light levels
    color1_brightness = map(lightSensorValue, 0, 1023, 255, 0);
    if (color1_brightness < 2) {
      color1_brightness = 2;
    }
}//end of sensorRead

it was working

Calibrate linear may work.

filters:
  - calibrate_linear:
      - 0.0 -> 600.0
      - 600.0 -> 0.0

hi, thanks for that.

i m not home right now so i can test it out in a couple of days,

does this invert the whole “logic” ?

hi, that does not work.

when its dark and i got this value: 685,74 lux without the liniar calibrate.

when i add the liniar calibrate i get the value: 395.314,25 lux

ADC Part:


# LDR Light Sensor
sensor:
  - platform: adc
    pin: GPIO33
    name: "${device_name} Brightness"
    unit_of_measurement: lux
    update_interval: 5s
    filters:
      - calibrate_linear:
          - 0.0 -> 600.0
          - 600.0 -> 0.0
      - lambda: |-
          return (x * 3.3 / 10000.0) * 2000000.0;

I don’t have any other suggestions. Have you checked the documentation for the sensor itself for a way to invert the values?

ok thanks to this post i got it working:

my code looks like this now:


    filters:
      - lambda: |-
          auto r = map((x * 3.3 / 10000.0) * 2000000.0, 0, 1023, 255, 0);
          if (r > 0) return r;
          return 0;

that now inverts the value and also changes the range from 0-255 so i can now dim my lights with it

2 Likes

I’m glad you found your answer. I have a HX711 and I thought I had seen something to invert values but just couldn’t find it. There is a check box for solution, consider checking it on your above post to make it easier for others to find answers.

Is there a way to use the digital output pin ?
As we can see in the picture, there are 2 pin, 1 for A0 and an other one for D0.
Do you know how to use it ?