Arduino projelerimizde I2C modüllerini kullanırken cihazın adres numarasını biliyor olmamız gerekiyor. Çoğu yaygın modülün hangi I2C adresine sahip olduğu biliniyor ve internette paylaşılan örneklerden bulunabiliyor.
Ancak I2C adresi hakkında hiçbir fikrimiz olmayan bir modül ile karşılaştığımızda veya modülümüz düşündüğümüz adres ile çalışmadığında ne yapmalıyız? Bu yazıda Arduino’ya bağlı I2C destekli modüllerin adres numaralarını bulmayı öğreneceğiz.
I2C iki elektronik cihaz arasında iletişim kurulmasını sağlayan özgün bir protokol. Bir adet master (sahip) cihaz ve teoride 127 adede kadar slave (köle) cihaz kullanımına izin veriyor. Arduino’muz bu durumda master olarak çalışıyor ve kullandığımız I2C destekli modüller de slave modunda üretiliyor.
I2C destekli modüller Arduino Uno ve Nano’ya SDA (veya A4) ve SCL (veya A5) adı verilen iki pin ile bağlanır. Arduino Mega 2560 kullanıyorsak D20 (SDA) ve D21 (SCL) pinlerini kullanmalıyız.
Bu kod sadece klasik bir tarama yapmakla kalmaz; Arduino Uno R4, ESP32, STM32 veya RP2040 gibi modern kartlarda bulunan birden fazla donanımsal I2C portunu (Wire, Wire1, Wire2, vb.) otomatik olarak tespit edip tarar. Dahası, kodun içerisinde geniş bir cihaz veritabanı bulunuyor. Ekranda sadece “0x3C” görmek yerine “0x3C => OLED (SSD1306 / SH1106)” gibi net ve anlaşılır sonuçlar alırsınız.
Kodun en can alıcı özelliklerinden biri de otomatik Multiplexer (Çoklayıcı) taramasıdır. Eğer projenizde aynı adrese sahip birden fazla sensör kullanmak için TCA9548A gibi bir I2C çoklayıcı kullanıyorsanız, tarayıcı bu entegreyi otomatik olarak algılar ve arkasına bağlı olan 8 farklı kanalı tek tek tarayarak sonucu size ağaç yapısında sunar. Ayrıca I2C bağlantılarındaki kararsızlıkları önlemek adına sıkı bir hata kontrol algoritması kullanılarak “hayalet” cihazların listeye girmesi engellenmiştir.
Kullanımı son derece basit: Kodu kopyalayın, kartınıza yükleyin ve Seri Port Ekranı’nı 115200 baud hızında açın. Saniyeler içinde tüm I2C hattınızın röntgenini çekmiş olacaksınız!
Arduino’da iyi olmak için öncelikle elektronikte iyi olmak gerekir. Türkiye’nin en iyi temel elektronik eğitim seti ARDUINO ÖNCESİ TEMEL ELEKTRONİK EĞİTİM VE DENEY SETİ‘ni keşfedin. Gereksiz bilgilerden arındırılmış basit ve eğlenceli anlatımla ELEKTRONİK öğrenin ve Arduino’ya METE HOCA farkıyla güçlü başlayın!
Sketch kodunu kopyalamak için pencerenin sağ üst köşesindeki butona tıklayabilirsiniz.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
/* * ╔══════════════════════════════════════════════════════════════════╗ * ║ METE HOCA ADVANCED I2C SCANNER ║ * ╠══════════════════════════════════════════════════════════════════╣ * ║ OVERVIEW: ║ * ║ This advanced I2C scanner detects devices across multiple ║ * ║ hardware I2C buses (Wire, Wire1, Wire2, Wire3). It identifies ║ * ║ the connected modules and maps them to their common names. ║ * ║ ║ * ║ HOW TO USE: ║ * ║ Upload the code to your microcontroller, open the Serial Monitor ║ * ║ at 115200 baud, and observe the output. The scanner will refresh ║ * ║ automatically based on the SCAN_INTERVAL_MS definition. Custom ║ * ║ SDA/SCL pins can be defined manually for ESP32/RP2040 boards. ║ * ║ ║ * ║ SUBTLE POINTS & NUANCES: ║ * ║ • Mux Recursion: Automatically detects TCA9548A multiplexers ║ * ║ (0x70-0x77) and scans all 8 of their sub-channels individually.║ * ║ (If this causes conflicts, comment out ENABLE_MUX_SCAN). ║ * ║ • Strict Validation: endTransmission() must return exactly 0. ║ * ║ This filters out NACKs, timeouts, and buffer overruns. ║ * ║ • Arduino Uno R4 WiFi: Be aware that the physical Qwiic ║ * ║ connector on this board is tied to Wire1, not the default Wire.║ * ║ • Sorting Algorithm: Results are strictly sorted by Port -> ║ * ║ Mux Address -> Mux Channel -> Device Address for readability. ║ * ╚══════════════════════════════════════════════════════════════════╝ */ #include <Wire.h> #define SCAN_INTERVAL_MS 3000 #define SCAN_DELAY_US 150 #define MAX_FOUND 32 #define ENABLE_MUX_SCAN #if defined(WIRE_INTERFACES_COUNT) #if WIRE_INTERFACES_COUNT >= 2 #define HAS_WIRE1 #endif #if WIRE_INTERFACES_COUNT >= 3 #define HAS_WIRE2 #endif #if WIRE_INTERFACES_COUNT >= 4 #define HAS_WIRE3 #endif #else #if defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_NANO_R4) || \ defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) || \ defined(CORE_TEENSY) || defined(ARDUINO_SAM_DUE) || \ defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_UNO_Q) #define HAS_WIRE1 #endif #endif struct FoundDevice { uint8_t address; uint8_t portIdx; uint8_t muxAddress; uint8_t muxChannel; }; FoundDevice foundDevices[MAX_FOUND]; uint8_t foundCount = 0; bool probeAddress(TwoWire& wire, uint8_t address) { wire.beginTransmission(address); uint8_t error = wire.endTransmission(); delayMicroseconds(SCAN_DELAY_US); return (error == 0); } bool isDevicePresent(const FoundDevice* deviceArray, uint8_t count, uint8_t address, uint8_t portIdx, uint8_t muxAddress, uint8_t muxChannel) { for (uint8_t i = 0; i < count; i++) { if (deviceArray[i].address == address && deviceArray[i].portIdx == portIdx && deviceArray[i].muxAddress == muxAddress && deviceArray[i].muxChannel == muxChannel) return true; } return false; } void addDevice(uint8_t address, uint8_t portIdx, uint8_t muxAddress, uint8_t muxChannel) { if (foundCount >= MAX_FOUND) return; if (isDevicePresent(foundDevices, foundCount, address, portIdx, muxAddress, muxChannel)) return; foundDevices[foundCount++] = {address, portIdx, muxAddress, muxChannel}; } #ifdef ENABLE_MUX_SCAN void scanMux(TwoWire& wire, uint8_t portIdx, uint8_t muxAddress) { for (uint8_t channel = 0; channel < 8; channel++) { wire.beginTransmission(muxAddress); wire.write(1 << channel); if (wire.endTransmission() != 0) continue; delayMicroseconds(200); for (uint8_t address = 1; address < 127; address++) { if (address >= 0x70 && address <= 0x77) continue; if (probeAddress(wire, address)) { addDevice(address, portIdx, muxAddress, channel); } } wire.beginTransmission(muxAddress); wire.write(0x00); wire.endTransmission(); delayMicroseconds(100); } } #endif void scanPort(TwoWire& wire, uint8_t portIdx) { for (uint8_t address = 1; address < 127; address++) { if (probeAddress(wire, address)) { addDevice(address, portIdx, 0, 0); #ifdef ENABLE_MUX_SCAN if (address >= 0x70 && address <= 0x77) { scanMux(wire, portIdx, address); } #endif } } } void sortResults() { if (foundCount < 2) return; for (uint8_t i = 0; i < foundCount - 1; i++) { for (uint8_t j = i + 1; j < foundCount; j++) { bool swapNeeded = false; const FoundDevice& a = foundDevices[i]; const FoundDevice& b = foundDevices[j]; if (a.portIdx > b.portIdx ) swapNeeded = true; else if (a.portIdx == b.portIdx ) { if (a.muxAddress > b.muxAddress) swapNeeded = true; else if (a.muxAddress == b.muxAddress) { if (a.muxChannel > b.muxChannel) swapNeeded = true; else if (a.muxChannel == b.muxChannel && a.address > b.address) swapNeeded = true; } } if (swapNeeded) { FoundDevice temp = foundDevices[i]; foundDevices[i] = foundDevices[j]; foundDevices[j] = temp; } } } } void printDeviceNames(uint8_t address) { bool isFirst = true; #define PRINT_NAME(s) do { if (!isFirst) Serial.print(F(", ")); \ Serial.print(F(s)); isFirst = false; } while(0) if (address == 0x0D) PRINT_NAME("QMC5883L (Compass Clone)"); if (address == 0x0E) PRINT_NAME("Pololu Tic (Stepper Motor Driver)"); if (address == 0x10) PRINT_NAME("VEML7700 (Light)"); if (address == 0x14) PRINT_NAME("MAG3110 (Magnetometer)"); if (address == 0x15) PRINT_NAME("Modulino Latch Relay"); if (address == 0x18) { PRINT_NAME("MCP9808 (High-Precision Temp)"); PRINT_NAME("SparkFun Qwiic Relay"); } if (address == 0x19) { PRINT_NAME("Modulino LED Matrix"); PRINT_NAME("LSM303 (Compass/Accel)"); } if (address == 0x1A) PRINT_NAME("MAX9744 (Audio Amplifier)"); if (address == 0x1C) PRINT_NAME("MMA8452Q / FXOS8700 (Accelerometer)"); if (address == 0x1D) PRINT_NAME("Modulino Vibro"); if (address == 0x1E) { PRINT_NAME("Modulino Buzzer"); PRINT_NAME("HMC5883L / LIS3MDL (Compass)"); } if (address == 0x20) { PRINT_NAME("Modulino Joystick"); PRINT_NAME("MCP23017 / PCF8574 (I/O Expander)"); } if (address == 0x23) PRINT_NAME("BH1750 (Light)"); if (address == 0x24) PRINT_NAME("PN532 (NFC/RFID Reader)"); if (address == 0x26) PRINT_NAME("STS40 (Temperature)"); if (address == 0x27) PRINT_NAME("LCD I2C (PCF8574)"); if (address == 0x28) PRINT_NAME("BNO055 (Smart IMU / Orientation)"); if (address == 0x29) { PRINT_NAME("Modulino Distance"); PRINT_NAME("VL53L0X / VL53L1X (Distance)"); PRINT_NAME("TCS34725 (Color)"); } if (address == 0x2A) PRINT_NAME("NAU7802 (Qwiic Scale / Load Cell)"); if (address == 0x2C) PRINT_NAME("CAP1188 (8-Ch Capacitive Touch)"); if (address == 0x2D) PRINT_NAME("CAP1188 (Alt Address)"); if (address == 0x33) PRINT_NAME("MLX90640 (IR Thermal Camera Array)"); if (address == 0x34) PRINT_NAME("AXP192 / AXP202 (Power Management - M5Stack)"); if (address == 0x36) { PRINT_NAME("Modulino Pixels"); PRINT_NAME("STEMMA Soil (Soil Moisture)"); } if (address == 0x38) { PRINT_NAME("AHT10 / AHT20 / AHT21 (Temp & Moisture)"); PRINT_NAME("VEML6070 (UV LSB)"); } if (address == 0x39) { PRINT_NAME("APDS9960 (Gesture/Color)"); PRINT_NAME("TSL2561 (Light)"); PRINT_NAME("VEML6070 (UV MSB)"); } if (address == 0x3A) PRINT_NAME("Modulino Knob"); if (address == 0x3C) PRINT_NAME("OLED (SSD1306 / SH1106)"); if (address == 0x3D) { PRINT_NAME("OLED (SSD1306 / SH1106)"); PRINT_NAME("Qwilino OLED"); } if (address == 0x3E) PRINT_NAME("Modulino Buttons"); if (address == 0x3F) PRINT_NAME("LCD I2C (PCF8574A)"); if (address == 0x40) { PRINT_NAME("HTU21D / Si7021 (Moisture)"); PRINT_NAME("PCA9685 (PWM)"); PRINT_NAME("INA219 (Current/Power)"); PRINT_NAME("HDC1080 (Temp & Moisture)"); } if (address == 0x41) PRINT_NAME("INA219 (Alternative Address)"); if (address == 0x42) PRINT_NAME("U-blox GPS/GNSS (Qwiic)"); if (address == 0x43) PRINT_NAME("SHTC3 (Temp & Moisture)"); if (address == 0x44) { PRINT_NAME("Modulino Thermo"); PRINT_NAME("SHT3x / SHT4x"); PRINT_NAME("INA219 (Alternative)"); PRINT_NAME("ISL29125 (RGB)"); } if (address == 0x45) { PRINT_NAME("SHT3x / SHT4x (Alt Address)"); PRINT_NAME("INA219 (Alternative)"); } if (address == 0x47) PRINT_NAME("TMP117 (High-Precision Temp)"); if (address == 0x48) { PRINT_NAME("ADS1115 / ADS1015 (ADC)"); PRINT_NAME("Qwilino Potentiometer"); } if (address == 0x49) { PRINT_NAME("ADS1115 / ADS1015 (ADC Alt)"); PRINT_NAME("Qwilino Temp"); } if (address == 0x4A) PRINT_NAME("MAX44009 (Light)"); if (address == 0x4B) PRINT_NAME("SparkFun Qwiic Keypad"); if (address == 0x4C) PRINT_NAME("EMC2101 (Fan Speed Controller / Temp)"); if (address == 0x4D) PRINT_NAME("MAX17043 (LiPo Fuel Gauge)"); if (address == 0x50) { PRINT_NAME("EEPROM (AT24C32 etc.)"); PRINT_NAME("Qwilino EEPROM"); } if (address == 0x51) { PRINT_NAME("PCF8563 RTC"); PRINT_NAME("Qwilino Clock"); } if (address == 0x52) PRINT_NAME("PAJ7620 (Gesture Sensor)"); if (address == 0x53) { PRINT_NAME("Modulino Light (Light/Color)"); PRINT_NAME("ADXL345 (Accelerometer)"); } if (address == 0x55) PRINT_NAME("BQ27441 / BQ27xxx (LiPo Fuel Gauge)"); if (address == 0x57) { PRINT_NAME("EEPROM (AT24C32 Alt)"); PRINT_NAME("MAX30105 (Pulse/Oximeter)"); } if (address == 0x58) PRINT_NAME("SGP30 (Air Quality / TVOC)"); if (address == 0x59) PRINT_NAME("SGP40 (VOC / Air Quality)"); if (address == 0x5A) { PRINT_NAME("MLX90614 (IR Temperature)"); PRINT_NAME("MPR121 (Capacitive Touch)"); PRINT_NAME("CCS811 (Air Quality)"); } if (address == 0x5B) PRINT_NAME("CCS811 (Alt Address)"); if (address == 0x5C) { PRINT_NAME("BH1750 (Alt Address)"); PRINT_NAME("AM2320 (Temp & Moisture)"); } if (address == 0x5D) PRINT_NAME("VL6180X (ToF Range + Ambient Light)"); if (address == 0x5F) PRINT_NAME("HTS221 (Humidity & Temp)"); if (address == 0x60) { PRINT_NAME("MCP4725 (DAC)"); PRINT_NAME("TEA5767 (FM Radio)"); PRINT_NAME("Adafruit Motor Shield V2"); PRINT_NAME("ATECC608 (Crypto)"); PRINT_NAME("VEML6075 (UV)"); } if (address == 0x61) PRINT_NAME("SCD30 (CO2 / Humidity / Temp)"); if (address == 0x62) PRINT_NAME("SCD40 / SCD41 (CO2 Sensor)"); if (address == 0x63) PRINT_NAME("AS5600 (Magnetic Encoder)"); if (address == 0x64) PRINT_NAME("SparkFun Qwiic Twist (RGB Encoder)"); if (address == 0x68) { PRINT_NAME("MPU6050 / MPU9250 (IMU)"); PRINT_NAME("DS3231 / DS1307 RTC"); PRINT_NAME("AMG8833 (Thermal Camera)"); } if (address == 0x69) { PRINT_NAME("MPU6050 / MPU9250 (Alt Address)"); PRINT_NAME("AMG8833 (Thermal Camera Alt)"); } if (address == 0x6A) PRINT_NAME("Modulino Movement"); if (address == 0x6B) { PRINT_NAME("Modulino Movement (Jumpered)"); PRINT_NAME("LSM6DS33 / L3GD20H (Pololu IMU)"); } if (address == 0x6C) PRINT_NAME("LPS22HB / LPS25HB (Barometric Pressure)"); if (address == 0x6D) PRINT_NAME("LPS22HB (Alt Address)"); if (address == 0x6F) PRINT_NAME("MCP7940N RTC"); if (address == 0x70) { PRINT_NAME("TCA9548A (I2C Mux, Addr=0)"); PRINT_NAME("PCA9685 (Broadcast)"); PRINT_NAME("HT16K33 (LED Matrix)"); } if (address == 0x71) { PRINT_NAME("TCA9548A (I2C Mux, Addr=1)"); PRINT_NAME("HT16K33 (LED Matrix Alt)"); } if (address == 0x72) PRINT_NAME("TCA9548A (I2C Mux, Addr=2)"); if (address == 0x73) PRINT_NAME("TCA9548A (I2C Mux, Addr=3)"); if (address == 0x74) { PRINT_NAME("TCA9548A (I2C Mux, Addr=4)"); PRINT_NAME("IS31FL3731 (LED Driver)"); } if (address == 0x75) { PRINT_NAME("TCA9548A (I2C Mux, Addr=5)"); PRINT_NAME("ICM-20948 (9-Axis IMU)"); } if (address == 0x76) { PRINT_NAME("TCA9548A (I2C Mux, Addr=6)"); PRINT_NAME("BME280 / BMP280 / BME680"); PRINT_NAME("MS5611 / BMP388 (Barometer)"); } if (address == 0x77) { PRINT_NAME("TCA9548A (I2C Mux, Addr=7)"); PRINT_NAME("BMP180 / BME280 / BMP280 / BME680"); PRINT_NAME("MS5611 / BMP388 (Alt)"); } if (address == 0x7D) PRINT_NAME("SparkFun Qwiic RFID Reader"); if (address == 0x7E) PRINT_NAME("Modulino Movement (Auxiliary)"); if (isFirst) Serial.print(F("Unknown Device")); #undef PRINT_NAME } void printResults() { Serial.println(F("\n============ METE HOCA ADVANCED I2C SCANNER ============")); if (foundCount == 0) { Serial.println(F(" No connected I2C devices found...")); } else { for (uint8_t i = 0; i < foundCount; i++) { const FoundDevice& device = foundDevices[i]; Serial.print(F(" 0x")); if (device.address < 16) Serial.print(F("0")); Serial.print(device.address, HEX); Serial.print(F(" @ Wire")); if (device.portIdx > 0) Serial.print((int)device.portIdx); if (device.muxAddress != 0) { Serial.print(F(" [Mux:0x")); if (device.muxAddress < 16) Serial.print(F("0")); Serial.print(device.muxAddress, HEX); Serial.print(F(" Ch")); Serial.print((int)device.muxChannel); Serial.print(F("]")); } Serial.print(F(" => ")); printDeviceNames(device.address); Serial.println(); } } Serial.println(F("==========================================================\n")); } void setup() { while (!Serial && millis() < 3000); Serial.begin(115200); #if (defined(WIRE_SDA_PIN) && defined(WIRE_SCL_PIN)) && \ (defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RP2040)) Wire.begin(WIRE_SDA_PIN, WIRE_SCL_PIN); #else Wire.begin(); #endif Wire.setClock(100000UL); #ifdef HAS_WIRE1 #if (defined(WIRE1_SDA_PIN) && defined(WIRE1_SCL_PIN)) && \ (defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_RP2040)) Wire1.begin(WIRE1_SDA_PIN, WIRE1_SCL_PIN); #else Wire1.begin(); #endif Wire1.setClock(100000UL); #endif #ifdef HAS_WIRE2 Wire2.begin(); Wire2.setClock(100000UL); #endif #ifdef HAS_WIRE3 Wire3.begin(); Wire3.setClock(100000UL); #endif } void loop() { foundCount = 0; scanPort(Wire, 0); #ifdef HAS_WIRE1 scanPort(Wire1, 1); #endif #ifdef HAS_WIRE2 scanPort(Wire2, 2); #endif #ifdef HAS_WIRE3 scanPort(Wire3, 3); #endif sortResults(); printResults(); delay(SCAN_INTERVAL_MS); } |










