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