GithubHelp home page GithubHelp logo

adafruit / adafruit_circuitpython_veml7700 Goto Github PK

View Code? Open in Web Editor NEW
11.0 19.0 8.0 111 KB

CircuitPython driver for VEML7700 high precision I2C ambient light sensor

License: MIT License

Python 100.00%
hacktoberfest

adafruit_circuitpython_veml7700's Introduction

Introduction

Documentation Status Discord Build Status Code Style: Black

CircuitPython driver for VEML7700 high precision I2C ambient light sensor.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-veml7700

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-veml7700

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-veml7700

Usage Example

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)
    time.sleep(0.1)

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_veml7700's People

Contributors

brianpugh avatar dhalbert avatar evaherrada avatar foamyguy avatar jposada202020 avatar kattni avatar ladyada avatar siddacious avatar sommersoft avatar standsi avatar tcfranks avatar tekktrik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_veml7700's Issues

Parameter settings of integration time may be wrong.

From "Designing the VEML7700 Into an Application"(https://www.vishay.com/docs/84323/designingveml7700.pdf),

REGISTER NAME BIT FUNCTION / DESCRIPTION R / W
ALS_IT 9 : 6
ALS integration time setting
1100 = 25 ms
1000 = 50 ms
0000 = 100 ms
0001 = 200 ms
0010 = 400 ms
0011 = 800 ms

So, I think the parameters of integration time settings should be,
ALS_25MS = const(0xC)
ALS_50MS = const(0x8)
ALS_100MS = const(0x0)
ALS_200MS = const(0x1)
ALS_400MS = const(0x2)
ALS_800MS = const(0x3)

Because of the current wrong parameter settings, both "light" and "lux" values are different from other sample codes.

Soft reset for this sensor

Hi all -

I've been using this sensor for a while now without any issues. Recently I came across an issue where sensor was providing regular Lux values and randomly, sensor was just stuck at one value. In other words, Lux value doesn't at all. I was wondering if there is a way to reset a sensor or soft reboot in the code in a scenario like when sensor doesn't give any new value. I'd appreciate all the possibilities.

Thanks,
Akshay

KeyError 4 getting lux value

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

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_veml7700.py:188

I2C error after soft reboot

I am having a very similar issue outlined in this issue:
adafruit/circuitpython#2060

Essentially, I am able to communicate with the VEML7700 on first boot from my nRF52840, but subsequent soft reboots (such as during code reloads) cause the traceback as follows:

>>> veml7700 = adafruit_veml7700.VEML7700(i2c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_veml7700.py", line 207, in __init__
  File "adafruit_register/i2c_bit.py", line 70, in __set__
  File "adafruit_register/i2c_bit.py", line 65, in __set__
  File "adafruit_bus_device/i2c_device.py", line 143, in write_then_readinto
OSError: [Errno 19] Unsupported operation

Through experimentation, I have found that this can be mitigated by replacing this line:

self.light_shutdown = False # Enable the ambient light sensor

with:

try:
    self.light_shutdown = False
except OSError:
    self.light_shutdown = False

On a cold boot, the first self.light_shutdown = False works successfully. On subsequent soft reboots, it will fail with the traceback previously mentioned; however, running the command again always works. I guess maybe the first attempt flushs some buffer or fixes some invalid state?

I can make a PR if convenient, but just checking if this maybe is just a symptom of an upstream bug that should be fixed instead.

I2C issue: Raspberry Pi 5 support

I tried running the example in README but I get the error libgpiod Python bindings not found. I have latest version of the Blinka installed on my venv:

(.venv) ubo@raspberrypi:~ $ pip3 show adafruit-circuitpython-veml7700
Name: adafruit-circuitpython-veml7700
Version: 1.1.21
Summary: CircuitPython driver for VEML7700 high precision I2C ambient light sensor.
Home-page: 
Author: 
Author-email: Adafruit Industries <[email protected]>
License: MIT
Location: /home/ubo/.venv/lib/python3.11/site-packages
Requires: Adafruit-Blinka, adafruit-circuitpython-busdevice, adafruit-circuitpython-register
Required-by: 

Package info for Blinka:

 pip3 show Adafruit-Blinka
Name: Adafruit-Blinka
Version: 8.26.0
Summary: CircuitPython APIs for non-CircuitPython versions of Python such as CPython on Linux and MicroPython.
Home-page: https://github.com/adafruit/Adafruit_Blinka
Author: Adafruit Industries
Author-email: [email protected]
License: MIT
Location: /home/ubo/.venv/lib/python3.11/site-packages
Requires: adafruit-circuitpython-typing, Adafruit-PlatformDetect, Adafruit-PureIO, pyftdi, rpi-ws281x, RPi.GPIO, sysv-ipc
Required-by: adafruit-circuitpython-busdevice, adafruit-circuitpython-register, adafruit-circuitpython-requests, adafruit-circuitpython-typing, adafruit-circuitpython-veml7700

Here's the error log trace:

ImportError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubo/lightsensor.py", line 2, in <module>
    import board
  File "/home/ubo/.venv/lib/python3.11/site-packages/board.py", line 47, in <module>
    from adafruit_blinka.board.raspberrypi.raspi_5b import *
  File "/home/ubo/.venv/lib/python3.11/site-packages/adafruit_blinka/board/raspberrypi/raspi_5b.py", line 6, in <module>
    from adafruit_blinka.microcontroller.bcm2712 import pin
  File "/home/ubo/.venv/lib/python3.11/site-packages/adafruit_blinka/microcontroller/bcm2712/pin.py", line 5, in <module>
    from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin
  File "/home/ubo/.venv/lib/python3.11/site-packages/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py", line 8, in <module>
    raise ImportError(
ImportError: libgpiod Python bindings not found, please install and try again! See https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh

Please let me know if I can investigate something further. BTW, the script mentioned in the error does not exist:

https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh

DisplayIO Example Wanted

Add a Basic DisplayIO Based Example

We would like to have a basic displayio example for this library. The example should be written for microcontrollers with a built-in display. At a minimum it should show a Label on the display and update it with live readings from the sensor.

The example should not be overly complex, it's intended to be a good starting point for displayio based projects that utilize this sensor library. Try to keep all visual content as near to the top left corner as possible in order to best fascilitate devices with small built-in display resolutions.

The new example should follow the naming convention examples/libraryname_displayio_simpletest.py with "libraryname" being replaced by the actual name of this library.

You can find an example of a Pull Request that adds this kind of example here: adafruit/Adafruit_CircuitPython_BME680#72

We have a guide that covers the process of contributing with git and github: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

If you're interested in contributing but need additional help, or just want to say hi, feel free to join the Discord server to ask questions: https://adafru.it/discord

TypeError: integer is required

I am using Jetson nano and Circuitpython.
Actually it was working but after updating Jetson kernel, I got this error
image

Do you have any idea?

Thanks.

Recommend setting initial light gain to lowest

Please review the PR I submitted, it explains this issue. While the learn guide could be changed to say default is 1x it leaves the sensor open to overflow if a very bright light source is incident when the class is initialized. While this could be a breaking change for code that worked because of the higher gain, it would only affect the raw light value. The lux calculation should be the same as before.

VEML7700 Not loading

So, I'm using a Jetson Nano, which has identical GPIO pins to the Raspberry Pi.

When I load the sample code,I get an error from this line:

veml7700 = adafruit_veml7700.VEML7700(i2c)

The error is:

Traceback (most recent call last):
  File "light.py", line 15, in <module>
    veml7700 = adafruit_veml7700.VEML7700(i2c)
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/adafruit_veml7700.py", line 204, in __init__
    self.light_shutdown = False  # Enable the ambient light sensor
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/adafruit_register/i2c_bit.py", line 65, in __set__
    out_end=1, in_start=1, stop=False)
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/adafruit_bus_device/i2c_device.py", line 157, in write_then_readinto
    in_start=in_start, in_end=in_end, stop=stop)
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/busio.py", line 70, in writeto_then_readfrom
    in_start=in_start, in_end=in_end, stop=stop)
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 63, in writeto_then_readfrom
    readin = self._i2c_bus.read_i2c_block_data(address, buffer_out[out_start:out_end][0], in_end-in_start)
  File "/home/nvidia/.virtualenvs/deep_learning/lib/python3.6/site-packages/Adafruit_PureIO/smbus.py", line 215, in read_i2c_block_data
    ioctl(self._device.fileno(), I2C_RDWR, request)
