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 8 import time 9 10 import board 11 import busio 12 import digitalio 13 14 from adafruit_mcp230xx.mcp23008 import MCP23008 15 16 # from adafruit_mcp230xx.mcp23017 import MCP23017 17 18 19 # Initialize the I2C bus: 20 i2c = 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: 24 mcp = 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). 37 pin0 = mcp.get_pin(0) 38 pin1 = mcp.get_pin(1) 39 40 # Setup pin0 as an output that's at a high logic level. 41 pin0.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. 45 pin1.direction = digitalio.Direction.INPUT 46 pin1.pull = digitalio.Pull.UP 47 48 # Now loop blinking the pin 0 output and reading the state of pin 1 input. 49 while 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))