When I try to use the I2C compatible module of the illuminance sensor TSL2561 and investigate, it can be troublesome. It relies on a number of Python modules, and somehow the sample program has hundreds of lines.
http://qiita.com/masato/items/1dd5bed82b19477b45d8 http://shimobayashi.hatenablog.com/entry/2015/07/27/001708
I thought that if it was an I2C compatible sensor, I should be able to write it simply with the smbus module.
https://github.com/aike/SimpleTSL2561.py
SimpleTSL2561.py
#!/usr/bin/python
#
# SimpleTSL2561.py by aike
# licenced under MIT License.
#
import smbus
import time
class SimpleTSL2561:
def __init__(self, address=0x39):
self.bus = smbus.SMBus(1)
self.address = address
self.write8(0x80, 0x03) # 0x03=PowerON 0x00=PowerOFF
def write8(self, reg, value):
try:
self.bus.write_byte_data(self.address, reg, value)
except IOError, err:
print "IO Error"
def readU16(self, reg):
try:
result = self.bus.read_word_data(self.address,reg)
return result
except IOError, err:
print "IO Error"
return 0
def setParam(self, param):
# param gain integral
# 0 x 1 13.7 ms
# 1 x 1 101 ms
# 2 x 1 402 ms (default)
# 3 x 16 13.7 ms
# 4 x 16 101 ms
# 5 x 16 402 ms
if param >= 3:
param = param - 3 + 16
self.write8(0x81, param)
def readData(self):
return self.readU16(0xAC)
if __name__ == "__main__":
tsl = SimpleTSL2561()
while True:
print tsl.readData()
time.sleep(1)
I tried to find out why the sample program is complicated by reading the data sheet.
・ Both visible light and infrared light can be detected. ・ Two types of sensitivity, 1x and 16x, can be selected. ・ Integration time can be selected from 13.7ms / 101ms / 402ms
-The value of the acquired data is not proportional to the value of lux, and a conversion coefficient for each case is required. ・ TSL2561 is roughly divided into two types, T, FN, CL series and CS series, and their coefficients are different.
That's why a program that can get accurate lux values for all TSL2561s has a reasonable number of lines. If you just want to support your hardware, you don't need two types of conversion processing and only one type is enough. In the first place, if it is a common application such as "turn on the light when it gets dark", the conversion process of the lux value can be omitted as only visible light, so it can be written as short as the above program.
Also, Adafruit_I2C.py is a thin wrapper for smbus, so it's pretty good without it.
Recommended Posts