Simple test

Ensure your device works with this simple test.

examples/ltr390_simpletest.py
 1# SPDX-FileCopyrightText: 2021 by Bryan Siepert, written for Adafruit Industries
 2#
 3# SPDX-License-Identifier: Unlicense
 4import time
 5
 6import board
 7
 8import adafruit_ltr390
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12ltr = adafruit_ltr390.LTR390(i2c)
13
14while True:
15    print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light)
16    print("UVI:", ltr.uvi, "\t\tLux:", ltr.lux)
17    time.sleep(1.0)

Configuration example

Adjust the configuration options for the sensor

examples/ltr390_configuration_example.py
 1# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries
 2#
 3# SPDX-License-Identifier: Unlicense
 4# pylint:disable=unused-import,no-member
 5
 6import time
 7
 8import board
 9
10from adafruit_ltr390 import LTR390, Gain, MeasurementDelay, Resolution
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
14ltr = LTR390(i2c)
15
16# ltr.resolution = Resolution.RESOLUTION_16BIT
17print("Measurement resolution is", Resolution.string[ltr.resolution])
18
19# ltr.gain = Gain.GAIN_1X
20print("Measurement gain is", Gain.string[ltr.gain])
21
22# ltr.measurement_delay = MeasurementDelay.DELAY_100MS
23print("Measurement delay is", MeasurementDelay.string[ltr.measurement_delay])
24print("")
25while True:
26    print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light)
27
28    # for shorter measurement delays you may need to make this sleep shorter to see a change
29    time.sleep(1.0)

Measurement threshold example

Be alerted when the measured value passes a high or low threshold

examples/ltr390_alert_test.py
 1# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries
 2#
 3# SPDX-License-Identifier: Unlicense
 4# pylint:disable=unused-import
 5import time
 6
 7import board
 8
 9from adafruit_ltr390 import ALS, LTR390, UV
10
11THRESHOLD_VALUE = 100
12
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
15ltr = LTR390(i2c)
16
17ltr.high_threshold = THRESHOLD_VALUE
18ltr.enable_alerts(True, UV, 1)
19
20while True:
21    if ltr.threshold_passed:
22        print("UV:", ltr.uvs)
23        print("threshold", THRESHOLD_VALUE, "passed!")
24        print("")
25    else:
26        print("threshold not passed yet")
27
28    time.sleep(1)

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/ltr390_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2025 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_ltr390
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
20ltr = adafruit_ltr390.LTR390(i2c)
21
22
23# Create Label(s) to show the readings. If you have a very small
24# display you may need to change to scale=1.
25display_output_light = Label(FONT, text="", scale=2)
26display_output_lux = Label(FONT, text="", scale=2)
27
28# place the label(s) in the middle of the screen with anchored positioning
29display_output_light.anchor_point = (0, 0)
30display_output_light.anchored_position = (
31    4,
32    board.DISPLAY.height // 2 - 60,
33)
34display_output_lux.anchor_point = (0, 0)
35display_output_lux.anchored_position = (
36    4,
37    board.DISPLAY.height // 2 - 40,
38)
39
40# add the label(s) to the main_group
41main_group.append(display_output_light)
42main_group.append(display_output_lux)
43
44# set the main_group as the root_group of the built-in DISPLAY
45board.DISPLAY.root_group = main_group
46
47# begin main loop
48while True:
49    # update the text of the label(s) to show the sensor readings
50    display_output_light.text = f"Ambient Light: {ltr.light:.2f}"
51    display_output_lux.text = f"Lux: {ltr.lux:.2f}"
52    # wait for a bit
53    time.sleep(0.5)