BlockingIOError: [Errno 11] Resource temporarily unavailable
Exiting... 
Cleaning up pins

If I do a scan on the I2C bus, I am getting [16], which seems to be a value on the bus.

print(i2c.scan())

Any idea what's going on here? Should I be selecting a specific bus? Is it getting data, but not able to manage the data? Kind of at a loss what the error means here..

RuntimeError: Unable to enable VEML7700 device

Running Micropython on Pi Pico and attempting to use the VEML7700 connected to a TCA9548a on Channel 0. Platform_Detector and Blinka src files are installed. Other adafruit libraries are working fine. As you can see in the output below my VEML6070 is communicating on channel 1 without issues.

import sys
import time
import board
import busio
import adafruit_tca9548a
import adafruit_veml7700
import adafruit_veml6070

# Setting Sampling Frequency
sample_half_sec = time.ticks_ms()
sample_sec = time.ticks_ms()

i2c = busio.I2C(board.GP5, board.GP4, frequency=400000)
tca = adafruit_tca9548a.TCA9548A(i2c)

for channel in range(8):
    if tca[channel].try_lock():
        print("Channel {}:".format(channel), end="")
        addresses = tca[channel].scan()
        print([hex(address) for address in addresses if address != 0x70])
        tca[channel].unlock()

# UV SENSOR
uv = adafruit_veml6070.VEML6070(tca[1])
# Alternative constructors with parameters
# uv = adafruit_veml6070.VEML6070(tca[1], 'VEML6070_1_T')
# uv = adafruit_veml6070.VEML6070(tca[1], 'VEML6070_HALF_T', True)

def UV_DATA():
    uv_raw = uv.uv_raw
    risk_level = uv.get_index(uv_raw)
    print("Reading: {0} | Risk Level: {1}".format(uv_raw, risk_level))

# LUX SENSOR
luxsensor = adafruit_veml7700.VEML7700(tca[0])
print("Light Integration Time:", luxsensor.light_integration_time)
print("Ambient Light:", luxsensor.lux)

def LUX_DATA():
    print("Ambient Light:", luxsensor.lux)

while True:
    if (time.ticks_ms() - sample_half_sec > 500):
        sample_half_sec = time.ticks_ms()


    if (time.ticks_ms() - sample_sec > 1000):
        UV_DATA()
        LUX_DATA()
        sample_sec = time.ticks_ms()

REPL OUTPUT:
MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040
Type "help()" for more information.

%Run -c $EDITOR_CONTENT
Channel 0:['0x10']
Channel 1:['0x38', '0x39']
Channel 2:['0x77']
Channel 3:[]
Channel 4:['0x61']
Channel 5:[]
Channel 6:[]
Channel 7:[]
Reading: 0 | Risk Level: LOW
Traceback (most recent call last):
File "", line 35, in
File "/lib/adafruit_veml7700.py", line 197, in init
RuntimeError: Unable to enable VEML7700 device

I've flashed the same Pico with CircuitPython and the sensor works properly with the same library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.