Skip to content

Commit 56e39c7

Browse files
authored
Merge pull request #2684 from caternuson/cp_i2c_scan_update
I2C Scanner Code Updates
2 parents d041b3a + bca8bd6 commit 56e39c7

8 files changed

Lines changed: 137 additions & 38 deletions

File tree

CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,34 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
# pylint: disable=bare-except, eval-used, unused-import
6-
75
"""CircuitPython I2C Device Address Scan"""
6+
# If you run this and it seems to hang, try manually unlocking
7+
# your I2C bus from the REPL with
8+
# >>> import board
9+
# >>> board.I2C().unlock()
10+
811
import time
912
import board
10-
import busio
11-
12-
# List of potential I2C busses
13-
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")
14-
15-
# Determine which busses are valid
16-
found_i2c = []
17-
for name in ALL_I2C:
18-
try:
19-
print("Checking {}...".format(name), end="")
20-
bus = eval(name)
21-
bus.unlock()
22-
found_i2c.append((name, bus))
23-
print("ADDED.")
24-
except:
25-
print("SKIPPED.")
2613

27-
# Scan valid busses
28-
if len(found_i2c):
29-
print("-" * 40)
30-
print("I2C SCAN")
31-
print("-" * 40)
32-
while True:
33-
for bus_info in found_i2c:
34-
name = bus_info[0]
35-
bus = bus_info[1]
36-
37-
while not bus.try_lock():
38-
pass
14+
# To use default I2C bus (most boards)
15+
i2c = board.I2C() # uses board.SCL and board.SDA
16+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
3917

40-
print(
41-
name,
42-
"addresses found:",
43-
[hex(device_address) for device_address in bus.scan()],
44-
)
18+
# To create I2C bus on specific pins
19+
# import busio
20+
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
21+
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
4522

46-
bus.unlock()
23+
while not i2c.try_lock():
24+
pass
4725

26+
try:
27+
while True:
28+
print(
29+
"I2C addresses found:",
30+
[hex(device_address) for device_address in i2c.scan()],
31+
)
4832
time.sleep(2)
49-
else:
50-
print("No valid I2C bus found.")
33+
34+
finally: # unlock the i2c bus when ctrl-c'ing out of the loop
35+
i2c.unlock()

I2C_Scanners/arduino/i2c_scanner/.gemma.test.skip

Whitespace-only changes.

I2C_Scanners/arduino/i2c_scanner/.neokeytrinkey_m0.test.skip

Whitespace-only changes.

I2C_Scanners/arduino/i2c_scanner/.rotarytrinkey_m0.test.skip

Whitespace-only changes.

I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0.test.skip

Whitespace-only changes.

I2C_Scanners/arduino/i2c_scanner/.trinket_5v.test.skip

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
// --------------------------------------
5+
// i2c_scanner
6+
//
7+
// Modified from https://playground.arduino.cc/Main/I2cScanner/
8+
// --------------------------------------
9+
10+
#include <Wire.h>
11+
12+
// Set I2C bus to use: Wire, Wire1, etc.
13+
#define WIRE Wire
14+
15+
void setup() {
16+
WIRE.begin();
17+
18+
Serial.begin(9600);
19+
while (!Serial)
20+
delay(10);
21+
Serial.println("\nI2C Scanner");
22+
}
23+
24+
25+
void loop() {
26+
byte error, address;
27+
int nDevices;
28+
29+
Serial.println("Scanning...");
30+
31+
nDevices = 0;
32+
for(address = 1; address < 127; address++ )
33+
{
34+
// The i2c_scanner uses the return value of
35+
// the Write.endTransmisstion to see if
36+
// a device did acknowledge to the address.
37+
WIRE.beginTransmission(address);
38+
error = WIRE.endTransmission();
39+
40+
if (error == 0)
41+
{
42+
Serial.print("I2C device found at address 0x");
43+
if (address<16)
44+
Serial.print("0");
45+
Serial.print(address,HEX);
46+
Serial.println(" !");
47+
48+
nDevices++;
49+
}
50+
else if (error==4)
51+
{
52+
Serial.print("Unknown error at address 0x");
53+
if (address<16)
54+
Serial.print("0");
55+
Serial.println(address,HEX);
56+
}
57+
}
58+
if (nDevices == 0)
59+
Serial.println("No I2C devices found\n");
60+
else
61+
Serial.println("done\n");
62+
63+
delay(5000); // wait 5 seconds for next scan
64+
}

I2C_Scanners/circuitpython/code.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# pylint: disable=broad-except, eval-used, unused-import
6+
7+
"""CircuitPython I2C Device Address Scan"""
8+
import time
9+
import board
10+
import busio
11+
12+
# List of potential I2C busses
13+
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")
14+
15+
# Determine which busses are valid
16+
found_i2c = []
17+
for name in ALL_I2C:
18+
try:
19+
print("Checking {}...".format(name), end="")
20+
bus = eval(name)
21+
bus.unlock()
22+
found_i2c.append((name, bus))
23+
print("ADDED.")
24+
except Exception as e:
25+
print("SKIPPED:", e)
26+
27+
# Scan valid busses
28+
if len(found_i2c):
29+
print("-" * 40)
30+
print("I2C SCAN")
31+
print("-" * 40)
32+
while True:
33+
for bus_info in found_i2c:
34+
name = bus_info[0]
35+
bus = bus_info[1]
36+
37+
while not bus.try_lock():
38+
pass
39+
40+
print(
41+
name,
42+
"addresses found:",
43+
[hex(device_address) for device_address in bus.scan()],
44+
)
45+
46+
bus.unlock()
47+
48+
time.sleep(2)
49+
else:
50+
print("No valid I2C bus found.")

0 commit comments

Comments
 (0)