Skip to content

Instantly share code, notes, and snippets.

@haeshh
Created April 7, 2021 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haeshh/c0f7df6687a42c02d2b06873fe9cec0e to your computer and use it in GitHub Desktop.
Save haeshh/c0f7df6687a42c02d2b06873fe9cec0e to your computer and use it in GitHub Desktop.
I2C-Scanner für den ESP32; scannt beide Standardkanäle (I2C_1: SDA=GPIO 21, SCL=22; I2C_2: SDA=17, SCL=16) // leicht modifizierte Version des Codes von Kutscher07; https://github.com/espressif/arduino-esp32/issues/977#issuecomment-365734787
/* I2C address scanner
Source: Kutscher07
https://github.com/espressif/arduino-esp32/issues/977#issuecomment-365734787
slightly modified version (only one scan function,
passing the I2C channel as parameter)
2020-11-19 Heiko / unsinnsbasis.de
*/
#include <Wire.h>
// I2C channel 1: SDA and SCL pin
#define SDA1 21
#define SCL1 22
// I2C channel 2: SDA and SCL pin
#define SDA2 17
#define SCL2 16
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
void scan(TwoWire * tw, char channel) {
Serial.print("Scanning I2C addresses on channel ");
Serial.println(channel);
uint8_t cnt=0;
for (uint8_t i=0; i<128; i++) {
tw->beginTransmission(i);
uint8_t ec = tw->endTransmission(true);
if (ec == 0) {
if (i<16) Serial.print('0'); // add leading zero
Serial.print(i,HEX);
cnt++;
} else {
Serial.print("..");
}
Serial.print(' ');
if ((i&0x0f) == 0x0f) Serial.println();
}
Serial.print("Scan completed, ");
Serial.print(cnt);
Serial.println(" I2C device(s) found.");
Serial.println();
}
void setup() {
Serial.begin(115200);
I2Cone.begin(SDA1, SCL1, 400000);
I2Ctwo.begin(SDA2, SCL2, 400000);
}
void loop() {
scan(&I2Cone, '1');
delay(100);
scan(&I2Ctwo, '2');
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment