Simple test
Ensure your device works with this simple test.
examples/tsl2591_simpletest.py
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
7
8import board
9
10import adafruit_tsl2591
11
12# Create sensor object, communicating over the board's default I2C bus
13i2c = board.I2C() # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
15
16# Initialize the sensor.
17sensor = adafruit_tsl2591.TSL2591(i2c)
18
19# You can optionally change the gain and integration time:
20# sensor.gain = adafruit_tsl2591.GAIN_LOW (1x gain)
21# sensor.gain = adafruit_tsl2591.GAIN_MED (25x gain, the default)
22# sensor.gain = adafruit_tsl2591.GAIN_HIGH (428x gain)
23# sensor.gain = adafruit_tsl2591.GAIN_MAX (9876x gain)
24# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_100MS (100ms, default)
25# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_200MS (200ms)
26# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_300MS (300ms)
27# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_400MS (400ms)
28# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_500MS (500ms)
29# sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_600MS (600ms)
30
31# Read the total lux, IR, and visible light levels and print it every second.
32while True:
33 # Read and calculate the light level in lux.
34 lux = sensor.lux
35 print(f"Total light: {lux}lux")
36 # You can also read the raw infrared and visible light levels.
37 # These are unsigned, the higher the number the more light of that type.
38 # There are no units like lux.
39 # Infrared levels range from 0-65535 (16-bit)
40 infrared = sensor.infrared
41 print(f"Infrared light: {infrared}")
42 # Visible-only levels range from 0-2147483647 (32-bit)
43 visible = sensor.visible
44 print(f"Visible light: {visible}")
45 # Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
46 full_spectrum = sensor.full_spectrum
47 print(f"Full spectrum (IR + visible) light: {full_spectrum}")
48 time.sleep(1.0)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/tsl2591_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6import time
7
8import board
9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_tsl2591
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20sensor = adafruit_tsl2591.TSL2591(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24light_output_label = Label(FONT, text="", scale=2)
25infra_output_label = Label(FONT, text="", scale=2)
26
27# place the label(s) in the middle of the screen with anchored positioning
28light_output_label.anchor_point = (0, 0)
29light_output_label.anchored_position = (
30 4,
31 board.DISPLAY.height // 2 - 60,
32)
33infra_output_label.anchor_point = (0, 0)
34infra_output_label.anchored_position = (
35 4,
36 board.DISPLAY.height // 2 - 30,
37)
38
39# add the label(s) to the main_group
40main_group.append(light_output_label)
41main_group.append(infra_output_label)
42
43# set the main_group as the root_group of the built-in DISPLAY
44board.DISPLAY.root_group = main_group
45
46# begin main loop
47while True:
48 # update the text of the label(s) to show the sensor readings
49 # Infrared levels range from 0-65535 (16-bit)
50 light_output_label.text = f"Total light:{sensor.lux:.1f}lux"
51 infra_output_label.text = f"Infrared light:{sensor.infrared}"
52 # wait for a bit
53 time.sleep(0.5)