Simple test

Ensure your device works with this simple test.

examples/mcp230xx_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5# Simple demo of reading and writing the digital I/O of the MCP2300xx as if
 6# they were native CircuitPython digital inputs/outputs.
 7# Author: Tony DiCola
 8import time
 9
10import board
11import busio
12import digitalio
13
14from adafruit_mcp230xx.mcp23008 import MCP23008
15
16# from adafruit_mcp230xx.mcp23017 import MCP23017
17
18
19# Initialize the I2C bus:
20i2c = busio.I2C(board.SCL, board.SDA)
21
22# Create an instance of either the MCP23008 or MCP23017 class depending on
23# which chip you're using:
24mcp = MCP23008(i2c)  # MCP23008
25# mcp = MCP23017(i2c)  # MCP23017
26
27# Optionally change the address of the device if you set any of the A0, A1, A2
28# pins.  Specify the new address with a keyword parameter:
29# mcp = MCP23017(i2c, address=0x21)  # MCP23017 w/ A0 set
30
31# Now call the get_pin function to get an instance of a pin on the chip.
32# This instance will act just like a digitalio.DigitalInOut class instance
33# and has all the same properties and methods (except you can't set pull-down
34# resistors, only pull-up!).  For the MCP23008 you specify a pin number from 0
35# to 7 for the GP0...GP7 pins.  For the MCP23017 you specify a pin number from
36# 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4).
37pin0 = mcp.get_pin(0)
38pin1 = mcp.get_pin(1)
39
40# Setup pin0 as an output that's at a high logic level.
41pin0.switch_to_output(value=True)
42
43# Setup pin1 as an input with a pull-up resistor enabled.  Notice you can also
44# use properties to change this state.
45pin1.direction = digitalio.Direction.INPUT
46pin1.pull = digitalio.Pull.UP
47
48# Now loop blinking the pin 0 output and reading the state of pin 1 input.
49while True:
50    # Blink pin 0 on and then off.
51    pin0.value = True
52    time.sleep(0.5)
53    pin0.value = False
54    time.sleep(0.5)
55    # Read pin 1 and print its state.
56    print("Pin 1 is at a high level: {0}".format(pin1.value))
examples/mcp23Sxx_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
 2# SPDX-FileCopyrightText: 2021 Red_M
 3#
 4# SPDX-License-Identifier: MIT
 5
 6# Simple demo of reading and writing the digital I/O of the MCP2300xx as if
 7# they were native CircuitPython digital inputs/outputs.
 8# Author: Tony DiCola
 9import time
10
11import board
12import busio
13import digitalio
14
15from adafruit_mcp230xx.mcp23s08 import MCP23S08
16
17# from adafruit_mcp230xx.mcp23s17 import MCP23S17
18
19
20# Initialize the SPI bus with a chip select pin:
21spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
22cs = digitalio.DigitalInOut(board.A1)
23
24# Create an instance of either the MCP23S08 or MCP23S17 class depending on
25# which chip you're using:
26mcp = MCP23S08(spi, cs)  # MCP23S08
27# mcp = MCP23S17(spi, cs)  # MCP23S17
28
29# Optionally change the address of the device if you set any of the A0, A1, A2
30# pins.  Specify the new address with a keyword parameter:
31# mcp = MCP23S17(spi, cs, address=0x21)  # MCP23S17 w/ A0 set
32
33# Now call the get_pin function to get an instance of a pin on the chip.
34# This instance will act just like a digitalio.DigitalInOut class instance
35# and has all the same properties and methods (except you can't set pull-down
36# resistors, only pull-up!).  For the MCP23S08 you specify a pin number from 0
37# to 7 for the GP0...GP7 pins.  For the MCP23S17 you specify a pin number from
38# 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4).
39pin0 = mcp.get_pin(0)
40pin1 = mcp.get_pin(1)
41
42# Setup pin0 as an output that's at a high logic level.
43pin0.switch_to_output(value=True)
44
45# Setup pin1 as an input with a pull-up resistor enabled.  Notice you can also
46# use properties to change this state.
47pin1.direction = digitalio.Direction.INPUT
48pin1.pull = digitalio.Pull.UP
49
50# Now loop blinking the pin 0 output and reading the state of pin 1 input.
51while True:
52    # Blink pin 0 on and then off.
53    pin0.value = True
54    time.sleep(0.5)
55    pin0.value = False
56    time.sleep(0.5)
57    # Read pin 1 and print its state.
58    print("Pin 1 is at a high level: {0}".format(pin1.value))