Skip to content

Commit f75cca3

Browse files
authored
Merge pull request #6 from SolderedElectronics/dev
Merge newly created modules to main
2 parents 20e723d + 624234e commit f75cca3

24 files changed

Lines changed: 2119 additions & 0 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# FILE: pcal6416a-digitalRead.py
2+
# AUTHOR: Fran Fodor @ Soldered
3+
# BRIEF: Read the state of a pin with an internal pull-up using the PCAL6416A IO expander.
4+
# Connect a pushbutton between pin A0 and GND — pressing it pulls the pin LOW.
5+
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
6+
# LAST UPDATED: 2026-04-23
7+
8+
import time
9+
from machine import Pin, I2C
10+
from pcal6416a import PCAL6416A, PCAL6416A_A0, INPUT_PULLUP
11+
12+
# If you aren't using the Qwiic connector, manually enter your I2C pins:
13+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
14+
# expander = PCAL6416A(i2c)
15+
16+
# Initialize expander over Qwiic
17+
expander = PCAL6416A()
18+
19+
# Set pin A0 as input with internal pull-up (pin reads HIGH when button is open)
20+
expander.pinMode(PCAL6416A_A0, INPUT_PULLUP)
21+
22+
while True:
23+
state = expander.digitalRead(PCAL6416A_A0)
24+
print("State of pin A0 is: {}".format("HIGH" if state else "LOW"))
25+
time.sleep(1)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# FILE: pcal6416a-digitalWrite.py
2+
# AUTHOR: Fran Fodor @ Soldered
3+
# BRIEF: Blink an output pin on the PCAL6416A IO expander.
4+
# Connect an LED (with series resistor) between pin A0 and GND.
5+
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
6+
# LAST UPDATED: 2026-04-23
7+
8+
import time
9+
from machine import Pin, I2C
10+
from pcal6416a import PCAL6416A, PCAL6416A_A0, OUTPUT
11+
12+
# If you aren't using the Qwiic connector, manually enter your I2C pins:
13+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
14+
# expander = PCAL6416A(i2c)
15+
16+
# Initialize expander over Qwiic
17+
expander = PCAL6416A()
18+
19+
# Set pin A0 as output
20+
expander.pinMode(PCAL6416A_A0, OUTPUT)
21+
22+
while True:
23+
expander.digitalWrite(PCAL6416A_A0, 1) # Set HIGH
24+
time.sleep(1)
25+
expander.digitalWrite(PCAL6416A_A0, 0) # Set LOW
26+
time.sleep(1)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# FILE: pcal6416a-driveStrength.py
2+
# AUTHOR: Fran Fodor @ Soldered
3+
# BRIEF: Cycle through all four drive-strength levels on pin A0 and set port B to
4+
# open-drain mode. Drive strength controls how many output transistor pairs
5+
# drive the pin — higher values allow more current to be sourced or sunk.
6+
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
7+
# LAST UPDATED: 2026-04-23
8+
9+
import time
10+
from machine import Pin, I2C
11+
from pcal6416a import PCAL6416A, PCAL6416A_A0, OUTPUT
12+
13+
# If you aren't using the Qwiic connector, manually enter your I2C pins:
14+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
15+
# expander = PCAL6416A(i2c)
16+
17+
# Initialize expander over Qwiic
18+
expander = PCAL6416A()
19+
20+
expander.pinMode(PCAL6416A_A0, OUTPUT)
21+
22+
# Set port B (port 1) to open-drain — pin floats HIGH, pulls LOW when driven low
23+
expander.openDrainPort(1, True)
24+
25+
expander.digitalWrite(PCAL6416A_A0, 1) # Hold A0 HIGH throughout
26+
27+
while True:
28+
for strength in range(4):
29+
# 0 = weakest (fewest transistor pairs), 3 = strongest (most current)
30+
expander.driveStrength(PCAL6416A_A0, strength)
31+
print("Drive strength set to {}".format(strength))
32+
time.sleep(1)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# FILE: pcal6416a-interrupts.py
2+
# AUTHOR: Fran Fodor @ Soldered
3+
# BRIEF: Detect pin-change interrupts from the PCAL6416A using the INT output.
4+
# Connect pushbuttons between pins A0/A1 and GND. Connect the INT pin of
5+
# the expander to pin D2 on your board.
6+
# WORKS WITH: PCAL6416A IO Expander breakout: www.solde.red/333351
7+
# LAST UPDATED: 2026-04-23
8+
9+
from machine import Pin, I2C
10+
from pcal6416a import PCAL6416A, PCAL6416A_A0, PCAL6416A_A1, INPUT_PULLUP
11+
12+
# If you aren't using the Qwiic connector, manually enter your I2C pins:
13+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
14+
# expander = PCAL6416A(i2c)
15+
16+
# Initialize expander over Qwiic
17+
expander = PCAL6416A()
18+
19+
expander.pinMode(PCAL6416A_A0, INPUT_PULLUP)
20+
expander.pinMode(PCAL6416A_A1, INPUT_PULLUP)
21+
22+
# Enable change-detection interrupt on both pins
23+
expander.setInterrupt(PCAL6416A_A0, True)
24+
expander.setInterrupt(PCAL6416A_A1, True)
25+
26+
int_flag = False
27+
28+
29+
def isr(pin):
30+
global int_flag
31+
int_flag = True
32+
33+
34+
# D2 on most boards — adjust if needed
35+
int_pin = Pin(2, Pin.IN, Pin.PULL_UP)
36+
int_pin.irq(trigger=Pin.IRQ_FALLING, handler=isr)
37+
38+
print("Waiting for interrupts on A0 and A1...")
39+
40+
while True:
41+
if int_flag:
42+
int_flag = False
43+
int_reg = expander.getInterrupts() # Reading clears the status register
44+
45+
if int_reg & (1 << PCAL6416A_A0):
46+
state = expander.digitalRead(PCAL6416A_A0)
47+
print("Interrupt on A0 — pin is now {}".format("HIGH" if state else "LOW"))
48+
49+
if int_reg & (1 << PCAL6416A_A1):
50+
state = expander.digitalRead(PCAL6416A_A1)
51+
print("Interrupt on A1 — pin is now {}".format("HIGH" if state else "LOW"))

0 commit comments

Comments
 (0)