Lux Sensor (TSL2561)

How to setup an Adafruit TSL2561 Lux Sensor with the Raspberry Pi.

PARTS:
RPI 3 – https://amzn.to/2VA9pQY
4 Amp Power Adapter – https://amzn.to/2CTptWu
16GB micro SD – https://amzn.to/2SFMwd3
120 pcs jumper cable: https://ebay.to/2VAb9cY
TSL2561 Lux Sensor: https://amzn.to/2SGwteQ

SCHEMATIC:

MODIFIED CODE:

Original Git Project:
https://github.com/ControlEverythingCommunity/TSL2561

# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# TSL2561
# This code is designed to work with the TSL2561_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Light?sku=TSL2561_I2CS#tabs-0-product_tabset-2

import smbus
import time

while True:

    # Get I2C bus
    bus = smbus.SMBus(1)

    # TSL2561 address, 0x39(57)
    # Select control register, 0x00(00) with command register, 0x80(128)
    #		0x03(03)	Power ON mode
    bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
    # TSL2561 address, 0x39(57)
    # Select timing register, 0x01(01) with command register, 0x80(128)
    #		0x02(02)	Nominal integration time = 402ms
    bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)

    time.sleep(0.5)

    # Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
    # ch0 LSB, ch0 MSB
    data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)

    # Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
    # ch1 LSB, ch1 MSB
    data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

    # Convert the data
    ch0 = data[1] * 256 + data[0]
    ch1 = data1[1] * 256 + data1[0]

    # Output data to screen
    print "------" * 5
    print "Full Spectrum(IR + Visible) :%d lux" %ch0
    print "Infrared Value :%d lux" %ch1
    print "Visible Value :%d lux" %(ch0 - ch1)

    time.sleep(5)