Using the digital illuminance sensor TSL2561 with Raspberry Pi

In addition to temperature and humidity sensors, there is an illuminance sensor as an easy-to-use sensor. The unit can be measured in lux. This time, we have prepared an Adafruit TSL2561 digital illuminance sensor. There is also a motion sensor that uses infrared rays as an optical sensor. It seems that you can do something like IoT when used in combination.

Breadboard wiring

The breadboard wiring is for the Arduino version on Adafruit's lern site, but can be found at TSL2561 Luminosity Sensor.

If the wiring is successful, you can check the 0x39 address.

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I2C driver

Python 2

The driver for retrieving data from the I2C bus is Python's [Adafruit_I2C.py] in the Adafruit-Raspberry-Pi-Python-Code repository. ](Https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_I2C/Adafruit_I2C.py) is used. This driver works with Python 2, which is installed by default on the Raspberry Pi.

$ cd ~/python_apps
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C

Python 3

Quick2Wire is also used by RPi-Light-Sensor etc. I will. I haven't installed Python 3 on my Raspberry Pi, so I'll prepare a virtualenv and use it next time.

TSL2561 program

The program to measure the illuminance from TSL2561 is [TSL2561.py](https://github. com / seanbechhofer / raspberrypi / blob / master / python / TSL2561.py) is introduced.

Also, in Raspberry Pi Hacks, tsl2561-lux.py The method using -lux.py) is posted. Both are almost the same code, but this time I will download tsl2561-lux.py from the rpihacks repository to the ʻAdafruit_I2C` directory and use it.

$ cd ./Adafruit_I2C
$ wget https://raw.githubusercontent.com/spotrh/rpihacks/master/tsl2561-lux.py
$ chmod +x tsl2561-lux.py

Even if you comment out the end of tsl2561-lux.py, it can be called from the outside as a module.

~/python_apps/Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C/tsl2561-lux.py


...
#oLuxmeter=Luxmeter()

#print "LUX HIGH GAIN ", oLuxmeter.getLux(16)
#print "LUX LOW GAIN ", oLuxmeter.getLux(1)
#print "LUX AUTO GAIN ", oLuxmeter.getLux()

Unfortunately, the file name contains a hyphen, so Python cannot import it as it is. Replace it with an underscore to create a symbolic link.

$ ln -s tsl2561-lux.py tsl2561_lux.py

Create tsl2561.py as an entry point to measure the illuminance using the Luxmeter class.

~/python_apps/Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C/tsl2561.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from time import sleep
from tsl2561_lux import Luxmeter

if __name__ == "__main__":

    while True:
        tsl=Luxmeter()
        sensor_value = tsl.getLux()
        print(sensor_value)
        sleep(5)

Run

Run the program that measures the illuminance at 5-second intervals. The unit is lux. The data has some variations, probably because of the instantaneous measurement.

$ chmod u+x tsl2561.py
$ ./tsl2561.py
1691.71324345
961.73944068
1274.95548759
690.479479419
14.30208
0
11.50656
388.866054431
1316.01531121
0
0

Illuminate the sensor with the iPhone flashlight app to increase lux. Next time, I will try to acquire stable data by measuring in a pitch-black room.

19088.793866
0
0

UPDATE 2015-05-25

I modified the program to publish to MQTT when it exceeds 500 lux. The measurement interval is also shortened to 3 seconds.

~/meshblu/publish.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from time import sleep
from tsl2561_lux import Luxmeter
import paho.mqtt.client as mqtt
import json

def sensor():
    sensor = W1ThermSensor()
    celsius = sensor.get_temperature()
    return celsius

def on_connect(client, userdata, rc):
    print("Connected with result code " + str(rc))

def on_publish(client, userdata, mid):
    print("publish: " + str(mid))

def main():
    client = mqtt.Client(client_id='',clean_session=True,
        protocol=mqtt.MQTTv311)

    client.username_pw_set("{username}","{password}")
    client.connect("{mqtt_broker_host}", 1883, 60)

    client.on_connect = on_connect
    client.on_publish = on_publish

    tsl=Luxmeter()

    while True:
        sensor_value = tsl.getLux()
        print(sensor_value)
        if sensor_value > 500.0:
            message = json.dumps(dict(trigger="on"))
            client.publish("data",message)
        sleep(3)
if __name__ == '__main__':
    main()

I moved to a dark room and tested it. Accurate measurement is possible when the illuminance is stable and dark. It seems to be 0 if the illuminance changes too much. I slowly brought the iPhone light closer and published it twice.

$ python ./publish.py
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
781.750737506
publish: 1
1212.30526902
publish: 2
2.45526898227
1.48479374557
119.459947915
287.1274
45.49168
0
0
1.45533948132
1.41892978382
1.41892978382

Recommended Posts

Using the digital illuminance sensor TSL2561 with Raspberry Pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
I tried using the DS18B20 temperature sensor with Raspberry Pi
Try using the temperature sensor (LM75B) on the Raspberry Pi.
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Using a webcam with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Use the Grove sensor on the Raspberry Pi
Improved motion sensor made with Raspberry Pi
Use PIR motion sensor with raspberry Pi
Periodically log the value of Omron environment sensor with Raspberry Pi
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
Observe the Geminids meteor shower with Raspberry Pi 4
Sound the buzzer using python on Raspberry Pi 3!
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Display images taken with the Raspberry Pi camera module
Using Akizuki Denshi's 4WD car FT-MC-004 with Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
Indoor monitoring using Raspberry Pi
Play with the Raspberry Pi Zero WH camera module Part 1
Check! Get sensor data via Bluetooth with Raspberry Pi ~ Preparation
Get the weather using the API and let the Raspberry Pi speak!
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
USB over ethernet using Raspberry pi
I tried to automate the watering of the planter with Raspberry Pi
I learned how the infrared remote control works with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
OS setup with Raspberry Pi Imager
Try using ArUco on Raspberry Pi
Try L Chika with raspberry pi
Use "TSL2561 Illuminance Sensor Module (Manufacturer Part Number: TSL2561)" manufactured by Strawberry Linux with Rasberry pi 3 (Summary)
VPN server construction with Raspberry Pi
Using cgo with the go command
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Try moving 3 servos with Raspberry Pi
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
Use python on Raspberry Pi 3 to light the LED with switch control!
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Detect "brightness" using python on Raspberry Pi 3!
Behind the flyer: Using Docker with Python
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Try using the camera with Python's OpenCV
Make Raspberry Pi speak Japanese using OpenJtalk
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Working with OpenStack using the Python SDK