Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# FILE: pcal6416a-digitalRead.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Read the state of a pin with an internal pull-up using the PCAL6416A IO expander.
# Connect a pushbutton between pin A0 and GND — pressing it pulls the pin LOW.
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
# LAST UPDATED: 2026-04-23

import time
from machine import Pin, I2C
from pcal6416a import PCAL6416A, PCAL6416A_A0, INPUT_PULLUP

# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# expander = PCAL6416A(i2c)

# Initialize expander over Qwiic
expander = PCAL6416A()

# Set pin A0 as input with internal pull-up (pin reads HIGH when button is open)
expander.pinMode(PCAL6416A_A0, INPUT_PULLUP)

while True:
state = expander.digitalRead(PCAL6416A_A0)
print("State of pin A0 is: {}".format("HIGH" if state else "LOW"))
time.sleep(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# FILE: pcal6416a-digitalWrite.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Blink an output pin on the PCAL6416A IO expander.
# Connect an LED (with series resistor) between pin A0 and GND.
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
# LAST UPDATED: 2026-04-23

import time
from machine import Pin, I2C
from pcal6416a import PCAL6416A, PCAL6416A_A0, OUTPUT

# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# expander = PCAL6416A(i2c)

# Initialize expander over Qwiic
expander = PCAL6416A()

# Set pin A0 as output
expander.pinMode(PCAL6416A_A0, OUTPUT)

while True:
expander.digitalWrite(PCAL6416A_A0, 1) # Set HIGH
time.sleep(1)
expander.digitalWrite(PCAL6416A_A0, 0) # Set LOW
time.sleep(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# FILE: pcal6416a-driveStrength.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Cycle through all four drive-strength levels on pin A0 and set port B to
# open-drain mode. Drive strength controls how many output transistor pairs
# drive the pin — higher values allow more current to be sourced or sunk.
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
# LAST UPDATED: 2026-04-23

import time
from machine import Pin, I2C
from pcal6416a import PCAL6416A, PCAL6416A_A0, OUTPUT

# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# expander = PCAL6416A(i2c)

# Initialize expander over Qwiic
expander = PCAL6416A()

expander.pinMode(PCAL6416A_A0, OUTPUT)

# Set port B (port 1) to open-drain — pin floats HIGH, pulls LOW when driven low
expander.openDrainPort(1, True)

expander.digitalWrite(PCAL6416A_A0, 1) # Hold A0 HIGH throughout

while True:
for strength in range(4):
# 0 = weakest (fewest transistor pairs), 3 = strongest (most current)
expander.driveStrength(PCAL6416A_A0, strength)
print("Drive strength set to {}".format(strength))
time.sleep(1)
51 changes: 51 additions & 0 deletions Communication/PCAL6416A/PCAL6416A/Examples/pcal6416a-interrupts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# FILE: pcal6416a-interrupts.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Detect pin-change interrupts from the PCAL6416A using the INT output.
# Connect pushbuttons between pins A0/A1 and GND. Connect the INT pin of
# the expander to pin D2 on your board.
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
# LAST UPDATED: 2026-04-23

from machine import Pin, I2C
from pcal6416a import PCAL6416A, PCAL6416A_A0, PCAL6416A_A1, INPUT_PULLUP

# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# expander = PCAL6416A(i2c)

# Initialize expander over Qwiic
expander = PCAL6416A()

expander.pinMode(PCAL6416A_A0, INPUT_PULLUP)
expander.pinMode(PCAL6416A_A1, INPUT_PULLUP)

# Enable change-detection interrupt on both pins
expander.setInterrupt(PCAL6416A_A0, True)
expander.setInterrupt(PCAL6416A_A1, True)

int_flag = False


def isr(pin):
global int_flag
int_flag = True


# D2 on most boards — adjust if needed
int_pin = Pin(2, Pin.IN, Pin.PULL_UP)
int_pin.irq(trigger=Pin.IRQ_FALLING, handler=isr)

print("Waiting for interrupts on A0 and A1...")

while True:
if int_flag:
int_flag = False
int_reg = expander.getInterrupts() # Reading clears the status register

if int_reg & (1 << PCAL6416A_A0):
state = expander.digitalRead(PCAL6416A_A0)
print("Interrupt on A0 — pin is now {}".format("HIGH" if state else "LOW"))

if int_reg & (1 << PCAL6416A_A1):
state = expander.digitalRead(PCAL6416A_A1)
print("Interrupt on A1 — pin is now {}".format("HIGH" if state else "LOW"))
Loading