adafruit / Adafruit_CircuitPython_VEML7700

CircuitPython driver for VEML7700 high precision I2C ambient light sensor
MIT License
11 stars 8 forks source link

KeyError 4 getting lux value #18

Closed RobCranfill closed 3 years ago

RobCranfill commented 3 years ago

The following code, a very slight modification of the standard test code, fails:

import time
import board
import adafruit_veml7700

i2c = board.I2C()  # uses board.SCL and board.SDA
veml7700 = adafruit_veml7700.VEML7700(i2c)

while True:
    # print("Ambient light:", veml7700.light) # this works
    print("Ambient light:", veml7700.lux) # this does not
    time.sleep(1.0)

The failure is this:

Traceback (most recent call last):
  File "v1.py", line 10, in <module>
    print("Ambient light:", veml7700.lux)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 239, in lux
    return self.resolution() * self.light
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 216, in resolution
    * (gain_max / self.gain_value())
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 194, in integration_time_value
    return self.integration_time_values[integration_time]
KeyError: 4
RobCranfill commented 3 years ago

I am running adafruit_veml7700 1.1.4 (the latest), as I have been for months. This stopped working a week or two ago.

caternuson commented 3 years ago

Did you change anything else? Try updating your CP firmware and libraries to all latest versions.

Can't recreate this with a quick test:

Adafruit CircuitPython 7.0.0 on 2021-09-20; Adafruit QT Py M0 with samd21e18
>>> import board
>>> import adafruit_veml7700
>>> veml = adafruit_veml7700.VEML7700(board.I2C())
>>> veml.light
5507
>>> veml.lux
317.491
>>> 

EDIT As another test/check - try printing the light_integration_time before the call to lux.

while True:
    print("Light Integration Time:", veml7700.light_integration_time)
    print("Ambient light:", veml7700.lux)
    time.sleep(1.0)
RobCranfill commented 3 years ago

Thanks for the reply.

I'm not using CircuitPython, I'm using python 3; is that an issue? I thought I updated the libraries:

@.***:~/proj/blatb $ pip3 install adafruit-circuitpython-veml7700 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: adafruit-circuitpython-veml7700 in /home/pi/.local/lib/python3.7/site-packages (1.1.4) Requirement already satisfied: adafruit-circuitpython-register in /home/pi/.local/lib/python3.7/site-packages (from adafruit-circuitpython-veml7700) (1.9.4) Requirement already satisfied: Adafruit-Blinka in /home/pi/.local/lib/python3.7/site-packages (from adafruit-circuitpython-veml7700) (6.3.0) Requirement already satisfied: adafruit-circuitpython-busdevice in /home/pi/.local/lib/python3.7/site-packages (from adafruit-circuitpython-veml7700) (5.0.5) Requirement already satisfied: sysv-ipc>=1.1.0 in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (1.1.0) Requirement already satisfied: rpi-ws281x>=4.0.0 in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (4.2.5) Requirement already satisfied: RPi.GPIO in /usr/lib/python3/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (0.7.0) Requirement already satisfied: pyftdi>=0.40.0 in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (0.52.9) Requirement already satisfied: Adafruit-PlatformDetect>=3.1.0 in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (3.2.0) Requirement already satisfied: Adafruit-PureIO>=1.1.7 in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-veml7700) (1.1.8) Requirement already satisfied: pyusb>=1.0.0 in /home/pi/.local/lib/python3.7/site-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-veml7700) (1.1.1) Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-veml7700) (3.4)

Sorry, I realize you are not the Adafruit helpdesk person. ;-) I've done a little Googling with no success.

On Wed, Oct 6, 2021 at 4:50 PM Carter Nelson @.***> wrote:

Did you change anything else? Try updating your CP firmware and libraries to all latest versions.

Can't recreate this with a quick test:

Adafruit CircuitPython 7.0.0 on 2021-09-20; Adafruit QT Py M0 with samd21e18>>> import board>>> import adafruit_veml7700>>> veml = adafruit_veml7700.VEML7700(board.I2C())>>> veml.light5507>>> veml.lux317.491>>>

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-937330265, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZTNOVAWUY7U23INVGLUFTODZANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

caternuson commented 3 years ago

Python3 on a Raspberry Pi should be fine, assuming you are using via Blinka: https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/overview

Just did a quick test on a Pi with Python3 and still seems to work OK:

(blinka) pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board
>>> import adafruit_veml7700
>>> veml = adafruit_veml7700.VEML7700(board.I2C())
>>> veml.light
5435
>>> veml.lux
312.192
>>> veml.light_integration_time
0
>>> 

version check:

(blinka) pi@raspberrypi:~ $ pip3 list | grep veml
adafruit-circuitpython-veml7700  1.1.7    
RobCranfill commented 3 years ago

Thanks, I’ll let you know if I figure out what my problem is.

I have a work-around in using “light” instead of “lux”, but it’s not really the same, right?

On Wed, Oct 6, 2021 at 17:46 Carter Nelson @.***> wrote:

Python3 on a Raspberry Pi should be fine, assuming you are using via Blinka: https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/overview

Just did a quick test on a Pi with Python3 and still seems to work OK:

(blinka) @.***:~ $ python3Python 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import board>>> import adafruit_veml7700>>> veml = adafruit_veml7700.VEML7700(board.I2C())>>> veml.light5435>>> veml.lux312.192>>> veml.light_integration_time0>>>

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-937353959, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZT3PP2V7Z55M2ZCJITUFTUX3ANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

