Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of the TSL2591 sensor. Will print the detected light value
5# every second.
6import time
7import board
8import adafruit_tsl2591
9
10# Create sensor object, communicating over the board's default I2C bus
11i2c = board.I2C() # uses board.SCL and board.SDA
12
13# Initialize the sensor.
14sensor = adafruit_tsl2591.TSL2591(i2c)
15
16# You can optionally change the gain and integration time:
17# sensor.gain = adafruit_tsl2591.GAIN_LOW (1x gain)
18# sensor.gain = adafruit_tsl2591.GAIN_MED (25x gain, the default)
19# sensor.gain = adafruit_tsl2591.GAIN_HIGH (428x gain)
20# sensor.gain = adafruit_tsl2591.GAIN_MAX (9876x gain)
21# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_100MS (100ms, default)
22# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_200MS (200ms)
23# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_300MS (300ms)
24# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_400MS (400ms)
25# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_500MS (500ms)
26# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_600MS (600ms)
27
28# Read the total lux, IR, and visible light levels and print it every second.
29while True:
30 # Read and calculate the light level in lux.
31 lux = sensor.lux
32 print("Total light: {0}lux".format(lux))
33 # You can also read the raw infrared and visible light levels.
34 # These are unsigned, the higher the number the more light of that type.
35 # There are no units like lux.
36 # Infrared levels range from 0-65535 (16-bit)
37 infrared = sensor.infrared
38 print("Infrared light: {0}".format(infrared))
39 # Visible-only levels range from 0-2147483647 (32-bit)
40 visible = sensor.visible
41 print("Visible light: {0}".format(visible))
42 # Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
43 full_spectrum = sensor.full_spectrum
44 print("Full spectrum (IR + visible) light: {0}".format(full_spectrum))
45 time.sleep(1.0)