i2c scanners updates

This commit is contained in:
caternuson 2023-12-05 11:54:46 -08:00
parent 5c7509c2a9
commit 8ea00f2bca
3 changed files with 134 additions and 38 deletions

View file

@ -2,49 +2,34 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
# pylint: disable=bare-except, eval-used, unused-import
"""CircuitPython I2C Device Address Scan""" """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 time
import board import board
import busio
# List of potential I2C busses # To use default I2C bus (most boards)
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)") i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
# Determine which busses are valid # To create I2C bus on specific pins
found_i2c = [] # import busio
for name in ALL_I2C: # i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
try: # i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
print("Checking {}...".format(name), end="")
bus = eval(name)
bus.unlock()
found_i2c.append((name, bus))
print("ADDED.")
except:
print("SKIPPED.")
# Scan valid busses while not i2c.try_lock():
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 pass
try:
while True:
print( print(
name, "I2C addresses found:",
"addresses found:", [hex(device_address) for device_address in i2c.scan()],
[hex(device_address) for device_address in bus.scan()],
) )
bus.unlock()
time.sleep(2) 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()

View file

@ -0,0 +1,61 @@
// --------------------------------------
// 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
}

View file

@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: disable=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.")