-
Notifications
You must be signed in to change notification settings - Fork 1
/
blink_with_pcf8574.py
36 lines (30 loc) · 991 Bytes
/
blink_with_pcf8574.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from machine import Pin, I2C
import utime
# Parameterize I2C settings
i2c_bus = 0
scl_pin = 17
sda_pin = 16
freq = 100000
address = 0x20
toggle_delay = 1 # delay in seconds
# Initialize I2C
i2c = I2C(i2c_bus, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=freq)
def toggle_pins(address, delay=1):
try:
for i in range(8):
pin_state = 1 << i
i2c.writeto(address, bytearray([pin_state]))
utime.sleep(delay)
# Verify the written state
read_back = i2c.readfrom(address, 1)
print("Written State:", bin(pin_state), "Read State:", bin(read_back[0]))
# Reset pins after each toggle
i2c.writeto(address, bytearray([0x00]))
except OSError as e:
print("Error accessing I2C device:", e)
# Run toggle function in a controlled manner
try:
while True:
toggle_pins(address, toggle_delay)
except KeyboardInterrupt:
print("Program stopped by user")