From 8ea00f2bca3438189d0d11cdb1c68469ddacce0b Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 5 Dec 2023 11:54:46 -0800 Subject: [PATCH 1/5] i2c scanners updates --- .../CircuitPython_I2C_Scan/code.py | 61 +++++++------------ .../arduino/i2c_scanner/i2c_scanner.ino | 61 +++++++++++++++++++ I2C_Scanners/circuitpython/code.py | 50 +++++++++++++++ 3 files changed, 134 insertions(+), 38 deletions(-) create mode 100644 I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino create mode 100644 I2C_Scanners/circuitpython/code.py diff --git a/CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py b/CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py index 010942fa4..572895fb8 100644 --- a/CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py +++ b/CircuitPython_Essentials/CircuitPython_I2C_Scan/code.py @@ -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() diff --git a/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino b/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino new file mode 100644 index 000000000..ad2e2608b --- /dev/null +++ b/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino @@ -0,0 +1,61 @@ +// -------------------------------------- +// i2c_scanner +// +// Modified from https://playground.arduino.cc/Main/I2cScanner/ +// -------------------------------------- + +#include + +// 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 +} diff --git a/I2C_Scanners/circuitpython/code.py b/I2C_Scanners/circuitpython/code.py new file mode 100644 index 000000000..669a267eb --- /dev/null +++ b/I2C_Scanners/circuitpython/code.py @@ -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.") From dba53b305f6cd6eed497f622b941d79b9ed6d11f Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 5 Dec 2023 12:07:58 -0800 Subject: [PATCH 2/5] add spdx --- I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino b/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino index ad2e2608b..af0be6de0 100644 --- a/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino +++ b/I2C_Scanners/arduino/i2c_scanner/i2c_scanner.ino @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries +// +// SPDX-License-Identifier: MIT // -------------------------------------- // i2c_scanner // @@ -26,7 +29,7 @@ void loop() { Serial.println("Scanning..."); nDevices = 0; - for(address = 1; address < 127; address++ ) + for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if @@ -37,20 +40,20 @@ void loop() { if (error == 0) { Serial.print("I2C device found at address 0x"); - if (address<16) + if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } - else if (error==4) + else if (error==4) { Serial.print("Unknown error at address 0x"); - if (address<16) + if (address<16) Serial.print("0"); Serial.println(address,HEX); - } + } } if (nDevices == 0) Serial.println("No I2C devices found\n"); From 92813152f929ff260aaa05b2f5f106cd9480a8bd Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 5 Dec 2023 12:26:59 -0800 Subject: [PATCH 3/5] arduino skips and linter shush --- I2C_Scanners/arduino/i2c_scanner/.gemma.test.skip | 0 I2C_Scanners/arduino/i2c_scanner/.neokeytrinkey_m0.test.skip | 0 I2C_Scanners/arduino/i2c_scanner/.rotarytrinkey_m0.test.skip | 0 .../arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip | 0 I2C_Scanners/arduino/i2c_scanner/.trinket_5v.test.skip | 0 I2C_Scanners/circuitpython/code.py | 2 +- 6 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 I2C_Scanners/arduino/i2c_scanner/.gemma.test.skip create mode 100644 I2C_Scanners/arduino/i2c_scanner/.neokeytrinkey_m0.test.skip create mode 100644 I2C_Scanners/arduino/i2c_scanner/.rotarytrinkey_m0.test.skip create mode 100644 I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip create mode 100644 I2C_Scanners/arduino/i2c_scanner/.trinket_5v.test.skip diff --git a/I2C_Scanners/arduino/i2c_scanner/.gemma.test.skip b/I2C_Scanners/arduino/i2c_scanner/.gemma.test.skip new file mode 100644 index 000000000..e69de29bb diff --git a/I2C_Scanners/arduino/i2c_scanner/.neokeytrinkey_m0.test.skip b/I2C_Scanners/arduino/i2c_scanner/.neokeytrinkey_m0.test.skip new file mode 100644 index 000000000..e69de29bb diff --git a/I2C_Scanners/arduino/i2c_scanner/.rotarytrinkey_m0.test.skip b/I2C_Scanners/arduino/i2c_scanner/.rotarytrinkey_m0.test.skip new file mode 100644 index 000000000..e69de29bb diff --git a/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip b/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip new file mode 100644 index 000000000..e69de29bb diff --git a/I2C_Scanners/arduino/i2c_scanner/.trinket_5v.test.skip b/I2C_Scanners/arduino/i2c_scanner/.trinket_5v.test.skip new file mode 100644 index 000000000..e69de29bb diff --git a/I2C_Scanners/circuitpython/code.py b/I2C_Scanners/circuitpython/code.py index 669a267eb..dd8f9a59b 100644 --- a/I2C_Scanners/circuitpython/code.py +++ b/I2C_Scanners/circuitpython/code.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: MIT -# pylint: disable=eval-used, unused-import +# pylint: disable=broad-except, eval-used, unused-import """CircuitPython I2C Device Address Scan""" import time From ae56f1a4985ad1b0b7f1f152c63fb99a3a050d6a Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 5 Dec 2023 13:01:25 -0800 Subject: [PATCH 4/5] fix skip --- .../{.slidetrinkey_m0gemma.test.skip => .slidetrinkey_m0} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename I2C_Scanners/arduino/i2c_scanner/{.slidetrinkey_m0gemma.test.skip => .slidetrinkey_m0} (100%) diff --git a/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip b/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0 similarity index 100% rename from I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0gemma.test.skip rename to I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0 From bca8bd6ade089f0b40dad17449f0f4fd3555fd8c Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 5 Dec 2023 13:09:06 -0800 Subject: [PATCH 5/5] fix skip --- .../i2c_scanner/{.slidetrinkey_m0 => .slidetrinkey_m0.test.skip} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename I2C_Scanners/arduino/i2c_scanner/{.slidetrinkey_m0 => .slidetrinkey_m0.test.skip} (100%) diff --git a/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0 b/I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0.test.skip similarity index 100% rename from I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0 rename to I2C_Scanners/arduino/i2c_scanner/.slidetrinkey_m0.test.skip