Hardware Reference
In-Depth Information
The TSL2561 sensor reports as device address 0x39 on the I2C bus, and sure enough,
i2cdetect sees it. If you want to change the device address, you can connect a wire to
the ADDR pin on the TSL2561 breakout board. Connect it to a ground (GND) pin to set
the address to 0x29, or connect it to a 3.3 V power pin to set the address to 0x49.
Accessing the Sensor via Python
The easiest way to get readings from the TSL2561 sensor is to use the Adafruit I2C
Python module. The simplest value to understand from the TSL2561 is lux, but the
TSL2561 doesn't read in those units directly; you have to write code to do the math-
ematical conversion.
As a further complication, you can adjust the gain on the TSL2561 sensor. If the gain
is set low (1), the sensor will calculate more accurate readings in bright light to avoid
sensor saturation. If the gain is set high (16), the sensor will calculate more accurate
readings in low light by boosting the sensitivity. You can also generate a reading with
“automatic” gain, which cycles between low and high gain.
You will need to install the python-smbus and Adafruit_I2C libraries. The python-smbus
library is included in the Pidora and Raspbian package repositories, so you can install
it normally. Here's the command for Pidora:
$ su -c 'yum install python-smbus'
And here's how to install it on Raspbian:
$ su -c 'apt-get install python-smbus'
The Adafruit_I2C code is in the Adafruit Raspberry Pi Python Code GitHub repository.
Check out a copy of it, and change into the Adafruit_I2C directory:
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C
From this directory, you can run a Python script to calculate the lux value measured
by the TSL2561. This script was written by Ty Brown and is included here with his
permission:
#!/usr/bin/python
import sys
import smbus
import time
from Adafruit_I2C import Adafruit_I2C
### Written for Python 2 <-!!!
### Big thanks to bryand, who wrote the code that I borrowed heavily from/
was inspired by
Search WWH ::




Custom Search