Skip to content

Commit 9b9afff

Browse files
committed
Catch Ctrl-C and unlock I2C bus before exiting
1 parent 26486af commit 9b9afff

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# CircuitPython demo - I2C scan
2+
#
3+
# If you run this and it seems to hang, try manually unlocking
4+
# your I2C bus from the REPL with
5+
# ` >>> import board `
6+
# ` >>> board.I2C().unlock() `
27

38
import time
4-
59
import board
610

711
i2c = board.I2C()
812

913
while not i2c.try_lock():
1014
pass
1115

16+
try:
17+
1218
while True:
1319
print("I2C addresses found:", [hex(device_address)
1420
for device_address in i2c.scan()])
1521
time.sleep(2)
22+
23+
finally: # unlock the i2c bus when ctrl-c'ing out of the loop
24+
i2c.unlock()

0 commit comments

Comments
 (0)