2016年6月19日日曜日

RaspberryPI3で照度センサー(TSL2561)を使ってみる

RaspberryPI3で照度センサー(TSL2561)を使ってみる

照度センサー(TSL2561)をRaspberryPI3に接続してみた。

1.Raspberry PI3でI2Cを有効にする

$ sudo raspi-config

Advance Option->I2Cで有効にするし、再起動。

$ lsmod

i2c_bcm2708,i2c_devが見れていればOK.

2.以下のようにRaspberry PI3と接続

GND (TSL2561) -> GPIO04 (Raspberry Piの9pin)
VCC (TSL2561) -> 3.3v (Raspberry Piの1pin)
SDA (TSL2561) -> GPIO08 SDA1(Raspberry Piの3pin)
SCL (TSL2561) -> GPIO09 SCL(Raspberry Piの5pin)
$ sudo apt-get install i2c-tools python-smbus
$ sudo i2cdetect -y 1
$ sudo apt install libi2c-dev

このようにでてくればOK
enter image description here

3.PythonからI2Cを叩く準備

1. wiringpiのインストール

以下を参考。
http://takashin10mura.blogspot.jp/2016/06/wiringpipython.html

2. SMBUSのインストール

python-smbusはよくでてくるがpython3で使うとなると少し手間がかかります。
以下のようにすればできました。

wget http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.1.orig.tar.bz2

tar -xf i2c-tools_3.1.1.orig.tar.bz2
cd i2c-tools-3.1.1/py-smbus
mv smbusmodule.c smbusmodule.c.orig # make backup
$ sudo wget https://gist.githubusercontent.com/sebastianludwig/c648a9e06c0dc2264fbd/raw/2b74f9e72bbdffe298ce02214be8ea1c20aa290f/smbusmodule.c
$ sudo vi setup.py
  /usr/bin/python3に修正
$ sudo python3 setup.py build
$ sudo python3 setup.py install

3. smbusの確認

$ python3
$ import smbus
ここでエラーがでなければOK

4.TSL2561の制御

データシートを参考に以下のコードで照度を取得できました。

#! /usr/bin/python3
#coding: UTF-8
#TSL2561 照度センサー

import smbus
import time
import math

bus = smbus.SMBus(1)        #I2cバス番号
address = 0x39              #I2Cアドレス
command = 0x00

#i2c データ出力 1byte
def write(regist, value):
    bus.write_byte_data(address, regist, value)

#i2c データ読み込み 1byte
def read(regist):
    value = bus.read_byte_data(address, regist)
    return value

#i2c データ読み込み ブロック
def blockread(regist, num):
    value = bus.read_i2c_block_data(address, regist, num)
    return value

def ratio_calc(ambient, ir):
    # Avoid division by zero
    if (float(ambient) == 0):
        ratio = 9999
    else:
        ratio = (ir / float(ambient))

    return ratio

def init():
    #read ID
    print("id_reg=0x{0:02x}".format(read(0x8A)))
    #device enable
    write(0x80,0x03)
    power = 0x03
    while power != 0x03:
        power = read(0x80) & 0x03
        print("{0:02x}".format(power))
    #print("reg=0x{0:02x}".format(read(0x80)))

    #gain setting
    write(0x81,0x11)
    print("reg=0x{0:02x}".format(read(0x81)))

def read_lux():
    value0 = blockread(0xAC,2)
    ch0 = 256*value0[1]+value0[0]
#    print("reg=0x{0:04x}".format(ch0))

    value1 = blockread(0xAE,2)
    ch1 = 256*value1[1]+value1[0]
#    print("reg=0x{0:04x}".format(ch1))

    ratio = ratio_calc(ch0,ch1)
    if ((ratio >= 0) & (ratio <= 0.52)):
        lux = (0.0315 * ch0) - (0.0593 * ch0 * (ratio**1.4))
    elif (ratio <= 0.65):
        lux = (0.0229 * ch0) - (0.0291 * ch1)
    elif (ratio <= 0.80):
        lux = (0.0157 * ch0) - (0.018 * ch1)
    elif (ratio <= 1.3):
        lux = (0.00338 * ch0) - (0.0026 * ch1)
    elif (ratio > 1.3):
        lux = 0

    return lux

def main():
    init()
    counter = 0
    while True:
        counter += 1
        if counter == 5:
            lux = read_lux()
            print("lux={0:f}".format(lux))
            counter = 0

        time.sleep(1.0)

if __name__ == '__main__':
    main()

0 件のコメント :

コメントを投稿