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))
MCP23Sxx Simple test¶
Simple demo of reading and writing the digital I/O of the MCP2300xx as if they were native CircuitPython digital inputs/outputs.
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))
MCP230xx Event detect interrupt¶
Example showing the event detect interrupt feature
examples/mcp230xx_event_detect_interrupt.py¶
1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5from time import sleep
6
7import board
8import busio
9from digitalio import Direction, Pull
10from RPi import GPIO
11from adafruit_mcp230xx.mcp23017 import MCP23017
12
13# Initialize the I2C bus:
14i2c = busio.I2C(board.SCL, board.SDA)
15
16# Initialize the MCP23017 chip on the bonnet
17mcp = MCP23017(i2c)
18
19# Optionally change the address of the device if you set any of the A0, A1, A2
20# pins. Specify the new address with a keyword parameter:
21# mcp = MCP23017(i2c, address=0x21) # MCP23017 w/ A0 set
22
23# Make a list of all the pins (a.k.a 0-16)
24pins = []
25for pin in range(0, 16):
26 pins.append(mcp.get_pin(pin))
27
28# Set all the pins to input
29for pin in pins:
30 pin.direction = Direction.INPUT
31 pin.pull = Pull.UP
32
33# Set up to check all the port B pins (pins 8-15) w/interrupts!
34mcp.interrupt_enable = 0xFFFF # Enable Interrupts in all pins
35# If intcon is set to 0's we will get interrupts on
36# both button presses and button releases
37mcp.interrupt_configuration = 0x0000 # interrupt on any change
38mcp.io_control = 0x44 # Interrupt as open drain and mirrored
39mcp.clear_ints() # Interrupts need to be cleared initially
40
41# Or, we can ask to be notified CONTINUOUSLY if a pin goes LOW (button press)
42# we won't get an IRQ pulse when the pin is HIGH!
43# mcp.interrupt_configuration = 0xFFFF # notify pin value
44# mcp.default_value = 0xFFFF # default value is 'high' so notify whenever 'low'
45
46
47def print_interrupt(port):
48 """Callback function to be called when an Interrupt occurs."""
49 for pin_flag in mcp.int_flag:
50 print("Interrupt connected to Pin: {}".format(port))
51 print("Pin number: {} changed to: {}".format(pin_flag, pins[pin_flag].value))
52 mcp.clear_ints()
53
54
55# connect either interrupt pin to the Raspberry pi's pin 17.
56# They were previously configured as mirrored.
57GPIO.setmode(GPIO.BCM)
58interrupt = 17
59GPIO.setup(interrupt, GPIO.IN, GPIO.PUD_UP) # Set up Pi's pin as input, pull up
60
61# The add_event_detect fuction will call our print_interrupt callback function
62# every time an interrupt gets triggered.
63GPIO.add_event_detect(interrupt, GPIO.FALLING, callback=print_interrupt, bouncetime=10)
64
65# The following lines are so the program runs for at least 60 seconds,
66# during that time it will detect any pin interrupt and print out the pin number
67# that changed state and its current state.
68# The program can be terminated using Ctrl+C. It doesn't matter how it
69# terminates it will always run GPIO.cleanup().
70try:
71 print("When button is pressed you'll see a message")
72 sleep(60) # You could run your main while loop here.
73 print("Time's up. Finished!")
74finally:
75 GPIO.cleanup()
MCP23Sxx Event detect interrupt¶
Example showing the event detect interrupt feature
examples/mcp23Sxx_event_detect_interrupt.py¶
1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2# SPDX-FileCopyrightText: 2021 Red_M
3#
4# SPDX-License-Identifier: MIT
5
6from time import sleep
7
8import board
9import busio
10from digitalio import DigitalInOut, Direction, Pull
11from RPi import GPIO
12from adafruit_mcp230xx.mcp23s17 import MCP23S17
13
14# Initialize the SPI bus:
15spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)
16cs = DigitalInOut(board.CS0)
17
18# Initialize the MCP23S17 chip on the bonnet
19mcp = MCP23S17(spi, cs)
20
21# Optionally change the address of the device if you set any of the A0, A1, A2
22# pins. Specify the new address with a keyword parameter:
23# mcp = MCP23S17(spi, cs, address=0x21) # MCP23S17 w/ A0 set
24
25# Make a list of all the pins (a.k.a 0-16)
26pins = []
27for pin in range(0, 16):
28 pins.append(mcp.get_pin(pin))
29
30# Set all the pins to input
31for pin in pins:
32 pin.direction = Direction.INPUT
33 pin.pull = Pull.UP
34
35# Set up to check all the port B pins (pins 8-15) w/interrupts!
36mcp.interrupt_enable = 0xFFFF # Enable Interrupts in all pins
37# If intcon is set to 0's we will get interrupts on
38# both button presses and button releases
39mcp.interrupt_configuration = 0x0000 # interrupt on any change
40mcp.io_control = 0x44 # Interrupt as open drain and mirrored
41mcp.clear_ints() # Interrupts need to be cleared initially
42
43# Or, we can ask to be notified CONTINUOUSLY if a pin goes LOW (button press)
44# we won't get an IRQ pulse when the pin is HIGH!
45# mcp.interrupt_configuration = 0xFFFF # notify pin value
46# mcp.default_value = 0xFFFF # default value is 'high' so notify whenever 'low'
47
48
49def print_interrupt(port):
50 """Callback function to be called when an Interrupt occurs."""
51 for pin_flag in mcp.int_flag:
52 print("Interrupt connected to Pin: {}".format(port))
53 print("Pin number: {} changed to: {}".format(pin_flag, pins[pin_flag].value))
54 mcp.clear_ints()
55
56
57# connect either interrupt pin to the Raspberry pi's pin 17.
58# They were previously configured as mirrored.
59GPIO.setmode(GPIO.BCM)
60interrupt = 17
61GPIO.setup(interrupt, GPIO.IN, GPIO.PUD_UP) # Set up Pi's pin as input, pull up
62
63# The add_event_detect fuction will call our print_interrupt callback function
64# every time an interrupt gets triggered.
65GPIO.add_event_detect(interrupt, GPIO.FALLING, callback=print_interrupt, bouncetime=10)
66
67# The following lines are so the program runs for at least 60 seconds,
68# during that time it will detect any pin interrupt and print out the pin number
69# that changed state and its current state.
70# The program can be terminated using Ctrl+C. It doesn't matter how it
71# terminates it will always run GPIO.cleanup().
72try:
73 print("When button is pressed you'll see a message")
74 sleep(60) # You could run your main while loop here.
75 print("Time's up. Finished!")
76finally:
77 GPIO.cleanup()