Simple test

Ensure your device works with this simple test.

examples/si1145_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2022 Carter Nelson for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import time
 7
 8import board
 9
10import adafruit_si1145
11
12# setup I2C bus using board default
13# change as needed for specific boards
14i2c = board.I2C()  # uses board.SCL and board.SDA
15# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
16
17# setup sensor
18si1145 = adafruit_si1145.SI1145(i2c)
19
20# loop forever printing values
21while True:
22    vis, ir = si1145.als
23    print(f"Visible = {vis}, Infrared = {ir}")
24    time.sleep(1)

Test Gains

Test the different gain settings of the sensor.

examples/si1145_test_gains.py
 1# SPDX-FileCopyrightText: Copyright (c) 2024 Aaron W Morris (aaronwmorris)
 2#
 3# SPDX-License-Identifier: Unlicense
 4
 5import time
 6
 7import board
 8
 9import adafruit_si1145
10
11# setup I2C bus using board default
12# change as needed for specific boards
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# setup sensor
17si1145 = adafruit_si1145.SI1145(i2c)
18
19
20print(f"Default Vis Gain: {si1145.vis_gain}")
21print(f"Default IR Gain: {si1145.ir_gain}")
22print(f"Default Vis High range: {str(si1145.als_vis_range_high)}")
23print(f"Default IR High range: {str(si1145.als_ir_range_high)}")
24print()
25
26
27### Low range
28si1145.als_range_high = False  # both settings
29# si1145.als_vis_range_high = False
30# si1145.als_ir_range_high = False
31
32# Change above to True for High Signal Range mode.
33# High Signal Range mode divides gain by 14.5
34# Useful for direct sunlight operation
35
36time.sleep(0.5)
37
38# test reading attributes
39print(f"Vis High range: {str(si1145.als_vis_range_high)}")
40print(f"IR High range: {str(si1145.als_ir_range_high)}")
41print()
42
43
44gain_list = (
45    adafruit_si1145.GAIN_ADC_CLOCK_DIV_1,  # (1x gain, default)
46    adafruit_si1145.GAIN_ADC_CLOCK_DIV_2,  # (2x gain)
47    adafruit_si1145.GAIN_ADC_CLOCK_DIV_4,  # (4x gain)
48    adafruit_si1145.GAIN_ADC_CLOCK_DIV_8,  # (8x gain)
49    adafruit_si1145.GAIN_ADC_CLOCK_DIV_16,  # (16x gain)
50    adafruit_si1145.GAIN_ADC_CLOCK_DIV_32,  # (32x gain)
51    adafruit_si1145.GAIN_ADC_CLOCK_DIV_64,  # (64x gain)
52    adafruit_si1145.GAIN_ADC_CLOCK_DIV_128,  # (128x gain)
53)
54
55
56for gain in gain_list:
57    si1145.gain = gain  # both gains
58    # si1145.vis_gain = gain
59    # si1145.ir_gain = gain
60
61    # test reading attributes
62    print(f"Vis Gain: {si1145.vis_gain}")
63    print(f"IR Gain: {si1145.ir_gain}")
64
65    vis, ir = si1145.als
66    uv_index = si1145.uv_index
67    print(f"Visible = {vis}, Infrared = {ir}, UV Index = {uv_index}")
68    print()
69    time.sleep(0.5)
70
71
72### High range
73# In high range mode, sensor gain should be ~14.5
74si1145.als_range_high = True  # both settings
75# si1145.als_vis_range_high = True
76# si1145.als_ir_range_high = True
77time.sleep(0.5)
78
79
80# test reading attributes
81print(f"Vis High range: {str(si1145.als_vis_range_high)}")
82print(f"IR High range: {str(si1145.als_ir_range_high)}")
83print()
84
85
86vis, ir = si1145.als
87uv_index = si1145.uv_index
88print(f"Visible = {vis}, Infrared = {ir}, UV Index = {uv_index}")

DisplayIO Simpletest

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

examples/si1145_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
 6# Simple demo of the SI1145 UV sensor using a built-in display.
 7import time
 8
 9import board
10from adafruit_display_text.bitmap_label import Label
11from displayio import Group
12from terminalio import FONT
13
14import adafruit_si1145
15
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
20si1145 = adafruit_si1145.SI1145(i2c)
21
22# Create a Label to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (4, board.DISPLAY.height // 2)
29
30# add the label to the main_group
31main_group.append(display_output_label)
32
33# set the main_group as the root_group of the built-in DISPLAY
34board.DISPLAY.root_group = main_group
35
36# begin main loop
37while True:
38    # Update the label.text property to change the text on the display
39    vis, ir = si1145.als
40    display_output_label.text = f"Visible = {vis}, Infrared = {ir}"
41    # wait for a bit
42    time.sleep(1.0)