Simple test

Ensure your device works with this simple test.

examples/i2c_expanders_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Pat Satyshur
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6# Make sure to change the code based on the expander type you are using.
 7
 8import time
 9import board
10import digitalio
11
12# Change this if you are not using a PCAL9555
13from i2c_expanders.PCAL9555 import PCAL9555
14
15# To use default I2C bus (most boards)
16i2c = board.I2C()  # uses board.SCL and board.SDA
17
18# Change this to match the address of the device.
19PCAL9555_Address = 0x20
20
21# Initialize the device and get pins
22# Change this if you are not using a PCAL9555
23IOEXP1_dev = PCAL9555.PCAL9555(i2c, address=PCAL9555_Address)
24pin0 = IOEXP1_dev.get_pin(0)
25pin1 = IOEXP1_dev.get_pin(1)
26pin2 = IOEXP1_dev.get_pin(2)
27pin3 = IOEXP1_dev.get_pin(3)
28
29# Pin 0 is an output with an initial value of high (true)
30pin0.switch_to_output(value=True)
31
32# Pin 1 is an output with an initial value of low (false)
33pin1.switch_to_output(value=False)
34
35# Pin 2 is an input with a pull up resistor and standard polarity
36pin2.switch_to_input(pull=digitalio.Pull.UP, invert_polarity=False)
37
38# Pin 3 is an input with a pull down resistor and inverted polarity
39pin3.switch_to_input(pull=digitalio.Pull.DOWN, invert_polarity=True)
40
41# Toggle output pins. Read value from input pins.
42while True:
43    pin0.value = False
44    pin1.value = True
45    print("pin 0: False")
46    print("pin 1: True")
47    print("pin 2: ", pin2.value)
48    print("pin 3: ", pin3.value)
49    time.sleep(1)
50
51    pin0.value = True
52    pin1.value = False
53    print("pin 0: True")
54    print("pin 1: False")
55    print("pin 2: ", pin2.value)
56    print("pin 3: ", pin3.value)
57    time.sleep(1)