1
- # FILE: MCP23017-interrupts.py
1
+ # FILE: MCP23017-interrupts.py
2
2
# AUTHOR: Josip Šimun Kuči @ Soldered
3
3
# BRIEF: An example showing how to configure interrupts on both INTA and INTB
4
4
# ports. For this example you have to make the following connections:
5
5
# INTA/INTB(IO Expander) - GPIO32 (Dasduino board)
6
6
# The IO expander generates the interrupt and the board detects it
7
7
# WORKS WITH: IO expander MCP23017 breakout: www.solde.red/333007
8
- # LAST UPDATED: 2025-07-17
8
+ # LAST UPDATED: 2025-07-17
9
9
10
10
from machine import I2C , Pin
11
11
from mcp23017 import *
21
21
# Configure which pin will trigger interrupts (using pin 8 in this example)
22
22
INTERRUPT_PIN = A7
23
23
BUTTON_PIN = A7 # Using pin 8 as our interrupt source
24
- interruptHappened = 0
24
+ interruptHappened = 0
25
25
26
26
# Configure the button pin as input with pull-up and interrupt
27
- mcp .pin (BUTTON_PIN ,
28
- mode = 1 , # input
29
- pullup = 1 , # enable pull-up resistor
30
- interrupt_enable = 1 , # enable interrupt
31
- interrupt_compare_default = 1 , # compare against default value
32
- default_value = 1 ) # interrupt when pin goes low (button press)
27
+ mcp .pin (
28
+ BUTTON_PIN ,
29
+ mode = 1 , # input
30
+ pullup = 1 , # enable pull-up resistor
31
+ interrupt_enable = 1 , # enable interrupt
32
+ interrupt_compare_default = 1 , # compare against default value
33
+ default_value = 1 ,
34
+ ) # interrupt when pin goes low (button press)
33
35
34
36
# Configure the MCP23017 interrupt settings
35
37
mcp .config (
36
38
interrupt_polarity = 1 , # active-high interrupt (set to 0 for active-low)
37
- interrupt_mirror = 1 , # mirror INTA/INTB (both pins show same interrupt)
38
- interrupt_open_drain = 0 # push-pull output (set to 1 for open-drain)
39
+ interrupt_mirror = 1 , # mirror INTA/INTB (both pins show same interrupt)
40
+ interrupt_open_drain = 0 , # push-pull output (set to 1 for open-drain)
39
41
)
40
42
41
43
# Set up the physical interrupt pin on the microcontroller
42
44
# Connect this to either INTA or INTB pin of the MCP23017
43
45
mcp_int_pin = Pin (32 , Pin .IN , Pin .PULL_UP ) # Using GPIO23, adjust for your board
44
46
47
+
45
48
# Interrupt Service Routine (ISR)
46
49
def mcp_interrupt_handler (pin ):
47
50
# Disable interrupts temporarily while we handle this one
48
51
mcp .pin (BUTTON_PIN , interrupt_enable = 0 )
49
52
# Trigger variable must use keyword global to not be viewed as local
50
53
global interruptHappened
51
- interruptHappened = 1
52
-
53
-
54
+ interruptHappened = 1
55
+
54
56
55
57
# Configure the interrupt handler
56
58
mcp_int_pin .irq (trigger = Pin .IRQ_FALLING | Pin .IRQ_RISING , handler = mcp_interrupt_handler )
@@ -61,13 +63,13 @@ def mcp_interrupt_handler(pin):
61
63
# Main loop doesn't need to do anything until an interrupt happens
62
64
while True :
63
65
# Check if interrupt trigger was set to 1
64
- if interruptHappened == 1 :
66
+ if interruptHappened == 1 :
65
67
# Inform user
66
68
print ("Interrupt detected!" )
67
69
# Small delay for debouncing
68
70
time .sleep (0.5 )
69
71
# Reset trigger
70
- interruptHappened = 0
72
+ interruptHappened = 0
71
73
# Re-enable the interrupt
72
74
mcp .pin (BUTTON_PIN , interrupt_enable = 1 )
73
- time .sleep (0.01 )
75
+ time .sleep (0.01 )
0 commit comments