Skip to content

Commit d670ab4

Browse files
authored
Merge pull request #1925 from caternuson/cp_i2c_scan_update
Update CP I2C Scan Example
2 parents d94fb1e + ccef988 commit d670ab4

File tree

2 files changed

+21
-4
lines changed
  • CircuitPython_Essentials/CircuitPython_I2C_Scan
  • CircuitPython_Templates/i2c_scan

2 files changed

+21
-4
lines changed

CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""CircuitPython Essentials I2C Scan example"""
1+
"""CircuitPython I2C Device Address Scan"""
22
# If you run this and it seems to hang, try manually unlocking
33
# your I2C bus from the REPL with
44
# >>> import board
@@ -7,15 +7,23 @@
77
import time
88
import board
99

10+
# To use default I2C bus (most boards)
1011
i2c = board.I2C()
1112

13+
# To create I2C bus on specific pins
14+
# import busio
15+
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
16+
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
17+
1218
while not i2c.try_lock():
1319
pass
1420

1521
try:
1622
while True:
17-
print("I2C addresses found:", [hex(device_address)
18-
for device_address in i2c.scan()])
23+
print(
24+
"I2C addresses found:",
25+
[hex(device_address) for device_address in i2c.scan()],
26+
)
1927
time.sleep(2)
2028

2129
finally: # unlock the i2c bus when ctrl-c'ing out of the loop

CircuitPython_Templates/i2c_scan/code.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
import time
33
import board
44

5+
# To use default I2C bus (most boards)
56
i2c = board.I2C()
67

8+
# To create I2C bus on specific pins
9+
# import busio
10+
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
11+
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
12+
713
while not i2c.try_lock():
814
pass
915

1016
try:
1117
while True:
12-
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
18+
print(
19+
"I2C addresses found:",
20+
[hex(device_address) for device_address in i2c.scan()],
21+
)
1322
time.sleep(2)
1423

1524
finally: # unlock the i2c bus when ctrl-c'ing out of the loop

0 commit comments

Comments
 (0)