-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2684 from caternuson/cp_i2c_scan_update
I2C Scanner Code Updates
- Loading branch information
Showing
8 changed files
with
137 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |