Add I2C scanner example.

This commit is contained in:
2025-02-09 11:59:38 -05:00
parent 043fa2fabb
commit 4063921340
9 changed files with 2515 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
idf_component_register(
SRCS "main.cpp"
INCLUDE_DIRS "."
)

View File

@@ -0,0 +1,17 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: '>=4.1.0'
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true
espressif/arduino-esp32: ^3.1.1

View File

@@ -0,0 +1,35 @@
#include "Arduino.h"
#include "Wire.h"
[[maybe_unused]] static const char *TAG = "i2c-scanner";
void i2c_scan()
{
uint8_t device_num = 0;
Serial.println("Scanning I2C devices...");
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.printf(
"[0x%.2x]: Device found with clock rate %lu and timeout %u\n",
address,
Wire.getClock(), Wire.getTimeOut());
device_num++;
} else if (error == 4) {
Serial.printf("[0x%.2x]: Unknown error.\n", address);
}
}
Serial.println(device_num > 0 ? "Done.\n" : "No I2C devices found.\n");
}
void setup()
{
Serial.begin(115200);
Wire.begin();
}
void loop()
{
i2c_scan();
}