Add I2C scanner example.

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

4
esp/cpp/06_i2c-scanner/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
managed_components
dependencies.lock
sdkconfig.old

View File

@ -0,0 +1,17 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.26)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(
#[[NAME]] i2c-scanner
VERSION 0.1
DESCRIPTION "Simple I2C device scanner"
LANGUAGES CXX
)
# For writing pure cmake components, see the documentation
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)

View File

@ -0,0 +1,32 @@
# 06_i2c-scanner
![schematic](./schematic.png)
Simple I2C device scanner.
For this example I used this [SSD1306 OLED display](https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255).
To build the example run the following commands.
```bash
source ~/path/to/esp-idf/export.sh
mkdir build
cd build
cmake ..
make -j $(nproc)
# Flash to ESP32
make flash
# Open Serial Monitor, press CTRL+] to exit.
make monitor
```
Expected output in serial monitor at 115200 baud
```bash
Scanning I2C devices...
[0x3c]: Device found with clock rate 100000 and timeout 50
Done.
```

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();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@ shaunrd0/klips/esp/
├── 03_temp-humidity-web # Temperature and humidity sensor within a web browser.
├── 04_esp-idf-arduino # CMake example instead of Arduino IDE for ESP development.
├── 05_temp-humidity-web # Temperature and humidity sensor within a web browser.
├── 06_i2c-scanner # Simple I2C device scanner.
├── ESP32-basic-starter-kit.pdf # PDF for tutorials in ESP32 starter kit.
├── ESP32-dev-module.png
└── README.md