Skip to content

Commit

Permalink
Merge pull request #2684 from caternuson/cp_i2c_scan_update
Browse files Browse the repository at this point in the history
I2C Scanner Code Updates
  • Loading branch information
caternuson authored Dec 8, 2023
2 parents d041b3a + bca8bd6 commit 56e39c7
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 38 deletions.
61 changes: 23 additions & 38 deletions CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,34 @@
#
# SPDX-License-Identifier: MIT

# pylint: disable=bare-except, eval-used, unused-import

"""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
# >>> board.I2C().unlock()

import time
import board
import busio

# List of potential I2C busses
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")

# Determine which busses are valid
found_i2c = []
for name in ALL_I2C:
try:
print("Checking {}...".format(name), end="")
bus = eval(name)
bus.unlock()
found_i2c.append((name, bus))
print("ADDED.")
except:
print("SKIPPED.")

# Scan valid busses
if len(found_i2c):
print("-" * 40)
print("I2C SCAN")
print("-" * 40)
while True:
for bus_info in found_i2c:
name = bus_info[0]
bus = bus_info[1]

while not bus.try_lock():
pass
# To use default I2C bus (most boards)
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

print(
name,
"addresses found:",
[hex(device_address) for device_address in bus.scan()],
)
# 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

bus.unlock()
while not i2c.try_lock():
pass

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

finally: # unlock the i2c bus when ctrl-c'ing out of the loop
i2c.unlock()
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
64 changes: 64 additions & 0 deletions I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------

#include <Wire.h>

// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire

void setup() {
WIRE.begin();

Serial.begin(9600);
while (!Serial)
delay(10);
Serial.println("\nI2C Scanner");
}


void loop() {
byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
WIRE.beginTransmission(address);
error = WIRE.endTransmission();

if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");

nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(5000); // wait 5 seconds for next scan
}
50 changes: 50 additions & 0 deletions I2C_Scanners/circuitpython/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# pylint: disable=broad-except, eval-used, unused-import

"""CircuitPython I2C Device Address Scan"""
import time
import board
import busio

# List of potential I2C busses
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")

# Determine which busses are valid
found_i2c = []
for name in ALL_I2C:
try:
print("Checking {}...".format(name), end="")
bus = eval(name)
bus.unlock()
found_i2c.append((name, bus))
print("ADDED.")
except Exception as e:
print("SKIPPED:", e)

# Scan valid busses
if len(found_i2c):
print("-" * 40)
print("I2C SCAN")
print("-" * 40)
while True:
for bus_info in found_i2c:
name = bus_info[0]
bus = bus_info[1]

while not bus.try_lock():
pass

print(
name,
"addresses found:",
[hex(device_address) for device_address in bus.scan()],
)

bus.unlock()

time.sleep(2)
else:
print("No valid I2C bus found.")

0 comments on commit 56e39c7

Please sign in to comment.