--

caternuson commented 3 years ago

No. They are not the same.

Have you tried printing the value of light_integration_time prior to the call to lux? Do you also get 0 as shown above?

RobCranfill commented 3 years ago

I do not get zero for light_integration_time . Could this be an issue?

@.***:~ $ python3 Python 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import board import adafruit_veml7700 veml = adafruit_veml7700.VEML7700(board.I2C()) veml.light_integration_time 4 veml.light 830 veml.lux Traceback (most recent call last): File "", line 1, in File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 239, in lux return self.resolution() * self.light File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 216, in resolution

  • (gain_max / self.gain_value()) File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 194, in integration_time_value return self.integration_time_values[integration_time] KeyError: 4

On Thu, Oct 7, 2021 at 7:43 AM Carter Nelson @.***> wrote:

No. They are not the same.

Have you tried printing the value of light_integration_time prior to the call to lux? Do you also get 0 as shown above?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-937861666, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZWTURLXGU3RO6C3APDUFWWYNANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

RobCranfill commented 3 years ago

Oho! So I tried setting the integration time to zero, and guess what? It works (or at any rate it doesn't throw an exception)

veml.light_integration_time = 0 veml.lux 38.7072

I will try this in my project. (I have an RPi driving an LCD display that shows output from my weather station; the light sensor automatically adjusts the LCDs backlight so it's not too bright at night. See https://github.com/RobCranfill/blatb if you care. ;-)

Thanks for your help!

On Thu, Oct 7, 2021 at 11:11 AM Rob Cranfill @.***> wrote:

I do not get zero for light_integration_time . Could this be an issue?

@.***:~ $ python3 Python 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import board import adafruit_veml7700 veml = adafruit_veml7700.VEML7700(board.I2C()) veml.light_integration_time 4 veml.light 830 veml.lux Traceback (most recent call last): File "", line 1, in File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 239, in lux return self.resolution() * self.light File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 216, in resolution

  • (gain_max / self.gain_value()) File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 194, in integration_time_value return self.integration_time_values[integration_time] KeyError: 4

On Thu, Oct 7, 2021 at 7:43 AM Carter Nelson @.***> wrote:

No. They are not the same.

Have you tried printing the value of light_integration_time prior to the call to lux? Do you also get 0 as shown above?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-937861666, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZWTURLXGU3RO6C3APDUFWWYNANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

caternuson commented 3 years ago

Yes, getting a 4 would be an issue. That is not a valid setting, which is what is throwing the key error. 0 is a valid setting. The valid settings are:

    ALS_25MS = const(0xC)
    ALS_50MS = const(0x8)
    ALS_100MS = const(0x0)
    ALS_200MS = const(0x1)
    ALS_400MS = const(0x2)
    ALS_800MS = const(0x3)

So the question is how it got set to 4 in the first place? Were you doing that?

RobCranfill commented 3 years ago

No, I was not setting light_integration_time.

I can't remember now if I did a cold restart on this machine. It had been up for over 30 days when it began throwing the error; I restarted it, it still errored, then I started looking into the code.

At any rate, I guess the "bug" is solved. Shall I close it, or did you?

On Thu, Oct 7, 2021 at 11:20 AM Carter Nelson @.***> wrote:

Yes, getting a 4 would be an issue. That is not a valid setting, which is what is throwing the key error. 0 is a valid setting. The valid settings are:

ALS_25MS = const(0xC)
ALS_50MS = const(0x8)
ALS_100MS = const(0x0)
ALS_200MS = const(0x1)
ALS_400MS = const(0x2)
ALS_800MS = const(0x3)

So the question is how it got set to 4 in the first place? Were you doing that?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-938042356, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZQQMCA5XDZOSH5NKXLUFXQFBANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

caternuson commented 3 years ago

See if you can recreate the issue. If so, we can continue to take a look. If not, then, yes, please close. Also, nothing really urgent here, so OK to keep this open for a little while if you want to continue exercise things and see if it happens again.

RobCranfill commented 3 years ago

Cool, I will do that. I’ll try it from a cold boot and see what happens.

On Thu, Oct 7, 2021 at 11:45 Carter Nelson @.***> wrote:

See if you can recreate the issue. If so, we can continue to take a look. If not, then, yes, please close. Also, nothing really urgent here, so OK to keep this open for a little while if you want to continue exercise things and see if it happens again.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adafruit/Adafruit_CircuitPython_VEML7700/issues/18#issuecomment-938060164, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLQHZRYWODJN5JNWSVYDNTUFXTETANCNFSM5FP5YOMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

--

RobCranfill commented 3 years ago

Cannot reproduce. Immediate cause of error, as mentioned above, was light_integration_time having a value of 4, which is not valid. I don't know how that happened, and I have not bee able to reproduce it.

RobCranfill commented 8 months ago

I had this exception crop up again. I do seem to be able to fix it (I thought I had already done this, but apparently not) by doing:

  veml7700 = adafruit_veml7700.VEML7700(i2c)

  # prevent key error? (is this the best way?)
  #     File "/home/pi/.local/lib/python3.7/site-packages/adafruit_veml7700.py", line 194, in integration_time_value
  #       return self.integration_time_values[integration_time]
  #   KeyError: 6
  veml7700.light_integration_time = veml7700.ALS_100MS