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