A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2

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)

Causes of complexity

I tried to find out why the sample program is complicated by reading the data sheet.

TSL2561 is multifunctional

・ 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

Special data specifications for TSL2561

-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

A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Use the Grove sensor on the Raspberry Pi
Use PIR motion sensor with raspberry Pi
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Use python on Raspberry Pi 3 to light the LED with switch control!
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
[Memo] Connect Raspberry Pi Zero to the Internet with a single USB cable! (Use nfs, no SD card / keyboard / mouse / monitor required)
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
I tried using the DS18B20 temperature sensor with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Make a note of what you want to do in the future with Raspberry Pi
Using a webcam with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried to automate the watering of the planter with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
[Memo] How to use BeautifulSoup4 (2) Display the article headline with Requests
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
A memo when connecting bluetooth from a smartphone / PC to Raspberry Pi 4
[Memo] How to use BeautifulSoup4 (3) Display the article headline with class_
Periodically log the value of Omron environment sensor with Raspberry Pi
Use "TSL2561 Illuminance Sensor Module (Manufacturer Part Number: TSL2561)" manufactured by Strawberry Linux with Rasberry pi 3 (Summary)
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Build a Tensorflow environment with Raspberry Pi [2020]
Improved motion sensor made with Raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
[Python] Explains how to use the range function with a concrete example
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
How to use Raspberry Pi pie camera Python
Specify the Python executable to use with virtualenv
The easiest way to use OpenCV with python
Use Majoca Iris elongated LCD with Raspberry Pi
Observe the Geminids meteor shower with Raspberry Pi 4
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Use "TSL2561 Illuminance Sensor Module Manufacturer Part Number: TSL2561" manufactured by Strawberry Linux on Raspberry pi 3 (trial and error)
Save the object to a file with pickle
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
How to make a Raspberry Pi that speaks the tweets of the specified user
Connect to MySQL with Python on Raspberry Pi
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
[Note] Use a wired LAN connection device with WiFi-Eth bridge on Raspberry Pi 4B
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
When I tried to do socket communication with Raspberry Pi, the protocol was different
A memo on how to overcome the difficult problem of capturing FX with AI
A memo of misunderstanding when trying to load the entire self-made module with Python3
A memo connected to HiveServer2 of EMR with python