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# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
13
14# Initialize the sensor.
15sensor = adafruit_tsl2591.TSL2591(i2c)
16
17# You can optionally change the gain and integration time:
18# sensor.gain = adafruit_tsl2591.GAIN_LOW (1x gain)
19# sensor.gain = adafruit_tsl2591.GAIN_MED (25x gain, the default)
20# sensor.gain = adafruit_tsl2591.GAIN_HIGH (428x gain)
21# sensor.gain = adafruit_tsl2591.GAIN_MAX (9876x gain)
22# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_100MS (100ms, default)
23# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_200MS (200ms)
24# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_300MS (300ms)
25# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_400MS (400ms)
26# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_500MS (500ms)
27# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_600MS (600ms)
28
29# Read the total lux, IR, and visible light levels and print it every second.
30while True:
31 # Read and calculate the light level in lux.
32 lux = sensor.lux
33 print("Total light: {0}lux".format(lux))
34 # You can also read the raw infrared and visible light levels.
35 # These are unsigned, the higher the number the more light of that type.
36 # There are no units like lux.
37 # Infrared levels range from 0-65535 (16-bit)
38 infrared = sensor.infrared
39 print("Infrared light: {0}".format(infrared))
40 # Visible-only levels range from 0-2147483647 (32-bit)
41 visible = sensor.visible
42 print("Visible light: {0}".format(visible))
43 # Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
44 full_spectrum = sensor.full_spectrum
45 print("Full spectrum (IR + visible) light: {0}".format(full_spectrum))
46 time.sleep(1.0)