123aaron
Posts: 1
Joined: Mon Nov 20, 2017 9:54 am

i2c communication with VEML7700 Light sensor help (python)

Mon Nov 20, 2017 10:04 am

Hi,

I am struggling to communicate with a VEML700 ambient light detector.
I can detect its presence with i2cdetect and it correctly gives address 0x10.

But from here I am having trouble get values from it.
I'm new to i2c communication, but have managed to get a number of other sensors up and running.

The datasheet is a little different to the other sensors I've been using and this is where I am probably stumbling.
The sheet says to write:
Slave addr---Command code---Data byte (LSB)---Data byte (MSB)
read:
Slave addr---Command code---Slave addr---(response) LSB---(response) MSB

How does it translate to, say, the "write_i2c_block_data()" and "read_i2c_block_data" commands?

Any pointers you guy could give me would be greatly appreciated.


Datasheet:
https://www.vishay.com/docs/84286/veml7700.pdf

robidouxj
Posts: 4
Joined: Thu Mar 22, 2018 5:05 pm

Re: i2c communication with VEML7700 Light sensor help (python)

Thu Mar 22, 2018 5:18 pm

I had similar struggles. I contacted Vishay and they were very helpful. Now that I've found a solution, I thought I'd go back and share it where I was searching. I hope this sample code helps future users:

Code: Select all

import smbus
import time

bus = smbus.SMBus(1)

addr = 0x10

#Write registers
als_conf_0 = 0x00
als_WH = 0x01
als_WL = 0x02
pow_sav = 0x03

#Read registers
als = 0x04
white = 0x05
interrupt = 0x06


# These settings will provide the max range for the sensor (0-120Klx)
# but at the lowest precision:
#              LSB   MSB
confValues = [0x00, 0x13] # 1/8 gain, 25ms IT (Integration Time)
#Reference data sheet Table 1 for configuration settings

interrupt_high = [0x00, 0x00] # Clear values
#Reference data sheet Table 2 for High Threshold

interrupt_low = [0x00, 0x00] # Clear values
#Reference data sheet Table 3 for Low Threshold

power_save_mode = [0x00, 0x00] # Clear values
#Reference data sheet Table 4 for Power Saving Modes

bus.write_i2c_block_data(addr, als_conf_0, confValues)
bus.write_i2c_block_data(addr, als_WH, interrupt_high)
bus.write_i2c_block_data(addr, als_WL, interrupt_low)
bus.write_i2c_block_data(addr, pow_sav, power_save_mode)

while True:
	#The frequency to read the sensor should be set greater than
	# the integration time (and the power saving delay if set).
	# Reading at a faster frequency will not cause an error, but
	# will result in reading the previous data
	time.sleep(.04) # 40ms 

	word = bus.read_word_data(addr,als)
	
	gain = 1.8432 #Gain for 1/8 gain & 25ms IT
	#Reference www.vishay.com/docs/84323/designingveml7700.pdf
	# 'Calculating the LUX Level'
	
	val = word * gain
	val = round(val,1) #Round value for presentation
		
	print "Lux: " + str(val) + " lx"
Good luck.

Joe Robidoux

andreraspberry
Posts: 1
Joined: Sat Dec 22, 2018 9:35 pm

Re: i2c communication with VEML7700 Light sensor help (python)

Mon Dec 24, 2018 12:45 pm

Hi, I used the above code, and it works. thanks Joe Robidoux.
The folowing code I use for VEML6030, is basicaly the same code. I put the correction formula for values above 1000lux.


# this program reads sensor VEML6030 and correct non linearity values above 1000 lux
# original code based on veml7700 example of Joe Robidoux.

import smbus
import time

bus = smbus.SMBus(1)

addr = 0x10

#Write registers
als_conf_0 = 0x00
als_WH = 0x01
als_WL = 0x02
pow_sav = 0x03

#Read registers
als = 0x04
white = 0x05
interrupt = 0x06


# These settings will provide the range for the sensor: (0 - 15099 lx)
#
# LSB MSB
confValues = [0x00, 0x18] # 1/4 gain, 100ms IT (Integration Time)


#Reference data sheet Table 1 for configuration settings
#MSB = 00011000 = 18 in hexadecimal ( als gain = 1/4, bits 12:11 = 11 )
#LSB = 00000000 = 00 ( integration time = 100ms, bits 9:6 = 0000 )

interrupt_high = [0x00, 0x00] # Clear values
#Reference data sheet Table 2 for High Threshold

interrupt_low = [0x00, 0x00] # Clear values
#Reference data sheet Table 3 for Low Threshold

power_save_mode = [0x00, 0x00] # Clear values
#Reference data sheet Table 4 for Power Saving Modes

bus.write_i2c_block_data(addr, als_conf_0, confValues)
bus.write_i2c_block_data(addr, als_WH, interrupt_high)
bus.write_i2c_block_data(addr, als_WL, interrupt_low)
bus.write_i2c_block_data(addr, pow_sav, power_save_mode)



while True:
#The frequency to read the sensor should be set greater than
# the integration time (and the power saving delay if set).
# Reading at a faster frequency will not cause an error, but
# will result in reading the previous data
time.sleep(.04) # 40ms

word = bus.read_word_data(addr,als)
# wordIR = bus.read_word_data(addr,white)

gain = 0.2304 # gain=1/4 and int time=100ms, max lux=15099 lux


#Reference www.vishay.com/docs/84367/designingveml6030.pdf
# 'Calculating the LUX Level'

val = word * gain
#valIR = wordIR * gain
#val = round(val,1)
#valIR = round(valIR,1)

#Reference www.vishay.com/docs/84367/designingveml6030.pdf
# 'Calculating the LUX Level'

valcorr = (6.0135E-13*val**4)-(9.392E-9*val**3)+(8.1488E-5*val**2)+(1.0023E0*val)
valcorr = round(valcorr,1) #Round corrected value for presentation
val = round(val,1) #Round value for presentation


print ("Measures: " + str(val) + " Lux" , str(valcorr) + "lux corrected")

Return to “Python”