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