-
Notifications
You must be signed in to change notification settings - Fork 34
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
ltr390 light sensor reports very low UVI values and resolution settings not working #4380
Comments
@OttoWinter |
or @jesserockz maybe you can have a little peak ? |
In my case with the default settings, the light reading reaches a maximum of 52428.6 lux while BH1750 under the same conditions exceeds that value. The UV Index reading shows absurd values, being minimum under direct sunlight and high when sunlight is not direct... Using the modification mentioned here, adding the component as external does not seem to correct the problem. For light readings, higher numbers are reached when configuring 20bit, but the lux value obtained is static. UV Index values are still absurd |
"the light reading reaches a maximum of 52428.6 lux" - the formula is "The UV Index reading shows absurd values, being minimum under direct sunlight and high when sunlight is not direct" - I have not seen that. So that is a different issue and stuff I mentioned here is not going to address that issue. BTW, als and uv are values directly from the sensor, while lux and UVI are calculated in the code of the esphome component. Until the couple issues are fixed in the esphome repository, here are two possible workarounds: workaround 1: workaround 2: I have been using this file for the last two months and it has been giving me outputs that are reasonable relative to expectation, but I have no other light measurement equipment to verify. This file has additional changes to the one in the opening post. Other than a bunch of superfluous changes relating to code formatting and commenting, the one additional functional change is that the data sensing is only enabled during each refresh interval. The original code has the data sensing enabled all the time, where the chip would do an automatic data sensing after each read. With my limited testing, the data acquired would then sit in the receive register and would be read out on the next esphome refresh interval, resulting in the data always being one refresh interval delayed. |
Any follow up on this @jesserockz would love to see this working in a normal way.... ? @plee3001 I tried to get 'workaround 2' working but it seems that i'm not getting the LTR390 files locally in my ESPhome directory and than under the specific module... |
@AussiSG
Create a directory in your "esphome" directory (where the .yaml files are) with a name that matches the path entry above ("my_components" in this example). |
Hi @plee3001, I submitted the original code for this component. Thank you for digging into this issue - unfortunately I have only had time now to look at it. To be honest when I wrote this component I was referencing the Adafruit implementation without thinking too hard about whether it was correct or not. I'll put together a PR with the changes soon! See my notes on the issues below. Issue 2 Absolutely correct, the code doesn't set resolution to the configured value. Issue 1 I think you're right that the conversion rate of 2300 counts/UVI is only for the specific combination of gain and resolution. However I found that after fixing issue 2, that the formula in the datasheet is correct for that specific combination of gain and resolution. In other words we don't need to multiply by 4. After some googling I found https://github.com/torvalds/linux/blob/master/drivers/iio/light/ltr390.c, where @ArchUsr64 has provided a formula that assumes linear scaling of sensitivity, which is:
where For example the defaults of X3 (3) gain and 18 bit resolution (100ms) I have tested this value to confirm and cross referenced live UVI data from https://www.arpansa.gov.au/our-services/monitoring/ultraviolet-radiation-monitoring/ultraviolet-radiation-index. It's not absolutely perfect since reported UVI values are slightly different between combinations of gain and resolution. But it is probably as good as we are going to get until someone can provide calibration points. |
Thanks for putting up this component in the first place. I do have a suggestion. Leave the UV detection to x18 gain all the time. From the testing I have done, the UV reading would never max out the input register with a number of bits to spare, so it is nice to just have the max precision regardless of the ambient light gain setting. (The ambient light detection do max out the input register easily under direct sun, so one may want to adjust the gain setting for that to tailor to one's application). |
I'm not convinced that "it doesn't use up all the bits" is a strong reason to lock the UV sensor to that gain setting. It might be that you can max the sensor counts if using a strong artificial UV source. I think the better solution is to:
I think if there's interest in supporting different gain/resolution for ALS/UV sensors this should be put into a feature request PR. |
The problem
Issue 1: When using the ltr390 sensor component, it reports very low UVI values. For example, when under direct midday sun, when UVI of 7 or greater is expected, it would report UVI of less than 1.
Issue 2: When changing the resolution to something other than default, the raw outputs, als or uvs, do not scale with the resolution. Instead the calculated light lux (lx) output would change by factors of 2 even though it should stay more or less the same under the same lighting conditions.
Looking into the source file, I think I have found and fixed the issues. Attached is the modified .cpp file that seems to give reasonable values. I have no light measurement tool to verify.
ltr390.cpp.txt
The cause of issue 1 seems to be the interpretation of the ambiguous datasheet. The UVI Conversion Formula in the datasheet has no terms to compensate for gain and resolution settings. While on Figure 4.6, it implies that the Sensitivity of 2300 counts/UVI is tied to Gain 18x and Resolution 20-bit.
In my experiments, even under direct midday sun, the UV ADC output values are still a number of bits short of the max afforded by the ADC register at Gain 18x. So it is best to use Gain 18x for UV measurement all the time. In the .cpp file attached, it uses the user config gain setting for the ambient light measurement and uses 18x for UV measurement. The calculation of UVI is changed to include a factor ( 4 / INT ) to account for the different resolution settings.
The cause of issue 2 is that the user config resolution setting is never applied to the ltr390 chip. There are a few lines in the original source in setup() with a comment of "Set resolution". But the few lines end up not really changing anything. In the attached .cpp, it has been modified to actually write the user config resolution setting to the ltr390 chip.
Which version of ESPHome has the issue?
2022.12.8 is what I am using
What type of installation are you using?
pip
Which version of Home Assistant has the issue?
n/a, debug using esphome log
What platform are you using?
ESP8266
Board
d1 mini
Component causing the issue
ltr390
Example YAML snippet
Anything in the logs that might be useful for us?
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: