Skip to content

Update CP I2C Scan Example #1925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2021
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
14 changes: 11 additions & 3 deletions CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""CircuitPython Essentials I2C Scan example"""
"""CircuitPython I2C Device Address Scan"""
# If you run this and it seems to hang, try manually unlocking
# your I2C bus from the REPL with
# >>> import board
Expand All @@ -7,15 +7,23 @@
import time
import board

# To use default I2C bus (most boards)
i2c = board.I2C()

# To create I2C bus on specific pins
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040

while not i2c.try_lock():
pass

try:
while True:
print("I2C addresses found:", [hex(device_address)
for device_address in i2c.scan()])
print(
"I2C addresses found:",
[hex(device_address) for device_address in i2c.scan()],
)
time.sleep(2)

finally: # unlock the i2c bus when ctrl-c'ing out of the loop
Expand Down
11 changes: 10 additions & 1 deletion CircuitPython_Templates/i2c_scan/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
import time
import board

# To use default I2C bus (most boards)
i2c = board.I2C()

# To create I2C bus on specific pins
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040

while not i2c.try_lock():
pass

try:
while True:
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
print(
"I2C addresses found:",
[hex(device_address) for device_address in i2c.scan()],
)
time.sleep(2)

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